The Web. config configuration is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net"/>
</Configsections>
<Log4net>
<! -- Error Log configuration -->
<Appender name = "errorappender" type = "log4net. appender. rollingfileappender">
<Param name = "file" value = "log \ logerror \"/>
<Param name = "appendtofile" value = "true"/>
<Param name = "maxsizerollbackups" value = "100"/>
<Param name = "maxfilesize" value = "10240"/>
<Param name = "staticlogfilename" value = "false"/>
<Param name = "datepattern" value = "yyyymmdd"/>
<Param name = "rollingstyle" value = "date"/>
<Layout type = "log4net. layout. patternlayout">
<Param name = "conversionpattern" value = "% N exception time: % d [% T] % N exception level: %-5 p % N exception class: % C [% x] % N % m % N "/>
</Layout>
</Appender>
<! -- Information log configuration -->
<Appender name = "infoappender" type = "log4net. appender. rollingfileappender">
<Param name = "file" value = "log \ loginfo \"/>
<Param name = "appendtofile" value = "true"/>
<Param name = "maxfilesize" value = "10240"/>
<Param name = "maxsizerollbackups" value = "100"/>
<Param name = "staticlogfilename" value = "false"/>
<Param name = "datepattern" value = "yyyymmdd"/>
<Param name = "rollingstyle" value = "date"/>
<Layout type = "log4net. layout. patternlayout">
<Param name = "conversionpattern" value = "% n log time: % d [% T] % n log level: %-5 p % n log class: % C [% x] % N % m % N "/>
</Layout>
</Appender>
<! -- Console -->
<Appender name = "leleappender" type = "log4net. appender. consoleappender">
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% 5 level [% thread] (% file: % line)-% message % newline"/>
</Layout>
</Appender>
<! -- Log4net. logmanager. getlogger ("logerror") use this to select this type -->
<Logger name = "logerror">
<Level value = "error"/>
<Appender-ref = "errorappender"/>
</Logger>
<Logger name = "loginfo">
<Level value = "info"/>
<Appender-ref = "infoappender"/>
</Logger>
<Root>
<Level value = "info"/>
<Appender-ref = "infoappender"/>
<Appender-ref = "leleappender"/>
</Root>
</Log4net>
</Configuration>
Log operations
Using system;
Using system. IO;
/** // <Summary>
/// Loghelper summary.
/// </Summary>
Public class loghelper
{
Private systemlog ()
{
}
Public static readonly log4net. ilog loginfo = log4net. logmanager. getlogger ("loginfo"); // select <logger name = "loginfo">
Public static readonly log4net. ilog logerror = log4net. logmanager. getlogger ("logerror"); // select the configuration of <logger name = "logerror">
Public static void setconfig ()
{
Log4net. config. xmlconfigurator. Configure ();
}
Public static void setconfig (fileinfo configfile)
{
Log4net. config. xmlconfigurator. Configure (configfile );
}
Public static void writelog (string info)
{
If (loginfo. isinfoenabled)
{
Loginfo. Info (Info );
}
}
Public static void writelog (string info, exception SE)
{
If (logerror. iserrorenabled)
{
Logerror. Error (Info, SE );
}
}
}
The configuration of the global. asax. CS file is as follows:
Protected void application_start (Object sender, eventargs E)
{
Systemlog. setconfig ();
}
Protected void application_error (Object sender, eventargs E)
{
Exception objexp = httpcontext. Current. server. getlasterror ();
Loghelper. writelog ("\ r \ n Client IP:" + request. userhostaddress + "\ r \ n error address:" + request. URL + "\ r \ n exception information:" + server. getlasterror (). message, objexp );
}
Protected void application_start (Object sender, eventargs E)
{
Systemlog. setconfig ();
}
Protected void application_error (Object sender, eventargs E)
{
Exception objexp = httpcontext. Current. server. getlasterror ();
Loghelper. writelog ("\ r \ n Client IP:" + request. userhostaddress + "\ r \ n error address:" + request. URL + "\ r \ n exception information:" + server. getlasterror (). message, objexp );
}
Eg:
Try
{}
Catch (exception ex)
{
Loghelper. writelog ("errorinfo" EX );
}
Above this article from the csdn blog, reprint please indicate the source: http://blog.csdn.net/javc/archive/2009/03/25/4022677.aspx
1. Call steps
1. config Configuration
2. initialize log4net. config. xmlconfigurator. Configure ();
3. Call logger. Info ("asdfasd ");
2. Details
1. The log4net Framework defines a class called logmanager to manage all the logger objects. It has a static getlogger () method that uses the name parameters we provide to retrieve existing logger objects. If this logger object does not exist in the framework, it will also create a logger object for us.CodeAs follows:
Log4net. ilog log = log4net. logmanager. getlogger ("logger-name ");
Generally, we call getlogger () with the type of the class as the parameter to track the classes that we are logging. The passed class type can be obtained using the typeof (classname) method, or the following reflection method can be used:
Log4net. ilog log = log4net. logmanager. getlogger (System. reflection. methodbase. getcurrentmethod (). declaringtype);
Although the symbol is longer, the latter can be used for some occasions, such as obtaining the type of the class that calls the method ).
Example: Private void button#click (Object sender, eventargs E)
{
Logger. Info ("sdfasd ");
}
System. reflection. methodbase. getcurrentmethod (). declaringtype is the type of the class where the button#click method is located.
2. ConfigurationXmlconfigurator
Use code to initialize the configuration.
Log4net. config. xmlconfigurator. Configure (New fileinfo ("test. log4net "));
In addition to initializing the configuration, the xmlconfigurator. configureandwatch () method also monitors the configuration file changes. Once modified, the configuration is automatically refreshed.
We can also use xmlconfiguratorattribute to replace xmlconfigurator. config ()/configureandwatch (). configuratorattribute is used to define the configuration file name associated with assembly.
Method 1: Associate with test. log4net and monitor changes.
[Assembly: log4net. config. xmlconfigurator (configfile = "test. log4net", watch = true)]
Method 2: Associate with test.exe. log4net (or test. dll. log4net, the file name prefix is the currentProgramSet Name), and monitor changes.
[Assembly: log4net. config. xmlconfigurator (configfileextension = "log4net", watch = true)]
The Web. config configuration is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Configsections>
<Section name = "log4net" type = "log4net. config. log4netconfigurationsectionhandler, log4net"/>
</Configsections>
<Log4net>
<! -- Error Log configuration -->
<Appender name = "errorappender" type = "log4net. appender. rollingfileappender">
<Param name = "file" value = "log \ logerror \"/>
<Param name = "appendtofile" value = "true"/>
<Param name = "maxsizerollbackups" value = "100"/>
<Param name = "maxfilesize" value = "10240"/>
<Param name = "staticlogfilename" value = "false"/>
<Param name = "datepattern" value = "yyyymmdd"/>
<Param name = "rollingstyle" value = "date"/>
<Layout type = "log4net. layout. patternlayout">
<Param name = "conversionpattern" value = "% N exception time: % d [% T] % N exception level: %-5 p % N exception class: % C [% x] % N % m % N "/>
</Layout>
</Appender>
<! -- Information log configuration -->
<Appender name = "infoappender" type = "log4net. appender. rollingfileappender">
<Param name = "file" value = "log \ loginfo \"/>
<Param name = "appendtofile" value = "true"/>
<Param name = "maxfilesize" value = "10240"/>
<Param name = "maxsizerollbackups" value = "100"/>
<Param name = "staticlogfilename" value = "false"/>
<Param name = "datepattern" value = "yyyymmdd"/>
<Param name = "rollingstyle" value = "date"/>
<Layout type = "log4net. layout. patternlayout">
<Param name = "conversionpattern" value = "% n log time: % d [% T] % n log level: %-5 p % n log class: % C [% x] % N % m % N "/>
</Layout>
</Appender>
<! -- Console -->
<Appender name = "leleappender" type = "log4net. appender. consoleappender">
<Layout type = "log4net. layout. patternlayout">
<Conversionpattern value = "% 5 level [% thread] (% file: % line)-% message % newline"/>
</Layout>
</Appender>
<! -- Log4net. logmanager. getlogger ("logerror") use this to select this type -->
<Logger name = "logerror">
<Level value = "error"/>
<Appender-ref = "errorappender"/>
</Logger>
<Logger name = "loginfo">
<Level value = "info"/>
<Appender-ref = "infoappender"/>
</Logger>
<Root>
<Level value = "info"/>
<Appender-ref = "infoappender"/>
<Appender-ref = "leleappender"/>
</Root>
</Log4net>
</Configuration>
Log operations
Using system;
Using system. IO;
/** // <Summary>
/// Loghelper summary.
/// </Summary>
Public class loghelper
{
Private systemlog ()
{
}
Public static readonly log4net. ilog loginfo = log4net. logmanager. getlogger ("loginfo"); // select <logger name = "loginfo">
Public static readonly log4net. ilog logerror = log4net. logmanager. getlogger ("logerror"); // select the configuration of <logger name = "logerror">
Public static void setconfig ()
{
Log4net. config. xmlconfigurator. Configure ();
}
Public static void setconfig (fileinfo configfile)
{
Log4net. config. xmlconfigurator. Configure (configfile );
}
Public static void writelog (string info)
{
If (loginfo. isinfoenabled)
{
Loginfo. Info (Info );
}
}
Public static void writelog (string info, exception SE)
{
If (logerror. iserrorenabled)
{
Logerror. Error (Info, SE );
}
}
}
The configuration of the global. asax. CS file is as follows:
Protected void application_start (Object sender, eventargs E)
{
Systemlog. setconfig ();
}
Protected void application_error (Object sender, eventargs E)
{
Exception objexp = httpcontext. Current. server. getlasterror ();
Loghelper. writelog ("\ r \ n Client IP:" + request. userhostaddress + "\ r \ n error address:" + request. URL + "\ r \ n exception information:" + server. getlasterror (). message, objexp );
}
Protected void application_start (Object sender, eventargs E)
{
Systemlog. setconfig ();
}
Protected void application_error (Object sender, eventargs E)
{
Exception objexp = httpcontext. Current. server. getlasterror ();
Loghelper. writelog ("\ r \ n Client IP:" + request. userhostaddress + "\ r \ n error address:" + request. URL + "\ r \ n exception information:" + server. getlasterror (). message, objexp );
}
Eg:
Try
{}
Catch (exception ex)
{
Loghelper. writelog ("errorinfo" EX );
}
Above this article from the csdn blog, reprint please indicate the source: http://blog.csdn.net/javc/archive/2009/03/25/4022677.aspx
1. Call steps
1. config Configuration
2. initialize log4net. config. xmlconfigurator. Configure ();
3. Call logger. Info ("asdfasd ");
2. Details
1. The log4net Framework defines a class called logmanager to manage all the logger objects. It has a static getlogger () method that uses the name parameters we provide to retrieve existing logger objects. If this logger object does not exist in the framework, it will also create a logger object for us. The Code is as follows:
Log4net. ilog log = log4net. logmanager. getlogger ("logger-name ");
Generally, we call getlogger () with the type of the class as the parameter to track the classes that we are logging. The passed class type can be obtained using the typeof (classname) method, or the following reflection method can be used:
Log4net. ilog log = log4net. logmanager. getlogger (System. reflection. methodbase. getcurrentmethod (). declaringtype);
Although the symbol is longer, the latter can be used for some occasions, such as obtaining the type of the class that calls the method ).
Example: Private void button#click (Object sender, eventargs E)
{
Logger. Info ("sdfasd ");
}
System. reflection. methodbase. getcurrentmethod (). declaringtype is the type of the class where the button#click method is located.
2. ConfigurationXmlconfigurator
Use code to initialize the configuration.
Log4net. config. xmlconfigurator. Configure (New fileinfo ("test. log4net "));
In addition to initializing the configuration, the xmlconfigurator. configureandwatch () method also monitors the configuration file changes. Once modified, the configuration is automatically refreshed.
We can also use xmlconfiguratorattribute to replace xmlconfigurator. config ()/configureandwatch (). configuratorattribute is used to define the configuration file name associated with assembly.
Method 1: Associate with test. log4net and monitor changes.
[Assembly: log4net. config. xmlconfigurator (configfile = "test. log4net", watch = true)]
Method 2: Associate with test.exe. log4net (or test. dll. log4net, with the prefix of the file name as the current Assembly name) and monitor changes.
[Assembly: log4net. config. xmlconfigurator (configfileextension = "log4net", watch = true)]