Original address http://www.cnblogs.com/jfzhu/archive/2012/12/01/2797013.html
Reprinted please indicate the source
Once our applicationProgramYou cannot debug the program after it is transplanted to the production environment. At this time, logs generated by the program are particularly important .. Net contains system. diagnostics. trace class. You can add a listener for the trace to output the trace content to a log file. However, this will cause the log file to be too large for a long time. Log4net is an open-source log component that contains many different appender. These appender are used to output the log Content to different places, such as adonetappender (output to database ), consoleappender (output to the console), eventlogappender (output to Windows event log), and fileappender (output to a log file ). This article describes rollingfileappender. As the name suggests, it can generate log files in a "scroll" manner, that is, it can output logs to multiple files. "Scroll" can be based on two methods: (1) file size. For example, if we set the log file size to not exceed 1 MB, when the size of a log file exceeds 1 MB, rollingfileappender generates a new log file; (2) date. This method generates a log file every day. In this article, I will give an example based on the two rolling methods.
At the time of writing this article, log4net has been released to version 1.2.11 (the new version has not been released yet ). Available in http://logging.apache.org/log4net/Source codeYou can also directly download binaries. After downloading binaries, you can see it is different in the \ log4net-1.2.11-bin-newkey \ log4net-1.2.11 \ bin \ net folder.. Net compiled log4net. DLL, including 2.0, 3.0, 3.5, and 4.0. Select the. NET version you want to use and add log4net. DLL to your project reference. The following describes how to use log4net and log4net configuration files for an ASP. NET application.
First, add the line in assemblyinfo. CS.
[Assembly: log4net. config. xmlconfigurator (watch = true)]
ThenCodeAs follows:
Public Class Logger { Private Static Ilog logger = logmanager. getlogger ( " Mycompany. Logger " ); Public Static Void Logdebug ( String Methodname, String Message) {logger. debug (methodname + " :: " + Message );} Public Static Void Loginfo ( String Methodname, String Message) {logger. Info (methodname + " :: " + Message );} Public Static Void Logwarning ( String Methodname, String Message) {logger. Warn (methodname + " :: " + Message );} Public Static Void Logerror ( String Methodname, String Message) {logger. Error (methodname + " :: " +Message );} Public Static Void Logfatal ( String Methodname, String Message) {logger. Fatal (methodname + " :: " + Message );}}
Protected Void Button#click ( Object Sender, eventargs e) {methodbase = New Stackframe (). getmethod (); String Methodstring = This . GetType (). Name + " :: " + Methodbase. Name; Try {Logger. logdebug (methodstring, " Button is clicked " );} Catch (Exception ex) {logger. logerror (methodstring, Ex. tostring ());}}
In the preceding example, the logger class is used to encapsulate log4net. In the Click Event processor of button1, the logger. logdebug method is used. The configuration file web. config of the ASP. NET web application is as follows:
< Configuration > < Configsections > < Section Name = "Log4net" Type = "Log4net. config. log4netconfigurationsectionhandler, log4net" /> </ Configsections > < Log4net Debug = "False" > < Root > < Level Value = "Debug" /> </ Root > < Appender Name = "Rollinglogfileappenderwebform" Type = "Log4net. appender. rollingfileappender" > < File Value = "C: \ temp \ mycompany.log.txt" /> < Appendtofile Value = "True" /> < Maxsizerollbackups Value = "10" /> < Maximumfilesize Value = "1 MB" /> < Rollingstyle Value = "Size" /> < Staticlogfilename Value = "True" /> < Layout Type = "Log4net. layout. patternlayout" > < Param Name = "Conversionpattern" Value = "% D [% T] %-5 p % C-% m % N" /> </ Layout > </ Appender > < Logger Name = "Mycompany. Logger" > < Appender-ref Ref = "Rollinglogfileappenderwebform" /> </ Logger > </ Log4net > < System. Web > < Compilation Debug = "True" Targetframework = "4.0" /> </ System. Web > </ Configuration >
Log4net defines different levels of log Content, from high to low, followed by fatal (fatal error), error (general error), warn (warning), Info (General Information) debug (debugging information ). You can set the Log Level in the configuration file. The above example shows that we have defined <level value = "debug"/> in the root, indicating that all levels above debug can be output to the log. <Logger name = "mycompany. Logger"> is defined in logger. mycompany. logger is used in our program:
Private Static ilog logger = logmanager. getlogger ("mycompany. Logger ");
In the definition of appender, rollingstyle (scroll mode) is size, which is based on the file size. Maximumfilesize (maximum file size) is 1 MB, and maxsizerollbackups exceeds. 2... mycompany.log.txt.10. If the number of logs exceeds 10, it will be overwritten from mycompany.log.txt. 1.
The following parameters can be used in layout definition:
% M (Message): Log message
% N (New Line): line feed
% D (datetime): Current Time
% R (Run time): number of milliseconds consumed by the program from running to executing to the current statement
% T (thread ID): thread ID
% P (priority): Current Log priority level, namely debug, info, warn... And so on
% C (class): name of the current log object
% F (File): name of the output statement.
% L (line): the row number of the output statement.
% Number: indicates the minimum length of the item. If not, fill it with spaces. For example, "%-5level" indicates that the minimum width of the level is 5 characters, if the actual length is less than 5 characters, fill it with spaces.
The output result of the preceding example "% d [% T] %-5 p % C-% m % N" is as follows:
00:21:12, 970 [17] Debug mycompany. logger-webform=aspx: button#click: button is clicked
See http://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html for details
The following is an example of time-based scrolling, listing only the configuration file section of log4net:
< Log4net Debug = "False" > < Root > < Level Value = "Debug" /> </ Root > < Appender Name = "Rollinglogfileappenderwebform" Type = "Log4net. appender. rollingfileappender" > < File Value = "C: \ temp \ mycompany.log.txt" /> < Appendtofile Value = "True" /> < Rollingstyle Value = "Date" /> < Datepattern Value = "Yyyymmdd" /> < Staticlogfilename Value = "True" /> < Layout Type = "Log4net. layout. patternlayout" > < Param Name = "Conversionpattern" Value = "% D [% T] %-5 p % C [% x]-% m % N" /> </ Layout > </ Appender > < Logger Name = "Mycompany. Logger" > < Appender-ref Ref = "Rollinglogfileappenderwebform" /> </ Logger > </ Log4net >
You can see that rollingstyle is changed to date. Datepattern is yyyymmdd. In this example, every day is written into a single file. The log file name of the day is mycompany.log.txt, and non-logs of the day will carry the date of the day. Of course, you may need to regularly clean up your log files.
conclusion: log4net is an open-source log component. Its rollingfileappender can output logs to multiple files. There are two ways to implement rolling. rollingstyle is size or date.