The original Article, today with WLW to the page, the original article and accessories: http://zhq.ahau.edu.cn/blog/article.asp? Id = 366
Log4net is a sub-project of Apache, which is implemented by. Net of Java version. For details, refer to its official website. You can also find its and detailed development documents.
Log4net official website http://logging.apache.org/log4net
WinForm and WebForm are used in the same way. They are basically (1.) configuring config, (2.) initializing the log system, and logging in (3.) files.
The configuration method in the Config file can be as follows:
<Configuration> <! -- Log configuration --> <configSections> <section name = "log4net" type = "log4net. Config. Log4NetConfigurationSectionHandler, log4net"/> </configSections> <! -- Site log configuration --> <log4net> <root> <priority value = "ALL"/> <appender-ref = "RollingFileAppender"/> </root> <appender name = "TraceAppender" type = "log4net. appender. traceAppender "> <layout type =" log4net. layout. patternLayout "> <conversionPattern value =" % date [% thread] %-5 level % logger [% property {NDC}]-% message % newline "/> </layout> </appender> <appender name = "leleappender" type = "log4net. appender. consoleAppender "> <layout type =" log4net. layout. patternLayout "> <conversionPattern value =" % date [% thread] %-5 level % logger [% property {NDC}]-% message % newline "/> </layout> </appender> <appender name = "RollingFileAppender" type = "log4net. appender. rollingFileAppender "> <file value =" log \ log.txt "/> <appendToFile value =" true "/> <maxSizeRollBackups value =" 10 "/> <maximumFileSize value =" 2000KB" /> <rollingStyle value = "Size"/> <staticLogFileName value = "true"/> <lockingModel type = "log4net. appender. fileAppender + MinimalLock "/> <layout type =" log4net. layout. patternLayout "> <conversionPattern value =" % date [% thread] %-5 level % logger [% property {NDC}]-% message % newline "/> </layout> </appender> </log4net> </configuration>
For details about the usage of the configuration file, see the log4net development documentation.
For the basic usage of Log4net, see its development documentation. here we provide a log operation class (AppLog. cs), so that you can easily use the log4net log system, but the disadvantage also arises (the specific disadvantage is that the specific position of the append log cannot be captured in the WinForm program ). the following is AppLog. code of the cs file:
/// <Summary> /// log processing /// </summary> public class AppLog {// <summary> // static class /// </summary> private AppLog () {} private const string LOG_REPOSITORY = "Default"; // this shoshould likely be set in the web config. private static ILog m_log; // <summary> /// initialize the log system /// start initialization when the system is running /// Global. in asax Application_Start /// </summary> public static void Init () {log4net. config. xmlConfigurator. configure ();} /// <summary> /// write logs /// </summary> /// <param name = "message"> log information </param> /// <param name = "messageType"> Log Type </param> public static void Write (string message, logMessageType messageType) {DoLog (message, messageType, null, Type. getType ("System. object "));} /// <summary> /// write logs /// </summary> /// <param name = "message"> log information </param> /// <param name = "messageType"> Log type </param> // <param name = "type"> </param> public static void Write (string message, logMessageType messageType, Type type) {DoLog (message, messageType, null, type );} /// <summary> /// write logs /// </summary> /// <param name = "message"> log information </param> /// <param name = "messageType"> Log Type </param> // <param name = "ex"> exception </param> public static void Write (string message, logMessageType messageType, Exception ex) {DoLog (message, messageType, ex, Type. getType ("System. object "));} /// <summary> /// write logs /// </summary> /// <param name = "message"> log information </param> /// <param name = "messageType"> Log type </param> /// <param name = "ex"> exception </param> /// <param name = "type"> </param> public static void Write (string message, logMessageType messageType, Exception ex, Type type) {DoLog (message, messageType, ex, type );} /// <summary> /// asserted /// </summary> /// <param name = "condition"> condition </param> /// <param name = "message"> log information </param> public static void Assert (bool condition, string message) {Assert (condition, message, Type. getType ("System. object "));} /// <summary> /// asserted /// </summary> /// <param name = "condition"> condition </param> /// <param name = "message"> log information </param> // <param name = "type"> Log type </param> public static void Assert (bool condition, string message, Type type) {if (condition = false) Write (message, LogMessageType. info );} /// <summary> // Save the log // </summary> /// <param name = "message"> log information </param> /// <param name = "messageType"> Log type </param> /// <param name = "ex"> exception </param> /// <param name = "type"> log type </param> private static void DoLog (string message, logMessageType messageType, Exception ex, Type type) {m_log = LogManager. getLogger (type); switch (messageType) {case LogMessageType. debug: AppLog. m_log.Debug (message, ex); break; case LogMessageType. info: AppLog. m_log.Info (message, ex); break; case LogMessageType. warn: AppLog. m_log.Warn (message, ex); break; case LogMessageType. error: AppLog. m_log.Error (message, ex); break; case LogMessageType. fatal: AppLog. m_log.Fatal (message, ex); break ;}} /// <summary> /// Log Type /// </summary> public enum LogMessageType {/// <summary> /// Debug /// </summary>, /// <summary> /// information /// </summary> Info, /// <summary> /// warning /// </summary> Warn, /// <summary> // Error // </summary> Error, /// <summary> // Fatal Error // </summary> Fatal }}
This class provides one enumeration LogMessageType of several types of logs. Several logging methods in log4net are typed and abstracted. when logging, you can call the Write () method to Write logs. write () has several reload methods to achieve different logging methods. example: exception log processing method:
Utility.AppLog.Write("Exception:", Utility.AppLog.LogMessageType.Error, ex);
General information-type log processing
Utility. AppLog. Write ("Write log test...", Utility. AppLog. LogMessageType. Info );
When used in WinForm, initialize the log4net Log System in the Main () method.
// Initialize the log encapsulation class
Utility. AppLog. Init ();
When using WebForm, in addition to configuring log4net settings in the config file, you also need to initialize related log processing classes in the Global File (Global. asax:
Void Application_Start (object sender, EventArgs e) {// code that runs when the application is started // The initialization log uses the encapsulation class TestLog4net. utility. appLog. init (); TestLog4net. utility. appLog. write ("Web site running... ", TestLog4net. utility. appLog. logMessageType. info);} void Application_End (object sender, EventArgs e) {// code TestLog4net that runs when the application is disabled. utility. appLog. write ("Disable the Web site... ", TestLog4net. utility. appLog. logMessageType. info );}
To capture exceptions on the Web site, capture exceptions in the Global File Application_Error and write the logs:
Void Application_Error (object sender, EventArgs e) {// code Exception ex = Server. GetLastError (); if (ex! = Null) {// write the error log TestLog4net. utility. appLog. write ("[Exception]:", TestLog4net. utility. appLog. logMessageType. error, ex); throw ex ;}}
You can find the detailed code in the Demo code file (TestLog4net.
In addition to using encapsulated log operation logs, the Demo code also uses the logging methods described in the log4net development documentation to record logs.
The Demo code project directory is as follows:
TestLog4net. WebProject is a web-based test project. You may need to reload the path during use.
TestLog4net. Utility is the AppLog. cs class used for log4net encapsulation.
TestLog4net. WinForm is the test project used by WinForm.