Log4net logging, detailed configuration (for your own use), and log4net Logging

Source: Internet
Author: User
Tags log4net

Log4net logging, detailed configuration (for your own use), and log4net Logging

[From Baidu encyclopedia] The log4net library is based on the Apache log4j framework in Microsoft. NET platform is a tool that helps programmers output log information to various targets (console, files, databases, etc.

1. First Add a reference to log4net. dll. You can download nuget from [http://logging.apache.org/log4net/download_log4net.cgi #

2. Configure in web. config (App. config)

1 <configuration> 2 <configSections> 3 <section name = "log4net" type = "log4net. config. log4NetConfigurationSectionHandler, log4net "/> 4 </configSections> 5 <log4net> 6 <! -- Define some output appenders --> 7 <appender name = "RollingLogFileAppender" type = "log4net. appender. rollingFileAppender "> 8 <file value =" test.txt "/> file name 9 <appendToFile value =" true "/> append or overwrite 10 <maxSizeRollBackups value =" 10 "/> quantity/count 11 <maximumFileSize value = "1024KB"/> txt Size 12 <rollingStyle value = "Size"/> by Size 13 <staticLogFileName value = "true"/> static name 14 <layout type = "log4net. layout. patternLayout "> Log File Format 15 <conversionPattern value =" % date [% thread] %-5 level % logger-% message % newline "/> 16 </layout> 17 </ appender> 18 <root> 19 <level value = "DEBUG"/> error level 20 <appender-ref = "RollingLogFileAppender"/> 21 </root> 22 </log4net> 23 </configuration>
View Code

2.1. This is the most basic configuration. If it is used in projects, I am used to dividing log names by time.

2.2 level from large to small: OFF> FATAL> ERROR> WARN> INFO> DEBUG> TRACE> ALL. Generally, an Error occurs.

3. Start recording at the beginning of the program. Add log4net. Config. XmlConfigurator. Configure () to Global. asax at the beginning of the program ();

4. LogManager. GetLogger (typeof (Program). Debug ("information ")

ILog logger=LogManager.GetLogger(typeof(Test))

It has been configured and can be used.

About rolling logs]

<Root>
<Level value = "DEBUG"/>
<Appender-ref = "RollingLogFileAppender"/> This line indicates log scrolling.
</Root>

What does rolling log mean?

The number of Log Files Added to us is 10. When the size of the test.txt file exceeds 1 MB (<maximumFileSize value = "1024KB"/> then, duplicate cut copies are performed.

Other logging Components]

Besides Log4Net, Logging Application Block in the Enterprise Library, Apache CommonLog, and NLog should be similar in usage.

I have used other products except Log4Net. Please feel free to contact me ~

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.