Log4.net using Configuration

Source: Internet
Author: User
Tags log4net

Development often use to the logging function, log4.net can log to the file, can also be recorded in the database, the use of very convenient, has been in use, and recently referred to the information on the Internet, want to summarize briefly

This article is heavy in the general log class to use Log4.net, as to how to configure the Log4.net node, there are many online data, there is no mention

1. First create a log4.net public action class

 /// <summary>    ///log4net Log Dedicated/// </summary>     Public classLoghelper {Private Static ReadOnlyLog4net. ILog Instance = log4net. Logmanager.getlogger ("Loginfo");  Public Static voidSetconfig () {log4net.        Config.XmlConfigurator.Configure (); }         Public Static voidSetconfig (stringFilePath) {FileInfo ConfigFile=NewFileInfo (FilePath); Log4net.        Config.XmlConfigurator.Configure (ConfigFile); }         Public Static voidsetconfig (FileInfo configfile) {log4net.        Config.XmlConfigurator.Configure (ConfigFile); }        /// <summary>        ///record common file records/// </summary>        /// <param name= "info" ></param>         Public Static voidInfo (stringinfo) {            if(instance.isinfoenabled) {instance.info (Info); }        }        /// <summary>        ///Logging Debug Information/// </summary>        /// <param name= "info" ></param>        /// <param name= "se" ></param>         Public Static voidDebug (stringinfo) {            if(instance.iserrorenabled) {instance.debug (info); }        }        /// <summary>        ///Log Warning Messages/// </summary>        /// <param name= "info" ></param>        /// <param name= "se" ></param>         Public Static voidWarn (stringinfo) {            if(instance.iswarnenabled) {Instance.warn (info); }        }        /// <summary>        ///Logging error Logs/// </summary>        /// <param name= "info" ></param>        /// <param name= "se" ></param>         Public Static voidError (stringinfo, Exception se) {            if(instance.iserrorenabled) {instance.error (info, SE); }        }        /// <summary>        ///Log Critical Errors/// </summary>        /// <param name= "info" ></param>        /// <param name= "se" ></param>         Public Static voidFatal (stringinfo, Exception se) {            if(instance.isfatalenabled) {instance.fatal (info, SE); }        }    }

2. Register Log4.net configuration information, I like the following 2 mode configuration, so that other assemblies (such as business assemblies) need to log, no need to configure the log4.net, directly call the log public class to

(1) You can register log4.net configuration information (such as Global.asax) at application startup, but other assemblies need to reconfigure Log4.net information when logging

  protected void Application_Start (object  sender, EventArgs e)        {            // load log profile, just load it once at program startup             string"log4net.config";            Loghelper.setconfig (FilePath);        }

(2) Registering in the AssemblyInfo.cs file of the assembly where the log public class LogHelper.cs is located

" Log4net.config " true)]

(3) Registering on the LogHelper.cs namespace of the log public class

" Log4net.config " true )]namespace  logutility{    //<summary>///     log4net Log Dedicated     /// </summary>     Public class Loghelper    {
....
}

3. Logging

   protected voidPage_Load (Objectsender, EventArgs e) {            #regionCalling through public classes//writing log logs across Assembly callsServices.TestService.TestLog (); //Record General InformationLoghelper.info ("Info"); //Logging Debug InformationLoghelper.debug ("Debug"); //Log Warning MessagesLoghelper.warn ("warn"); //Logging error LogsLoghelper.error ("Error",NewException ("An exception has occurred")); //Log Critical ErrorsLoghelper.fatal ("Fatal",NewException ("A fatal error has occurred.")); #endregionCalling through public classesResponse.Write ("log records are complete. </br>"); }

4. Reference to some of the following:

http://blog.csdn.net/zhoufoxcn/article/details/2220533
http://blog.csdn.net/zhoufoxcn/article/details/6029021
Http://www.cnblogs.com/ringwang/archive/2012/07/06/2579775.html
Http://www.cnblogs.com/zfanlong1314/p/3662679.html

5. The code for this example (demonstrates the use configuration on WinForm, WebForm)

(1) Register directly at application startup

Log4netsolution.zip

(2) Registering in the Public log class

Log4netnewsolution.zip

Log4.net using Configuration

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.