The app. config should be placed in the root directory of the solution. The specific process is as follows:
The first step: you should download the Log4net.dll and introduce it to your project, download the attached
Second step: need to configure the relevant configuration file app. config or Web. config, as the name implies for normal application configuration files
The configuration file for App.config,asp.net is Web. config. The configuration for log is as follows:
<configuration>
<configSections>
<!--definition of log4net
<section name= "log4net" type= "log4net. Config.log4netconfigurationsectionhandler, log4net "/>
</configSections>
<log4net>
<!--console section log output format settings--
<appender name= "Consoleappender" type= "log4net. Appender.consoleappender ">
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%date [%thread]%-5level%logger%ndc-%message%newline"/>
</layout>
</appender>
<!--log file part of the output format setting--
<appender name= "Rollinglogfileappender" type= "log4net. Appender.rollingfileappender ">
<file value= "Specific log file path name"/>
<appendtofile value= "true"/>
<maxsizerollbackups value= "Ten"/>
<maximumfilesize value= "1MB"/>
<rollingstyle value= "Size"/>
<staticlogfilename value= "true"/>
<layout type= "log4net. Layout.patternlayout ">
<footer value= "[footer]& #13;& #10;"/>
<conversionpattern value= "%date [%thread]%-5level%logger [%NDC]-%message%newline"/>
</layout>
</appender>
<!--Setup The root category, add the Appenders and set the default level--
<root>
<level value= "All"/>
<appender-ref ref= "Consoleappender"/>
<appender-ref ref= "Rollinglogfileappender"/>
</root>
</log4net>
</configuration>
The third step: the project AssemblyInfo.cs inside add [assembly:log4net. Config.xmlconfigurator ()], so project engineering and Log4net
An association was established. "Where app. config should be placed in the root directory of the solution!" 】
If you do not place the directory, you should use this method: [Assembly:log4net. Config.xmlconfigurator (configfileextension[email protected]"C:/app.config", Watch=true)]
Fourth step: Write the log in the program
The following example instantiates Ilog first:
private static readonly log4net. ILog log = log4net. Logmanager.getlogger (System.Reflection.MethodBase.GetCurrentMethod (). DeclaringType);
Then write the custom message in the Application_Error method:
protected void Application_Error (Object sender, EventArgs e)
{
Log. Info ("/r/n Client IP:" + request.userhostaddress + "/r/n<br> Error Address:" + Request.url,
Server.GetLastError ());
}
Log4net usage in class libraries