[Translation] How to Use log4net for logging

Source: Internet
Author: User
Tags log4net
Introduction

Log4net is an open-source component that enables. net programs to record logs and output them in various formats.

Use Code

Log4net provides a simple way to use powerful log functions. The steps are as follows:
1. obtain the latest log4net component and add the reference to the program.
2. Add the following lines to your assemblyinfo. CS file.

[Assembly: log4net. config. xmlconfigurator (configfile = "Web. config", watch = true)] // For log4net 1.2.10.0
The preceding statement provides a configuration file for configuring log4net parameters.

3. Add the following nodes to Web. config.

<Configsections>
<Section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net"/>
</Configsections>
<Log4net DEBUG = "true">
<Appender name = "rollinglogfileappender" type = "log4net. appender. rollingfileappender">
<File value = "C :\\ testproj \ testlog.txt"/>
<Appendtofile value = "true"/>
<Rollingstyle value = "size"/>
<Maxsizerollbackups value = "10"/>
<Maximumfilesize value = "10 MB"/>
<Staticlogfilename value = "true"/>
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "%-5 p % d % 5rms %-22.22c {1} %-18.18 M-% m % N"/>
</Layout>
</Appender>
<Root>
<Level value = "debug"/>
<Appender-ref = "rollinglogfileappender"/>
</Root>
</Log4net>

The above node defines the configuration record Log Parameters

Rollinglogfileappender describes how to record logs. This means that logs will be written in a file and will be automatically added when the file is full. There are other available ways to save logs.

Layout is responsible for formatting log requests. However, the definition of the log storage path is related to the log output format. The preceding layout output format is as follows:

20:26:04, 033 [1736] Error utility [paystub]-cocould not find a part of the path
"C: \ Inetpub \ wwwroot \ testproj \ template \ paystub. xml"

4. If you want to add your diagnosis information to log4net, add the following code to the Web. config file:

<Deleetask>
<Add key = "log4net. Internal. debug" value = "true"/>
</Appsettings>

Run the following code on the system. Web node:

<System. Diagnostics>
<Trace autoflush = "true">
<Listeners>
<Add name = "textwritertracelistener"
Type = "system. Diagnostics. textwritertracelistener"
Initializedata = "C :\\ testproj \ testprojlog4net.txt"/>
</Listeners>
</Trace>
</System. Diagnostics>

5. perform the following steps on the code page:

A. Add a namespace Using log4net;

B. Add a declaration with a Class Definition Private Static readonly ilog log = logmanager. getlogger (typeof (testpage1). Name );

Or

Private Static readonly ilog log = logmanager. getlogger (
System. reflection. methodbase. getcurrentmethod (). declaringtype );

6. Now you can use the following statement to record logs:

If (log. iserrorenabled)
{
Log. Error ("Page load failed:" + ex. Message );
}

Or If (log. isdebugenabled)
{
Log. debug ("application loaded successfully .");
}

Now we have access to log4net, and log4net has many features available. Created by jecray.

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.