Remember groceries-log4net components use

Source: Internet
Author: User
Tags log4net

Let me introduce you to the groceries-log4net component use, when the program published on the server, sometimes error occurred, do not know that, so the log4net component is a good solution to the problem.

1, add the development package, and the reference to Log4net.dll

2. Add configuration in Web. config (or app. config)

<configSections><!--must be before all nodes--

<section name= "log4net" type= "log4net. Config.log4netconfigurationsectionhandler,log4net "/>

</configSections>

3. New Log4net Node

<log4net>

<root>

<!--log level off (off), Fatal,error,warn,info,debug,all (Show All)-

<level value= "All"/>

<appender-ref ref= "Rollingfiletracer"/>

</root>

<!--Print Only messages of level DEBUG or above in the packages-

<appender name= "Rollingfiletracer" type= "log4net. Appender.rollingfileappender,log4net "> <!--rollingfileappender means scrolling log information--

<param name= "File" value= "app_data/log/"/> <!--log information is saved--

<param name= "Appendtofile" value= "true"/> <!--is an additional file, true to append content to the original file--

<param name= "Rollingstyle" value= "Date"/> <!--files according to what to scroll to store, here is based on time--

<param name= "maxsizerollbackups" value= "x"/> <!--up to how many files, when larger than this number, will delete the first file--

<param name= "maximumFileSize" value= "1MB"/> <!--maximum size per file--

<param name= "Datepattern" value= "&quot; Logs_&quot;yyyymmdd&quot;. txt&quot; "/> <!--log file name format--

<param name= "Staticlogfilename" value= "false"/>

<layout type= "log4net. Layout.patternlayout,log4net ">

<param name= "Conversionpattern" value= "%d [%t]%-5p%c-%m%n"/> <!--output format--

</layout>

</appender>

</log4net>

Access levels can be controlled, the call can be set to all so that debugging information will be output to the log file, after the program is stable, set to warn so that debugging information will not be output to the log file, but also do not have to modify the code

4. Initialize: Write in global Application_Start (read config when program starts)

Log4net. Config.XmlConfigurator.Configure ();

Note : Be sure to join this, no plus although 8 will not error but will not have any effect

5. Record unhandled exception information at global Application_Error

Example: ILog ILog = Logmanager.getlogger (typeof (Global));

Ilog. Error ("The system has unhandled exception information", Context.error);

This method can be added manually in MVC

protected void Application_Error (object sender, EventArgs e)

{

This method is called when unhandled exception information occurs in the system.

ILog ILog = Logmanager.getlogger (typeof (Mvcapplication));

Ilog. Error ("The system has unhandled exception information", Context.error);

}

Logmanager.getlogger (typeof) where you want to print the log. Debug ("Information");

Pass the log class name that you want to log by Logmanager.getlogger get the ilog of this class (so that in the log file you can see which class output the log is), and then call the Debug method output message.

Because there is more than one place inside a class to print the log, Ilog is generally declared as a static field.

Output error message with the Ilog.error method, the second parameter can pass the exception object. Log. Error ("* * * * +ex"), log. Error ("* * * ERROR", ex)

Cases:

private static ILog ILog = Logmanager.getlogger (typeof (WebForm1));

protected void Page_Load (object sender, EventArgs e)

{

Ilog. Debug ("Debug Information");

Ilog. info ("Information");

Ilog. Warn ("warning message");

Ilog. Error ("wrong message");

Ilog. Fatal ("Critical Information");

}

Remember groceries-log4net components use

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.