I. Log4net INTRODUCTION
Log4net is Apache next open source project, we can control the output destination of log information (database, console, text document, file). A variety of log information output modes are defined in log4net.
My biggest headache when doing projects is that after the release of the program to the formal environment, we cannot debug and the problem is difficult to re-start, so we need a lot of log data to accurately track the health of the program.
Log4net can help me to solve this problem, for the output of the log we do not need human intervention, it can be as needed to output logs to the console, text files, Windows log Event Viewer, including databases, mail and so on, so that we quickly track program bugs.
Two. Log4net Simple to use
The use of log4net must introduce Log4net.dll files, because log4net is open source, so you can directly add the source code to the project. The new DLL file is then generated.
1. First quote Log4Net.dll
2. In a Web program, we can add this to the Application_Start (object sender, EventArgs e) method of the Global.asax file, initializing the configuration information when the program starts:
Log4net. Config.XmlConfigurator.Configure (); --This is the equivalent of initializing the registration log4net,
Note: If it is not a Web program, write this sentence each time it is called.
3. Configure the Web. config
The following use configures one of the simplest file output and console outputs.
Log4net configuration file, which is added to the app. Config or Web. config file:
<configSections>
<section name= "log4net" type= "log4net. Config.log4netconfigurationsectionhandler,log4net "/>
</configSections>
<log4net>
<appender name= "Fileappender" type= "log4net. Appender.fileappender ">
<file value= "D:\a.log" ></file>
<appendtofile value= "true" ></appendToFile>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "[%d]:%n%m%n" ></conversionPattern>
</layout>
</appender>
<appender name= "Consoleappender" type= "log4net. Appender.consoleappender ">
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "[%d]:%n%m%n" ></conversionPattern>
</layout>
</appender>
<root>
<level value= "INFO" ></level>
<appender-ref ref= "Fileappender" ></appender-ref>
<appender-ref ref= "Consoleappender" ></appender-ref>
</root>
<logger name= "MyLogger" >
<level value= "ERROR" ></level>
<appender-ref ref= "Fileappender" ></appender-ref>
<appender-ref ref= "Consoleappender" ></appender-ref>
</logger>
</log4net>
Console program Test Examples:
ILog log = Logmanager.getlogger ("MyLogger");
static void Main (string[] args)
{
Log4net. Config.XmlConfigurator.Configure ();
Program program = new Program ();
Program.log.Info (">>>>>>>>>>>>>>");
Program.log.Info (">>>>>>>>>>>>>>1");
Program.log.Info (">>>>>>>>>>>>>>2");
}
Note: The above example is not output any log information because of the configuration level limit. Let's take a look at the structure of the log
Three. Log4net structure
Log4net is mainly divided into 5 core components: Logger, Appender, Filter, Layout, Object Render.
Logger:
Primarily used to record the classification of logs and the level of control logs. It can output log information in a variety of formats, and it can also control the output level of the log. The simple example above looks like a log output, but does not output any log results because we have a limit on the log level.
Appender:
Here we simply call it "the attachment". This means that the output of the log must depend on it, more specifically in the form of text output, or console format output must depend on it to control. Log4net defines a variety of attachments:
(1) Adonetappender logs to the database. SQL and stored procedures can be used in two ways.
(2) Ansicolorterminalappender write high-brightness log event in ANSI window terminal.
(3) Aspnettraceappender can view logged logs in the same way as trace in ASP.
(4) Bufferingforwardingappender cache log events before outputting to sub-appenders.
(5) Consoleappender the log output to the console.
(6) Eventlogappender writes the log to Windows Event log.
(7) Fileappender writes the log to the file.
(8) Memoryappender logs the log to the memory buffer.
(9) Netsendappender the log output to the Windows Messenger Service, which is displayed in the User Terminal's dialog box.
Remotesyslogappender logs are written to the remote Syslog service via the UDP network protocol.
(one) Remotingappender writes logs to the remote receive end through. NET Remoting.
Rollingfileappender writes the log to a file in the form of a rollback file.
Smtpappender writes the log to the message.
Traceappender writes the log to the. NET trace System.
(udpappender) Send the log connectionless UDP datagrams to the remote host or broadcast in the form of udpclient.
Filter:
Appender is the output of the log as a default value, meaning that it is output by default. Filter means filters, using filter to output the log in different formats.
Layout:
This is used to control the log output display format.
Object Render:
Used to output logs according to user-defined criteria.
Log4net structure diagram (from other places)
Log4net's Log Object Manager Logmanager:logmanager is used to manage all logger objects, GetLogger () can be used to find logger objects that already exist, and if the object does not exist it will automatically create a logger object. and manage it.
Four. Log4net Log level
The log4net is divided into the following levels:
FATAL Destruction Level
Error level
WARN Warning Level
INFO Message Level
Debug Debugging Level
These log levels are high and low:fatal> error> WARN > INFO > DEBUG.
Only the log output level is greater than or equal to the configured level to output log information.
For example, my log configuration level is info, then log only. Info (), log. Warn (), log. Error (), log. Fatal () to output the log information, the debug mode can not be output.
There are two special levels of configuration in log4net: All-Allow all log level output, OFF-Deny all log level output.
Five. Log4net Configuration Instructions
<configSections>
<section name= "log4net" type= "log4net. Config.log4netconfigurationsectionhandler,log4net "/>
</configSections>
<log4net>
<!--log to database--
<appender name= "Adonetappender" type= "log4net. Appender.adonetappender ">
<buffersize value= "/>"
<connectiontype value= "System.Data.SqlClient.SqlConnection, System.Data, version=1.0.3300.0, Culture=neutral, publickeytoken=b77a5c561934e089 "/>
<connectionstring value= "Data source=gas-hechen;initial catalog=dsc141051_db;integrated security=false;persist Security info=true; User Id=sa; password=000000 "/>
<commandtext value= "INSERT into Log ([date],[thread],[level],[logger],[message],[exception]) VALUES (@log_date, @ Thread, @log_level, @logger, @message, @exception) "/>
<parameter>
<parametername value= "@log_date"/>
<dbtype value= "DateTime"/>
<layout type= "log4net. Layout.rawtimestamplayout "/>
</parameter>
<parameter>
<parametername value= "@thread"/>
<dbtype value= "String"/>
<size value= "255"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%thread"/>
</layout>
</parameter>
<parameter>
<parametername value= "@log_level"/>
<dbtype value= "String"/>
<size value= "/>"
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%level"/>
</layout>
</parameter>
<parameter>
<parametername value= "@logger"/>
<dbtype value= "String"/>
<size value= "255"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%logger"/>
</layout>
</parameter>
<parameter>
<parametername value= "@message"/>
<dbtype value= "String"/>
<size value= "4000"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%message"/>
</layout>
</parameter>
<parameter>
<parametername value= "@exception"/>
<dbtype value= "String"/>
<size value= "/>"
<layout type= "log4net. Layout.exceptionlayout "/>
</parameter>
</appender>
<!--input Log to console--
<appender name= "Consoleappender" type= "log4net. Appender.consoleappender ">
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "[Time]:%d%n[level]:%p%n[content]:%m%n%n" ></conversionPattern>
</layout>
</appender>
<appender name= "Fileappender" type= "log4net. Appender.fileappender ">
<param name= "File" value= "D:\a.log" ></param>
<param name= "Appendtofile" value= "true" ></param>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "[Time]:%d%n[level]:%p%n[content]:%m%n%n" ></conversionPattern>
</layout>
</appender>
<root>
<level value= "INFO" ></level>
<appender-ref ref= "Fileappender" ></appender-ref>
<appender-ref ref= "Consoleappender" ></appender-ref>
</root>
<logger name= "MyLogger" >
<level value= "INFO" ></level>
<appender-ref ref= "Fileappender" ></appender-ref>
<appender-ref ref= "Consoleappender" ></appender-ref>
</logger>
</log4net>
This configuration file defines three different appender, namely, ADO log to data, Consoleappender from console output, fileappender log information file output.
However, log4net configuration of the attachment is far more than these, here to upload a more complete attachment configuration file for your reference click to download.
The following code tests are still used according to the log configuration file above:
It needs attention here, log4net. Config.XmlConfigurator.Configure (); This is equivalent to initializing the log4net, if it is in the Web program, we can add this to the Global.asax file Application_Start (object sender, EventArgs e) method, Initializes the configuration information when the program is started.
The above program runs the result diagram, and the console outputs the result:
The information is logged to the specified disk file, and the results are recorded as:
Two configurations are described here:
Here the root and logger configuration nodes, root is equivalent to the default log object configuration, and logger is to specify a specific log configuration. They can coexist.
For each of these two nodes, there are levels and APPENDER-REF nodes that specify the output level of the log and which attachments are referenced. Each log object can use multiple Appender, where the configuration uses both the file output and the console output. The resulting result is the above two result graphs.
The following logger object adds the Adonetappender attachment, which is used to log logs in three different ways when using MyLogger log objects.
The above program declares that two log objects, log and Log1,log, are present at the time of initialization, resulting in <logger name= "MyLogger" > This log object, and Log1 is a log object that does not exist in Logmanager. Here it will re-create one and be managed by Logmanager, then use this already existing object again after use. Here are two log objects that use the results as follows:
Now I modify the log file output level of the configuration file:
<logger name= "MyLogger" >
<level value= "WARN" ></level>
<appender-ref ref= "Fileappender" ></appender-ref>
<appender-ref ref= "Consoleappender" ></appender-ref>
<appender-ref ref= "Adonetappender" ></appender-ref>
</logger>
Results from running
Obviously the MyLogger object log does not have output log information.
Six. Log4net parameter description
The configuration files we see above have symbols like%m%n and so on that have special meanings.
%m[%message]: Output of log messages
%n: Line break
%d[%datetime]: Outputs the moment at which the current statement runs
%r: The number of milliseconds that the output program consumes from running to executing to the current statement
%d: The thread ID where the current statement is located
%p: The current priority level of the log
%c: Name of the current log object
%l: The line number where the output statement is located
%F: The file name where the output statement resides
%-Number: Indicates the minimum length of the item and, if not enough, fills it with a space
Log4net log records simple to use explanation