Accumulate [C #] --- use the log4net component to record error logs (recorded in text format ),
Effect:
Description:
The log4net component is used to record error logs. I know four methods for logging errors in log4net. logs are recorded in the No.1 text format, and logs are stored in the database in the No. 2 format, no. 3 console control display log, No. 4 Windows event log. This article uses the first form-text form to record logs.
First, reference the log4net. dll file, and create a config file to configure log information, such as the configuration path and incorrect display format. Create a Global. asax application file to execute the configuration file during the first access. Create a class, organize error logs, and call this class to write logs.
Code:
Configuration file (log. config)
<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <configSections> <section name = "log4net" type = "log4net. config. log4NetConfigurationSectionHandler, log4net "/> </configSections> <log4net> <root> <level value =" ALL "/> <! -- Log in the file format --> <appender-ref = "RollingLogFileAppender"/> </root> <! -- Output to file --> <appender name = "RollingLogFileAppender" type = "log4net. Appender. RollingFileAppender"> <! -- Save path --> <file value = "log \ Log-"/> <! -- Whether to continue writing --> <appendToFile value = "true"/> <! -- Change the log file according to the file size --> <rollingStyle value = "Date"/> <! -- The number of retained log files is exceeded. Previously, the log files are automatically deleted only when they are split by Size. --> <param name = "MaxSizeRollBackups" value = "100"/> <param name = "StaticLogFileName" value = "false"/> <param name = "DatePattern" value = "yyyy-MM-dd & quot ;. log & quot; "/> <encoding value =" UTF-8 "/> <! -- Minimum lock model to allow multiple processes to write data to the same file --> <lockingModel type = "log4net. Appender. RollingFileAppender + MinimalLock"/> <! -- <MaxSizeRollBackups value = "10"/> <maximumFileSize value = "1 MB"/> --> <layout type = "log4net. layout. patternLayout "> <param name =" conversionPattern "value =" Time: % d % n thread: [% t] % n type: %-5 p % n title: % c % n content: % m % n ---------- % n "/> <param name =" Header "value =" & #13; & #10; ---------------------- header ------------------------ & #13; & #10; & #13; & #10; "/> <param name =" Footer "value =" & #13; & #10; ---------------------- footer ------------------------ & #13; & #10; "/> </layout> </appender> </log4net> </configuration>
Application (Global. asax)
protected void Application_Start(object sender, EventArgs e) { log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(Server.MapPath("~") + @"\log.config")); }
Log processing (logHelper. cs)
Using System; using System. collections. generic; using System. linq; using System. web; namespace log4net_Write {public class logHelper {// <summary> // generate log information -- Fatal (Fatal error) /// </summary> /// <param name = "message"> log content </param> public static void Fatal (string message) {log4net. ILog log = log4net. logManager. getLogger ("Fatal"); if (log. isFatalEnabled) {log. fatal (message) ;}log = null ;}/// <summary> /// generate the log information -- Fatal (Fatal error) /// </summary> /// <param name = "name"> name </param> /// <param name = "message"> log content </param> public static void Fatal (string name, string message) {log4net. ILog log = log4net. logManager. getLogger (name); if (log. isFatalEnabled) {log. fatal (message) ;}log = null ;}/// <summary> /// generate log information -- Error (general Error) /// </summary> /// <param name = "message"> log content </param> public static void Error (string message) {log4net. ILog log = log4net. logManager. getLogger ("Error"); if (log. isErrorEnabled) {log. error (message) ;}log = null ;}/// <summary> /// generate log information -- Error (general Error) /// </summary> /// <param name = "name"> name </param> /// <param name = "message"> log content </param> public static void Error (string name, string message) {log4net. ILog log = log4net. logManager. getLogger (name); if (log. isErrorEnabled) {log. error (message) ;}log = null ;}/// <summary> /// generate log information -- Warn (warning) /// </summary> /// <param name = "message"> log content </param> public static void Warn (string message) {log4net. ILog log = log4net. logManager. getLogger ("Warn"); if (log. isWarnEnabled) {log. warn (message);} log = null;} // <summary> // generate log information -- Warn (warning) /// </summary> /// <param name = "name"> name </param> /// <param name = "message"> log content </param> public static void Warn (string name, string message) {log4net. ILog log = log4net. logManager. getLogger (name); if (log. isWarnEnabled) {log. warn (message);} log = null;} // <summary> // generate log information -- Info (General Information) /// </summary> /// <param name = "message"> log content </param> public static void Info (string message) {log4net. ILog log = log4net. logManager. getLogger ("Info"); if (log. isInfoEnabled) {log. info (message) ;}log = null ;}/// <summary> /// generate log information -- Info (General Information) /// </summary> /// <param name = "name"> name </param> /// <param name = "message"> log content </param> public static void Info (string name, string message) {log4net. ILog log = log4net. logManager. getLogger (name); if (log. isInfoEnabled) {log. info (message) ;}log = null ;}/// <summary> /// generate log information -- Debug (debugging information) /// </summary> /// <param name = "message"> log content </param> public static void Debug (string message) {log4net. ILog log = log4net. logManager. getLogger ("Debug"); if (log. isDebugEnabled) {log. debug (message);} log = null;} // <summary> // generate log information -- Debug (debugging information) /// </summary> /// <param name = "name"> name </param> /// <param name = "message"> log content </param> public static void Debug (string name, string message) {log4net. ILog log = log4net. logManager. getLogger (name); if (log. isDebugEnabled) {log. debug (message) ;}log = null ;}}}
Call
Protected void btn_Click (object sender, EventArgs e) {try {int intStr = Convert. toInt32 (tb. text); tb2.Text = "converted successfully:" + intStr. toString ();} catch (Exception ex) {// logHelper. error (ex. toString (); // logHelper. debug (ex. toString (); // logHelper. fatal (ex. toString (); // logHelper. info (ex. toString (); logHelper. warn ("warn name", ex. toString ());}}
Download Demo:
Http://files.cnblogs.com/files/xinchun/GetLog2.zip