Dummies Tutorial: Asp.net (C #) How to Use log4net

Source: Internet
Author: User

I'm a Dummies. Today I want to talk about how to use log4net, of course. Under. net.

First, make sure that you have downloaded the log4net. dll file andProgramIn the bin folder, otherwise ......

 

1: In web. config<Configsections>... </configsections>Add:

<Section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net"/>

 

2:<Configuration>... </configuration>Add:

<Log4net DEBUG = "false">

<Appender name = "logfileappender" type = "log4net. appender. fileappender">
<Param name = "file" value = "D: \ applog.txt"/>
<Param name = "datepattern" value = "yyyy-mm-dd hh: mm"/>
<Param name = "appendtofile" value = "true"/>
<Layout type = "log4net. layout. patternlayout">
<Param name = "conversionpattern" value = "" % d % T % P % L % m % N "/>
</Layout>
</Appender>

<Appender name = "httptraceappender" type = "log4net. appender. aspnettraceappender">
<Layout type = "log4net. layout. patternlayout">
<Param name = "conversionpattern" value = "% d [% T] %-5 p % C [% x]-% m % N"/>
</Layout>
</Appender>

<Appender name = "eventlogappender" type = "log4net. appender. eventlogappender">
<Layout type = "log4net. layout. patternlayout">
<Param name = "conversionpattern" value = "% d [% T] %-5 p % C [% x]-% m % N"/>
</Layout>
</Appender>

<Appender name = "rollinglogfileappender" type = "log4net. appender. rollingfileappender">
<Param name = "file" value = "D: \ log.txt"/>
<Param name = "appendtofile" value = "true"/>
<Param name = "maxsizerollbackups" value = "10"/>
<Param name = "maximumfilesize" value = "5 MB"/>
<Param name = "rollingstyle" value = "size"/>
<Param name = "staticlogfilename" value = "true"/>
<Layout type = "log4net. layout. patternlayout">
<Param name = "conversionpattern" value = "% d [% T] %-5 p % C [% x]-% m % N"/>
</Layout>
</Appender>

<Root>
<Level value = "debug"/>
<Appender-ref = "logfileappender"/>
</Root>

</Log4net>

 

3: Finally, define a log object in the Aspx. CS or CS class to record the log:

Private Static log4net. ilog log = log4net. logmanager. getlogger ("debug ");

 

4: call:

InCodeBlock, add this line, OK!

Log. debug ("XX operation recorded");

 

5:Finally, I will explain "% d % T % P % L % m % N" in Web. config:

% D:The date or time of the log output time point,You can specify a format later, for example, % d {yyyy-mm-dd hh: mm: SS}. The output is similar to: 17:49:27, which is suitable for inserting sqlserver;

% T:The thread name that generates the log event;

% P: log_level of the log, such as debug, warn, Or info;

% C: the output category, usually the full name of the class, such as "iNotes. Default ";

% M: log content;

% L: the location where the output log event occurs, including the category name, the thread that occurs, and the number of lines in the code.

% N output a carriage return line break, wiThe ndows platform is "\ r \ n" and the UNIX platform is "\ n"

 

If you still don't know much about it, use the format % d % T % P % L % m % N. I set it so clearly.

 

Dummies: Blue breeze QQ: 3053166

 

 

Related Article

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.