Log4net, c # log4net

Source: Internet
Author: User

Log4net, c # log4net

The previous article was copied from others, mainly about rules and instructions. This article focuses on actual use.

First, we will introduce the most common configuration file methods in the project.

1. log4net. config file Method

I used to put the log4net configuration in a separate configuration file, rather than in the app. config or web. in the config file, although this is troublesome, the advantage is that it is clear and clear.

1. add a sentence to the AssemblyInfo. cs file.

[assembly:log4net.Config.DOMConfigurator(Watch=true, ConfigFile="Log4net.config")]

 

2. the following two methods are available. config/web. add two sentences in the config file, and one sentence in the program. let's look at the first one first.

2.1 web. config

<configSections>  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /></configSections><log4net configSource="log4net.config" />

I have killed all configurations unrelated to this. Next, you can check my Log4net. config file.

<? Xml version = "1.0" encoding = "UTF-8"?> <Log4net> <root> <level value = "ALL"/> <appender-ref = "ErrorLogFileAppender"/> <appender-ref = "TraceLogFileAppender"/> </root> <! -- Trace log output --> <appender name = "TraceLogFileAppender" type = "log4net. appender. rollingFileAppender "> <file value =" Logs \ Trace \ "/> <appendToFile value =" true "/> <MaxSizeRollBackups value =" 20 "/> <RollingStyle value =" Date" /> <DatePattern value = "yyyy-MM-dd'.txt '"/> <StaticLogFileName value = "false"/> <layout type = "log4net. layout. patternLayout, log4net "> 

I made an error in the controller and posted the Code clearly.

Public ActionResult Index () {LogHelper. log ("trace "). writeInfo ("Enter HomeController/Index method"); try {int a = 0; int B = 1; var s = B/a;} catch (Exception ex) {string errorMsg = string. format (@ "background exception information: [{0}], the method for triggering the exception: [{1}], the object that causes the exception: [{2}]", ex. message, // exception Message ex. targetSite. toString (), // exception method name ex. source // the object that causes the exception); LogHelper. log ("logsys "). writeError (errorMsg);} LogHelper. log ("trace "). writeInfo ("Exit HomeController/Index method"); return View (list );}

Next, let's take a look at the results:

1). Check the generated directory.

 

Here we use the date naming method. One file is generated every day, and the other is the size restriction method. The size limit for a file is 10 MB, but 10 MB is used, A file is automatically generated.

2). Check the recorded content in the log.

The example here uses the simplest method, mainly to introduce how to use it.

 

2.2 Method configured in the program

Public class LogHelper {static LogHelper () {XmlConfigurator. configure (new FileInfo ("Log4net. config ");} private static LogHelper _ instance = null; private static ILog; public static LogHelper Log (Type t) {if (_ instance = null) _ instance = new LogHelper (); ILog = LogManager. getLogger (t); return _ instance;} public static LogHelper Log (string sType) {if (_ instance = null) _ instance = new LogHelper (); ILog = LogManager. getLogger (sType); return _ instance ;} /// <summary> /// write debugging information /// </summary> /// <param name = "msg"> message </param> public void WriteDebug (string msg) {ILog. debug (msg );} /// <summary> /// write common information /// </summary> /// <param name = "msg"> message </param> public void WriteInfo (string msg) {ILog. info (msg );} /// <summary> /// write warning information /// </summary> /// <param name = "msg"> message </param> public void WriteWarn (string msg) {ILog. warn (msg );} /// <summary> /// write error message /// </summary> /// <param name = "msg"> message </param> public void WriteError (string msg) {ILog. error (msg );} /// <summary> /// write error message /// </summary> /// <param name = "msg"> message </param> /// <param name = "ex"> error message </param> public void WriteError (string msg, exception ex) {ILog. error (msg, ex );} /// <summary> /// write a major error message /// </summary> /// <param name = "msg"> message </param> public void WriteFatal (string msg) {ILog. fatal (msg );}}

It is mainly because no configuration is added to the app. config/web. config file in the XmlConfigurator. Configure () method. config. This statement is also acceptable.

Take a look at the results:

This is only Begin logging. Why is there no End logging? This is automatically added by logger and will be added at the End.

 

Related Article

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.