Use log4net notes in Asp.net)

Source: Internet
Author: User
Log4net (log for net) is an open-source application of Apache.. NET Framework logging tool. For more information, see the Apache website. it is a sister tool for Java log4j (log for Java. all log4j users know that it has powerful functions, flexible configuration, thread security, and convenient log output management and level management. For more information, see.

First, you should download log4net. dll and introduce it to your project references, orSource codeAdd a project to your project as part of your project. Click here to download log4net.

To use log4net, you need to know two important concepts: logger and appender. The former is the log record object, and the latter is the log record object, including the console, files, quantitative files, and remote broadcast. That is to say, the process of using log4net can be like this: Get a tool logger for logging, and then specify the log record location for the logger object. what's wonderful is that all this work can be done on the web. config configuration file.CodeVery few.
The configuration is as follows:

Add the following configuration under the <configuration> node in Web. config:

< Configsections >
< Section Name = "Log4net" Type = "Log4net. config. log4netconfigurationsectionhandler, log4net" />
</ Configsections >

Anyone who has used a web. config custom node knows that the log4net node should also be configured. Next, add the following log4net node configuration under the <configuration> node:

1 < Log4net Debug = "False" >
2 < Appender Name = "Logfileappender" Type = "Log4net. appender. fileappender" >
3 < Param Name = "File" Value = "C: \ applog.txt" />
4 < Param Name = "Datepattern" Value = "Yyyy-mm-dd hh: mm" />
5 < Param Name = "Appendtofile" Value = "True" />
6 < Layout Type = "Log4net. layout. patternlayout" >
7 < Param Name = "Conversionpattern" Value = "% D [% T] %-5 p % C [% x]-% m % N" />
8 </ Layout >
9 </ Appender >
10 < Appender Name = "Httptraceappender" Type = "Log4net. appender. aspnettraceappender" >
11 < Layout Type = "Log4net. layout. patternlayout" >
12 < Param Name = "Conversionpattern" Value = "% D [% T] %-5 p % C [% x]-% m % N" />
13 </ Layout >
14 </ Appender >
15 < Appender Name = "Eventlogappender" Type = "Log4net. appender. eventlogappender" >
16 < Layout Type = "Log4net. layout. patternlayout" >
17 < Param Name = "Conversionpattern" Value = "% D [% T] %-5 p % C [% x]-% m % N" />
18 </ Layout >
19 </ Appender >
20 < Appender Name = "Rollinglogfileappender" Type = "Log4net. appender. rollingfileappender" >
21 < Param Name = "File" Value = "C: \ log.txt" />
22 < Param Name = "Appendtofile" Value = "True" />
23 < Param Name = "Maxsizerollbackups" Value = "10" />
24 < Param Name = "Maximumfilesize" Value = "5 MB" />
25 < Param Name = "Rollingstyle" Value = "Size" />
26 < Param Name = "Staticlogfilename" Value = "True" />
27 < Layout Type = "Log4net. layout. patternlayout" >
28 < Param Name = "Conversionpattern" Value = "% D [% T] %-5 p % C [% x]-% m % N" />
29 </ Layout >
30 </ Appender >
31 < Root >
32 < Level Value = "Debug" />
33 < Appender-ref Ref = "Logfileappender" />
34 </ Root >
35 </ Log4net >

From the configuration above, we can see that multiple appender are defined, and each appender is the target of a log record. The root node specifies the appender to be selected. Here I select logfileappender.

The configuration is complete here, but how can I use the above configuration? Log4net provides usProgramThe entry for loading configuration information at startup is very simple. Add the following code in the application_start process of global. asax. CS:
Log4net. config. domconfigurator. Configure ();
Now the configuration process has been completed, and the following is a question about how to call it.

Create a log4nettest. ASPX page, add reference using log4net to the CS file, and define a global variable on this page:
Private Static readonly ilog log = logmanager. getlogger (typeof (log4nettest ));

Then add the following code to page_load: log. debug ("this is my use of log4net"); run this page, under c: \, an applog.txt text file will be generated. Open it and you will see the logs you have written.

From: http://www.cnblogs.com/fineboy/archive/2005/08/15/215478.html

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.