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 -->