Use of log4net in. net, and use of. netlog4net
The console application is used as an example.
First, add reference:
After installation, you can see that log4net references are added to the project:
Add the application configuration file app. config and configure log4net
<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <configSections> <section name = "log4net" type = "log4net. Config. Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <! -- Define some output appenders --> <appender name = "RollingLogFileAppender" type = "log4net. Appender. RollingFileAppender"> <! -- Log Path --> <file value = "test.txt"/> <! -- Whether to append logs to files --> <appendToFile value = "true"/> <! -- Log Retention days --> <maxSizeRollBackups value = "10"/> <! -- The size of each file. It is only used in the mixed mode and file size mode. After the size is exceeded, a positive integer is automatically added after all file names, and the maximum number is the earliest written. Available unit: KB | MB | GB. Do not use decimals. Otherwise, the current log is always written --> <maximumFileSize value = "1024KB"/> <! -- How to generate multiple log files (Date, Size, and combination of [Composite]) --> <rollingStyle value = "Size"/> <! -- No, only one file is written --> <staticLogFileName value = "true"/> <layout type = "log4net. Layout. PatternLayout"> <! -- Record time: % date thread ID: [% thread] Log level: %-5 level record class: % logger Operator ID: % property {Operator} Operation Type: % property {Action} % n current machine name: % property % n current machine name and login user: % username % n location: % location % n Message description: % property {Message} % n exception: % exception % n Message: % message % newline % n --> <conversionPattern value = "% date [% thread] %-5 level % logger-% message % newline"/> </layout> </appender> <root> <level value = "DEBUG"/> <appender-ref = "RollingLogFileAppender"/> </root> </log4net> </configuration>
Add code to Program. cs:
Static void Main (string [] args) {log4net. config. xmlConfigurator. configure (); // create a logging component instance ILog log = log4net. logManager. getLogger (typeof (Program); // record the error log. error ("Error occurred:", new Exception ("log4net test Error message"); // record the fatal Error log. fatal ("Fatal error:", new Exception ("log4net test Fatal information"); // record general information log. info ("General log4net information"); // record debugging information log. debug ("log4net debugging information"); // log the warning information. warn ("log4net warning information"); Console. writeLine ("OK"); Console. readKey ();}
Run the program,
Here is the console application. If it is a Web application, you can go to Global. asax. call log4net in the Application_Start method in cs. config. xmlConfigurator. configure (); in Global. asax. define a variable in cs, get an exception in Application_Error, and record it:
Public class Global: System. web. httpApplication {private static ILog log = LogManager. getLogger (typeof (Global); protected void Application_Start (object sender, EventArgs e) {log4net. config. xmlConfigurator. configure ();} protected void Session_Start (object sender, EventArgs e) {} protected void Application_BeginRequest (object sender, EventArgs e) {} protected void handle (object sender, EventArgs e) {} protected void Application_Error (object sender, EventArgs e) {log. error ("an exception occurred", Server. getLastError ();} protected void Session_End (object sender, EventArgs e) {} protected void Application_End (object sender, EventArgs e ){}}