ASP. NET use log4net log component tutorial (one log is generated every day and the log is cut by size)

Source: Internet
Author: User
Tags log4net

Logs are a required tool for a large website or system. You can use logs to clearly understand the running status of the program and get feedback in a timely manner to solve the problem. asp is introduced below. the log4net log component of the Net version is a very powerful tool, the latest version. net2.0 1.2.10.0
After a day, I finally learned how to use it. I just recorded it.
The following describes how to use a separate configuration file for log4net.
Start action:
Step 1: reference the log4net. dll file in the project
Step 2:
Add the following code to the Web. config file:

<configuration>    <configSections>      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />    </configSections></configuration>
Step 3:
Create a separate configuration file named log4net. config
(This configuration file will generate a log file every day under the app_data directory, for example, logs_20090703.txt)
<log4net>
<!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
<!-- Set root logger level to ERROR and its appenders -->
<root>
<level value="ALL"/>
<appender-ref ref="SysAppender"/>
</root>

<!-- Print only messages of level DEBUG or above in the packages -->
<logger name="WebLogger">
<level value="DEBUG"/>
</logger>

<appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="App_Data/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value="&#13;&#10;----------------------header--------------------------&#13;&#10;" />
<param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" />
</layout>
</appender>
<appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
</log4net>
Step 4: (this step is critical)
Add a code to the assemblyinfo. CS file. The path of assemblyinfo. CS is located in the properties directory.
The added code is as follows:
// Log component configuration
[assembly: log4net.Config.DOMConfigurator(ConfigFile = "log4net.config", Watch = true)]

Step 5: start using the. CS code of log4net
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 a normal message ");

This is simple.
The following describes how to automatically cut multiple files by size in a configuration file.
The separate configuration file (by size) of log4net. config is as follows:
 

<Log4net>
<! -- Off, fatal, error, warn, info, debug, all -->
<! -- Set root logger level to Error and Its appenders -->
<Root>
<Level value = "all"/>
<Appender-ref = "sysappender"/>
</Root>

<! -- Print only messages of level debug or abve in the packages -->
<Logger name = "weblogger">
<Level value = "debug"/>
</Logger>

<Appender name = "sysappender" type = "log4net. appender. rollingfileappender">
<Param name = "file" value = "app_data \ logs.txt"/>
<Param name = "appendtofile" value = "true"/>
<Param name = "maxsizerollbackups" value = "12"/> <! -- Cut the maximum number of files -->
<Param name = "maximumfilesize" value = "500kb"/> <! -- Size of each file -->
<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>
</Log4net>

Next, let's take a look at what I used on my server:

 
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.