here is the next one to optimize, the previous article: Error log information logging in ASP.
Log4net is used to record the log, the program can be run in the process of information output to some places (files, databases, eventlog, etc.), the log is the program's black box, you can check the system through the log operation process, so as to discover the system problems.
1. Quoting Log4net.dll
Bin\net\2.0\release, do not reference the debug version
2. Add a configuration in Web. config
Add the following configuration information to Web. config
log4net Configuration<Configuration> <configsections> < Sectionname= "Log4net"type= "log4net." Config.log4netconfigurationsectionhandler, log4net "/> </configsections> <log4net> <!--Define some output appenders - <Appendername= "Rollinglogfileappender"type= "log4net." Appender.rollingfileappender "> <filevalue= "Log/test.txt"/> <Appendtofilevalue= "true"/> <maxsizerollbackupsvalue= "Ten"/> <maximumFileSizevalue= "1024KB"/> <Rollingstylevalue= "Size"/> <Staticlogfilenamevalue= "true"/> <Layouttype= "log4net." Layout.patternlayout "> <Conversionpatternvalue= "%date [%thread]%-5level%logger-%message%newline"/> </Layout> </Appender> <Root> < Levelvalue= "DEBUG"/> <Appender-refref= "Rollinglogfileappender"/> </Root> </log4net></Configuration>
The nodes inside are explained below.
3. Initialize: Add log4net at the beginning of the program. Config.XmlConfigurator.Configure (); Do not add to the page load
Add code to Global.asax.cs
Log4net. Config.XmlConfigurator.Configure (); // Read about log4net configuration information in the configuration file
The code that writes the log to the file will change to:
if (ex! = null // Write exception information to the log file // Strin G FileName = DateTime.Now.ToString ("Yyyy-mm-dd"); // File.appendalltext (FilePath + FileName + ". txt", ex. ToString (), System.Text.Encoding.UTF8); ILog logger = Logmanager.getlogger ( errormsg ); Logger. Error (ex. ToString ()); }
Note: The namespaces introduced are: using Log4net; do not reference to: using Common.logging (This is the logging component in spring)
Execute an error code, in the project's log folder, found a test.txt log information file, which records the error information.
One of the first acts:
Explanation of the parameters of the configuration:
Code:
log4net Configuration<Configuration> <configsections> < Sectionname= "Log4net"type= "log4net." Config.log4netconfigurationsectionhandler, log4net "/> </configsections> <log4net> <!--Define some output appenders - <Appendername= "Rollinglogfileappender"type= "log4net." Appender.rollingfileappender "> <filevalue= "Log/test.txt"/> <Appendtofilevalue= "true"/> <maxsizerollbackupsvalue= "Ten"/> <maximumFileSizevalue= "1024KB"/> <Rollingstylevalue= "Size"/> <Staticlogfilenamevalue= "true"/> <Layouttype= "log4net." Layout.patternlayout "> <Conversionpatternvalue= "%date [%thread]%-5level%logger-%message%newline"/> </Layout> </Appender> <Root> < Levelvalue= "DEBUG"/> <Appender-refref= "Rollinglogfileappender"/> </Root> </log4net></Configuration>
Appender: The logs can be output to different locations, different output targets correspond to different appender:rollingfileappender (scrolling files), Adonetappender (database), Smtpappender (mail) and so on.
File : The file name of the log
appendtofile: Append to Text
maximumfilesize: maximum number of KB files
maxsizerollbackups: The maximum number of log files stored, assuming test.txt can save 1M files, when Test.txt is full, Log4net will create a file outside the assumption is called Test1.txt, and then the test.txt in the 1M content copied into the Test1.txt, and then empty the contents of the Test.txt to reload the log information, We'll build another file if it's full. Assuming that it is called test2.txt, as before, until the maximum number of files set is 10, and then full, it will delete the contents of the first backup to move the contents of the present.
rollingstyle: How files are scrolled by size
staticlogfilename: static file name
layout: The style of the log file,%date is the date, open the first line of the log can be seen,%thread error thread id,%-5level error level,%logger is Global.asax.cs parameters,% Message error messages,%newline last line break.
4. Complete Log4net Configuration
The front is the basic configuration, usually in the project can be configured with the following
<log4net> <!--OFF, FATAL, ERROR, WARN, INFO, DEBUG, all - <!--Set Root Logger level-to-ERROR and its appenders - <Root> < Levelvalue= "All"/> <Appender-refref= "Sysappender"/> </Root> <!--Print only messages of level DEBUG or above in the packages - <Loggername= "Weblogger"> < Levelvalue= "DEBUG"/> </Logger> <Appendername= "Sysappender"type= "log4net." Appender.rollingfileappender,log4net " > <paramname= "File"value= "app_data/" /> <paramname= "Appendtofile"value= "true" /> <paramname= "Rollingstyle"value= "Date" /> <paramname= "Datepattern"value= "" Logs_"yyyymmdd". txt" " /> <paramname= "Staticlogfilename"value= "false" /> <Layouttype= "log4net." Layout.patternlayout,log4net "> <paramname= "Conversionpattern"value= "%d [%t]%-5p%c-%m%n" /> <paramname= "Header"value= "& #13;& #10;----------------------header--------------------------& #13;& #10;" /> <paramname= "Footer"value= "& #13;& #10;----------------------footer--------------------------& #13;& #10;" /> </Layout> </Appender> <Appendername= "ConsoleApp"type= "log4net." Appender.consoleappender,log4net "> <Layouttype= "log4net." Layout.patternlayout,log4net "> <paramname= "Conversionpattern"value= "%d [%t]%-5p%c-%m%n" /> </Layout> </Appender> </log4net>
Rollingstyle:date Logging by date
Datepattern: Log file name
Staticlogfilename:false the way file names are dynamic
This is done, here is the log in the form of text records, if you want to record information in the database or mail to send the message is only changed to configure the code is not necessary, please find the relevant configuration.
Log4net Log Records