How to use log4net under. Net


This example for you to share the. Net log4net use, for your reference, the specific content is as follows

Take the console application as an example

First, add a reference:

After installation, you can see that there are more references to log4net in the project:

Add application configuration file app.config and configure log4net

<?xml version="1.0" encoding="utf-8" ?>
 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, 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="10"/>
  <!-- The size of each file. Only in mixed mode and file size mode. After exceeding the size, the positive integer will be added automatically after all file names, and the file with the largest number will be written as early as possible. Available units: KB | MB | GB. Do not use decimal, otherwise it will always be written to the current log -- >
  <maximumFileSize value="1024KB"/>
  <!-- How to generate multiple log files (date, size, composite)
  <rollingStyle value="Size"/>
  <!-- No, write to only one file -- >
  <staticLogFileName value="true"/>
  <layout type="log4net.Layout.PatternLayout">
  <!-- Record time: Date thread ID: [% thread] log level:% - 5level record class: logger operator ID: property {operator} operation type: property {action}% ncurrent machine name: Property% ncurrent machine name and login user: username% nrecord location: Location: property {message}% nexception: message% newline% n %n-->
  <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
  <level value="DEBUG"/>
  <appender-ref ref="RollingLogFileAppender"/>

Add code in program.cs:

static void Main(string[] args)
 //Create a logging component instance
 ILog log = log4net.LogManager.GetLogger(typeof(Program));
 //Record error log
 Log. Error ("error occurred):, new exception (" test error information of log4net ");
 //Record fatal errors
 Log. Fatal ("fatal error occurred):, new exception (" log4net test fatal information ");
 //Record general information ("general information of log4net");
 //Record debugging information
 Log. Debug ("debugging information of log4net");
 //Record warning messages
 Log. Warn ("log4net warning information");

Run the program

Here is the console application. If it is a web application, you can apply it in global.asax.cs_ Log4net.Config.XmlConfigurator.Configure () is called in the Start method.    Define a variable in global.asax.cs and use the_ Get the exception in error and record:

public class Global : System.Web.HttpApplication
 private static ILog log = LogManager.GetLogger(typeof(Global));

 protected void Application_Start(object sender, EventArgs e)

 protected void Session_Start(object sender, EventArgs e)


 protected void Application_BeginRequest(object sender, EventArgs e)


 protected void Application_AuthenticateRequest(object sender, EventArgs e)


 protected void Application_Error(object sender, EventArgs e)
  Log. Error ("exception occurred", server. Getlasterror());

 protected void Session_End(object sender, EventArgs e)


 protected void Application_End(object sender, EventArgs e)


The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.