. Net: how to use the framework log4net. dll

Source: Internet
Author: User
Tags log4net

First configure the web. config file
1. Add the following nodes
<! -- Log4net configuration BEGIN -->
<ConfigSections>
<Section name = "log4net" type = "log4net. Config. Log4NetConfigurationSectionHandler, log4net"/>
</ConfigSections>

<Log4net debug = "true">
<Appender name = "LogFileAppender" type = "log4net. Appender. FileAppender">
<Param name = "File" value = "SysLog/ErrorLog.txt"/>
<Layout type = "log4net. Layout. PatternLayout">
<Param name = "ConversionPattern" value = "% d [% t] %-5 p % c % m % n"/>
</Layout>
</Appender>

<Logger name = "File">
<Level value = "All"/>
<Appender-ref = "LogFileAppender"/>
</Logger>
</Log4net>
<! -- Log4net configure END -->

2. configure global. asax

Void Application_Start (object sender, EventArgs e)
{
// Register log Configuration
Log4net. Config. XmlConfigurator. Configure ();

}
Void Application_Error (object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
NetFan. Core. Logger. Log. Error (e. ToString ());
}

3. Create a global Logger class

/// <Summary>
/// Class, Event Log class.
/// Single-State encapsulation log4net
/// </Summary>
Public class Logger
{
Private static log4net. ILog log = null;
Private static object lockHelper = new object ();

Public static log4net. ILog Log
{
Get
{
If (log = null)
Lock (lockHelper)
If (log = null)
Log = log4net. LogManager. GetLogger ("File ");
Return log;
}
}
}

4. After the above OK, you can use it.
The method is:

Try
{
// Process Information
}

Catch (Exception ex)
{
// Run the log after an error occurs. If the IP address is 127.0.0.1, you do not need to write logs.
Logger. Log. Error ("Error from:" + Request. UserHostAddress, ex );

}
Company project Configuration:

<Configuration>
<ConfigSections>
<Section name = "SSO" type = "SXT. SSO. Client. SSOEntry, SXT. SSO. Client"/>
<Section name = "log4net" type = "log4net. Config. Log4NetConfigurationSectionHandler, log4net"/>

......

<! -- Log4net configuration BEGIN -->
<Log4net debug = "true">
<Appender name = "LogFileAppender" type = "log4net. Appender. RollingFileAppender">
<File value = "../Log/RES"/>
<AppendToFile value = "true"/>
<Rollingstyle value = "date"/>
<DatePattern value = "yyyy-MM-dd'. log'"/>
<StaticLogFileName value = "false"/>
<MaxSizeRollBackups value = "100"/>
<Layout type = "log4net. Layout. PatternLayout">
<Param name = "ConversionPattern" value = "record time: % date thread ID: [% thread] Log level: %-5 level error Description: % message % newline"/>
</Layout>
</Appender>
<Appender name = "leleappender" type = "log4net. Appender. ConsoleAppender">
<Layout type = "log4net. Layout. PatternLayout">
<ConversionPattern value = "% date [% thread] %-5 level % logger [% property {NDC}]-% message % newline"/>
</Layout>
</Appender>
<Root>
<Level value = "debug"/>
<Appender-ref = "LogFileAppender"/>
<! -- <Appender-ref = "leleappender"/> -->
</Root>
</Log4net>
<! -- Log4net configure END -->

 

 

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.