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 ~