Asp. NET using the Log4net Log component tutorial (cut by date and by log size)

Source: Internet
Author: User
Tags log4net

For a large web site and system, log is a necessary tool, through the log you can very clear the operation of the program, timely feedback to solve the problem, the following introduction of the ASP. NET version of the Log4net log component is a very powerful tool, the latest version for. net2.0 1.2.10.0

I had a day, finally know how to use, simple record.

The methods described below are log4net using a separate configuration file.

Start Action:

First step: Referencing the Log4net.dll file in the project

Step Two:

Add a code to the Web. config file in the following location:

<configuration>
<configSections>
<section name= "log4net" type= "log4net. Config.log4netconfigurationsectionhandler,log4net "/>
</configSections>
</configuration>

Step Three:

Create a separate configuration file with the file name: Log4net.config

(This configuration file will generate a log file per day by date in the App_Data directory, such as: Logs_20090703.txt)

Log4net.config Configuration file Contents:

Log4net.config Code[http://www.xueit.com]<?XML version= "1.0" encoding= "Utf-8"?><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= "&quot; Logs_&quot;yyyymmdd&quot;. txt&quot; "/><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>

Fourth step: (This step is critical)

Add a code to the file AssemblyInfo.cs, the path to the AssemblyInfo.cs is located in the properties directory

Add the following code:

Log Component Configuration
[Assembly:log4net. Config.domconfigurator (configfile = "Log4net.config", Watch = True)]

Fifth Step : Start using the log4net. CS Code

First introduce:

Using Log4net;

Code:

Object o = ConfigurationSettings.GetConfig ("log4net");
Log4net. Config.DOMConfigurator.Configure (o as System.Xml.XmlElement);

Log4net. ILog logger = log4net. Logmanager.getlogger ("Weblogger");
Logger. Debug ("Debug");
Logger. Error ("This is an error log");
Logger. Fatal ("This is a fatal error log");
Logger. Warn ("This is a warning log");
Logger. Info ("This is an ordinary message");

It's so easy to finish.

The following is how the configuration file automatically cuts multiple files by size.

Log4net.config the contents of individual profiles (by size) are as follows:

Log4net.config Code[http://www.xueit.com]<?XML version= "1.0" encoding= "Utf-8"?><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 "><paramname= "File"value= "App_datalogs.txt"/><paramname= "Appendtofile"value= "true"/><paramname= "Maxsizerollbackups"value= " a"/> <!--cutting the maximum number of files -<paramname= "maximumFileSize"value= "500KB"/> <!--the size of each file -<paramname= "Rollingstyle"value= "Size"/><paramname= "Staticlogfilename"value= "true"/><Layouttype= "log4net." Layout.patternlayout "><paramname= "Conversionpattern"value= "%d [%t]%-5p%c [%x]-%m%n"/></Layout></Appender></log4net>

Let's look at what I'm using on my server:

Log4net Generating logs

Dodo

Original: http://www.xueit.com/html/2009-12-18/21-1907360304171.html

This article is copyrighted by the author and the school IT Network, welcome reprint, but without the consent of the author must retain this paragraph of the statement, and retain the original link. Otherwise, the right to pursue legal liability is retained.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.