Log4net usage in class libraries

Source: Internet
Author: User
Tags log4net

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

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.