Log4Net log configuration, log4net log

Source: Internet
Author: User
Tags log4net

Log4Net log configuration, log4net log

1. Add Log4net. dll reference

Copy Log4net. dll of release to the Lib folder and add references.

 

2. Add configuration in Web. Config (or App. Config.

(1) Add the configuration under the configSections node:

<! -- Log4Net configuration --> <section name = "log4net" type = "log4net. Config. Log4NetConfigurationSectionHandler, log4net"/>

(2) Add configuration under the configuration node:

<! -- Log4net configuration (Professional configuration, recommended) --> <log4net> <! -- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL --> <! -- Set root logger level to ERROR and its appenders --> <root> <level value = "WARN"/> <appender-ref = "SysAppender"/> </root> <! -- Print only messages of level DEBUG or abve in the packages --> <logger name = "WebLogger"> <level value = "DEBUG"/> </logger> <appender name =" sysAppender "type =" log4net. appender. rollingFileAppender, log4net "> <param name =" File "value =" App_Data/"/> <param name =" AppendToFile "value =" true "/> <param name =" RollingStyle "value = "Date"/> <param name = "DatePattern" value = "& quot; logs _ & quot; yyyyMMdd & quot ;. t Xt & quot; "/> <param name =" StaticLogFileName "value =" false "/> <layout type =" log4net. layout. patternLayout, log4net "> <param name =" ConversionPattern "value =" % d [% t] %-5 p % c-% m % n "/> <param name =" Header" value = "& amp; #13; & #10; ---------------------- header ------------------------ & #13; & #10; "/> <param name =" Footer "value =" & #13; & #10; ---------------------- footer ------------------------ & #13; & #10; "/> </lay Out> </appender> <appender name = "leleapp" type = "log4net. appender. consoleAppender, log4net "> <layout type =" log4net. layout. patternLayout, log4net "> <param name =" ConversionPattern "value =" % d [% t] %-5 p % c-% m % n "/> </layout> </appender> </log4net> <! -- Log4net configuration is complete -->

Configuration Information Description:

Appender: logs can be output to different places. Different output templates correspond to different Appender:

RollingFileAppender, AdoNetAppender, and SmtpAppender.

 

Level: An Important Level that identifies the log information. None> Fatal> ERROR> WARN> DEBUG> INFO> ALL. If a Level is set, logs lower than this Level will not be written to the Appender.

 

Log4Net can also set multiple Appender to simultaneously record logs to files, data, and send emails. You can set different Appender levels to record files at common levels. Send emails at the Error level or above. You can set different Appender for different classes. You can also customize Appender, so that you can send messages to Error messages.

 

In addition to Log4Net, there are also Logging Application Block in the Enterprise Library, CommonLog of Apache, and NLog.

 

Basic and simple configuration, not recommended

<! -- Log4net configuration (the most basic and simple configuration is not recommended) --> <log4net> <appender name = "RollingLogFileAppender" type = "log4net. appender. rollingFileAppender "> <file value =" test.txt "/> <appendToFile value =" true "/> --> <! -- Append --> <! -- <MaxSizeRollBackups value = "10"/> --> <! -- Backup quantity --> <! -- <MaximumFileSize value = "1024KB"/> <rollingStyle value = "Size"/> <staticLogFileName value = "true"/> <layout type = "log4net. layout. patternLayout "> <conversionPattern value =" % date [% thread] %-5 level % logger-% message % newline "/> </layout> </appender> <root> <level value = "DEBUG"/> <appender-ref = "RollingLogFileAppender"/> </root> </log4net> <! -- Log4net configuration is complete -->View Code

 

3. In the Application_Start method of the Global file, the program starts initialization.

Using log4net; using Spring. web. mvc; using System. collections. generic; using System. IO; using System. linq; using System. text; using System. threading; using System. web; using System. web. http; using System. web. mvc; using System. web. optimization; using System. web. routing; using WebApp. models; namespace WebApp {// Note: For instructions on enabling the IIS6 or IIS7 Classic mode, // visit http://go.microsoft.com/?LinkId=9394801 Public class MvcApplication: SpringMvcApplication // System. web. httpApplication {protected void Application_Start () {AreaRegistration. registerAllAreas (); WebApiConfig. register (GlobalConfiguration. configuration); FilterConfig. registerGlobalFilters (GlobalFilters. filters); RouteConfig. registerRoutes (RouteTable. routes); BundleConfig. registerBundles (BundleTable. bundles); log4net. config. xmlConfigurator. Configure (); // read Log4Net configuration information # region enable thread scanning for different queues and handle exception information string fileLogPath = Server. mapPath ("/Log/"); // knows the folder path used to save the Error Log File // enables a thread to scan the Log queue ThreadPool. unsafeQueueUserWorkItem (a) =>{ while (true) {if (MyExceptionAttribute. predictionqueue. count> 0) // checks whether data exists in the queue {Exception ex = MyExceptionAttribute. predictionqueue. dequeue (); // if (ex! = Null) {// string fileName = DateTime. now. toString ("yyyy-MM-dd") + ". txt "; // File. appendAllText (fileLogPath + fileName, ex. toString (), Encoding. default); // append the exception to the file ILog logger = LogManager. getLogger ("errorMsg"); logger. error (ex); // write the exception information to the disk} else {Thread. sleep (3000) ;}} else {Thread. sleep (3000); // if there is no data in the queue, let the current thread rest for 3 seconds to avoid CPU idling and CPU waste}, fileLogPath ); # endregion }}}

 

4. Test Results

 

5. Download the source code:Click to download>

 

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.