This article is mainly for you to introduce in detail the use of the. NET Log4net, with the console application as an example, with a certain reference value, interested in small partners can refer to
In this paper, we share the use of the. NET log4net, for your reference, the specific content is as follows
Here is an example of a console application
The first is to add a reference:
After installation, you can see more log4net references in your project:
Add application configuration file app. config, configuring 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 "/> <!--append log to file--< Appendtofile value= "true"/> <!--log retention days-<maxsizerollbackups value= "/>" <!--the size of each file. Use only in mixed mode and file size mode. After the size is exceeded, the positive integer is automatically renamed after all file names, and the oldest write of the number is the largest. Available units: kb| mb| GB. Do not use decimals, otherwise it will always be written to the current log--<maximumfilesize value= "1024KB"/> <!--how to generate multiple log files (date [dates], file size [size], mixed [ Composite]--<rollingstyle value= "Size"/> <!--no write only to one file--<staticlogfilename value= "true"/> <layout type= "log4net. Layout.patternlayout "> <!--recording time:%date thread Id:[%thread] Log level:%-5level 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 record Location:%location%n message Description:%property{message}%n exception:%exception%n Message:%message%newline%n%n--> <conversionpattern value= "%date [%thread]%-5level%logger-%message%newline"/> & lt;/layout> </appender> <root> <level value= "DEBUG"/> <appender-ref ref= " Rollinglogfileappender "/> </root> </log4net></configuration>
To add code in Program.cs:
static void Main (string[] args) {log4net. Config.XmlConfigurator.Configure (); Create a log Record component instance ILog log = Log4net. Logmanager.getlogger (typeof (program)); Log error logs. Error ("Errors occurred:", New Exception ("Log4net Test error message")); Logs the fatal error log. Fatal ("Fatal error occurred:", New Exception ("Log4net Test Fatal Message")); Log general information. Info ("Log4net general Information"); Logs debug Information log. Debug ("Log4net debug Information"); Log the warning message. Warn ("log4net warning message"); Console.WriteLine ("OK"); Console.readkey ();}
Run the program
Here is the console application, if it is a Web application, you can call Log4net.Config.XmlConfigurator.Configure () in the Application_Start method in Global.asax.cs. Define a variable in Global.asax.cs and get the exception in Application_Error and log:
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, even Targs e) {} protected void Application_AuthenticateRequest (object sender, EventArgs e) {} protected void Application_err or (object sender, EventArgs e) {log. Error ("An exception has occurred", Server.GetLastError ()); } protected void Session_End (object sender, EventArgs e) {} protected void Application_End (object sender, EventArgs e) { }}