I. Characteristics of log4net
Log4net is a logging component for the. NET development environment and is used by many large applications due to its ultra-fast and ultra-flexible.
It has the following characteristics:
1. Custom log output levels
Log4net divides the log into five levels of priority from high to Low: FATAL > ERROR > WARN > INFO > DEBUG, plus all (allow all log requests) and off (deny all log requests) to both of these special levels. Log levels can be used to control whether the log is output. If you define an info level in your profile, there are three ways to log records in your program
Log.info ("Log Contents"), Log.warn ("Log Contents"), Log.debug ("Log Contents")
The log.info,log.debug will output the log, and Log.warn will not output the log.
2. Custom log Output mode
Log4net's log output is flexible enough to output logs to a text file, console, mail, Windows Event log, database, and so on.
The following are very flexible configurations for writing logs to a text file.
A. Print logs by time period, by month, by day, on time, by minute, etc with your configuration
B. Print the scroll log according to the size of the file, such as each 10m,100m a log file, so as to prevent the log file is not too large, the log file reached 4,5g later we are basically difficult to open.
C. Specify the log size, for example, specify 500M, then the log will be only one and will not exceed 500M.
3. Ultra-fast Performance
Log4net logging performance is about 4 times times the StringWriter logging method.
Second, log4net in the project application
1. On Log4net's official website http://logging.apache.org/log4net/download a log4net.dll and then add a reference to the project.
2. Configuration
There are two configurations for A.log4net, one independent to one configuration file and one in the configuration file of the program itself (such as the Web.config,winform appsetting of a Web project), but these two configurations are similar to each other.
B. Configure the steps, add the following line under the <configSections> node in the configuration file
<section name= "log4net" type= "log4net. Config.log4netconfigurationsectionhandler, log4net "/>
C. Add the actual work configuration under the <configSections> node.
For example:
<log4net debug= "false" >
<appender name= "My appender" type= "log4net. Appender.rollingfileappender ">
<file value= "D:\Vehpro\MyLogger.log"/>
<appendtofile value= "true"/>
<rollingstyle value= "Date"/>
<datepattern value= "Yyyymmdd" log" "/>
<layout type= "log4net. Layout.patternlayout ">
<param name= "Conversionpattern" value= "%d [%t]%-5p%c%m%n"/>
</layout>
</appender>
<logger name= "MyLogger" additivity= "false" >
<level value= "All"/>
<appender-ref ref= "My appender"/>
</logger>
<root>
<level value= "All"/>
<appender-ref ref= "My appender"/>
</root>
</log4net>
defines two logger in this configuration one is root and the other is mylogger. They all output logs by appender-ref The specified my Appender media.
D. Activate log4net
for Web engineering, if isolated in one configuration file, assume that the profile name is Log4net.config.xml
Add the following two lines to the Global.asax Application_Start
System.IO.FileInfo file = new System.IO.FileInfo (Server.MapPath ("~\\log4net.config.xml"));
log4net. Config.XmlConfigurator.ConfigureAndWatch (file);
if configured in Web. config, replace the code with log4net.Config.XmlConfigurator.Configure ();
For applications, winform,windows services, etc. can be added to the AssemblyInfo
[Assembly:log4net. Config.xmlconfigurator (Watch = True)].
3. On the battlefield
Add a field to the class log as follows
private static log4net. ILog log = log4net. Logmanager.getlogger ("MyLogger");
The parameter MyLogger in the GetLogger method body is the logger name defined in the configuration file, which means I want to use MyLogger configuration for logging.
Next, you can call the log method in your code to output the logs.
The first blog, found that writing a little bit of text is really not an easy thing. Want to write good or not write well, want to write clear not write clear hehe, welcome to shoot bricks ....
Log4net Use guide, the most frequently encountered problem collation ...