Steps to use the Log4net logging component are explained and downloaded
- 2020-05-26 08:15:29
- OfStack
Step 1:
Add and apply Log4net.dll. Then add the following bureau to the Web.config file
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
Step 2:
Create a new configuration file for Log4Net.config and add the following configuration information:
<?xml version="1.0" encoding="UTF-8"?>
<log4net debug="false">
<!-- Split log files by date 1 day 1 a -->
<appender name="LogFileAppenderByDate" type="log4net.Appender.RollingFileAppender" >
<!-- Whether written -->
<param name="AppendToFile" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"true" />
<!-- Minimal locking model to allow multiple processes to write to the same 1 A file -->
<param name="LockingModel" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"log4net.Appender.FileAppender.MinimalLock" />
<param name="StaticLogFileName" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"true" />
<!-- Save the path -->
<param name="File" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"d:\Log\\" />
<param name="DatePattern" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"yyyy-MM-dd.LOG" />
<param name="StaticLogFileName" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"false" />
<param name="RollingStyle" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/" time :%d %n level :%level %n The name of the class :%c%n file :%F The first %L line %n Log contents :%m%n-----------------------------------------%n%n" />
</layout>
</appender>
<!-- Split log files by log capacity 10KB1 a -->
<appender name="LogFileAppenderBySize" type="log4net.Appender.RollingFileAppender" >
<!-- Whether written -->
<param name="AppendToFile" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"true" />
<!-- Minimal locking model to allow multiple processes to write to the same 1 A file -->
<param name="LockingModel" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"log4net.Appender.FileAppender.MinimalLock" />
<param name="StaticLogFileName" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"true" />
<!-- Transform log files by file size -->
<param name="RollingStyle" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"Size" />
<param name="File" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"log.txt" />
<!-- Maximum number of a single file As if only in According to the Size Valid at partition -->
<param name="MaximumFileSize" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"200KB"/>
<!-- Keep the log The number of files After this quantity is exceeded Auto delete before As if only in According to the Size Valid at partition -->
<param name="MaxSizeRollBackups" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"2" />
<param name="StaticLogFileName" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"false" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/" Time of occurrence :%d %n Event level :%level %n Related to the name of the class :%c%n Program files :%F The first %L line %n Log contents :%m%n-----------------------------------------%n%n" />
</layout>
</appender>
<!-- Log to database -->
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"1" />
<!-- The buffer size -->
<connectionType value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"Data Source=.\SQLEXPRESS;Initial Catalog=CVDS;User ID=sa;Password=sasa" />
<commandText value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"INSERT INTO Log([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
<parameter>
<parameterName value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"@log_date" />
<dbType value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"@thread" />
<dbType value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"String" />
<size value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"%thread" />
</layout>
</parameter>
<parameter>
<parameterName value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"@log_level" />
<dbType value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"String" />
<size value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"%level" />
</layout>
</parameter>
<parameter>
<parameterName value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"@logger" />
<dbType value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"String" />
<size value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"%logger" />
</layout>
</parameter>
<parameter>
<parameterName value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"@message" />
<dbType value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"String" />
<size value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"%message" />
</layout>
</parameter>
<parameter>
<parameterName value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"@exception" />
<dbType value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"String" />
<size value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<root>
<level value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"INFO" />
<!-- Enables splitting by date -->
<appender-ref ref="LogFileAppenderByDate" />
<!-- Enable partitioning by capacity -->
<!--<appender-ref ref="LogFileAppenderBySize" />-->
<!-- Enable saving to the database -->
<!--<appender-ref ref="AdoNetAppender" />-->
</root>
</log4net>
Step 3:
In the Application_Start event in the Global.asax file, add the following code:
protected void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(Server.MapPath("Log4Net.config")));
}
Step 4: call LOG4net to log
protected void Button2_Click(object sender, EventArgs e)
{
ILog logs = LogManager.GetLogger(typeof(TEST));
logs.Fatal("Excption: So that's the hint LOG information ");
}
You can also encapsulate it by writing a class LogHelper.cs:
using System;
using System.Collections.Generic;
using System.Web;
using log4net;
namespace SBIT.Web.Class
{
/// <summary>
/// Log helper class
/// </summary>
public class LogHelper
{
private static ILog log;
private static LogHelper logHelper = null;
/// <summary>
/// Initialize the
/// </summary>
/// <returns></returns>
public static ILog GetInstance()
{
logHelper = new LogHelper(null);
return log;
}
/// <summary>
/// Initialize the
/// </summary>
/// <param name="configPath"></param>
/// <returns></returns>
public static ILog GetInstance(string configPath)
{
logHelper = new LogHelper(configPath);
return log;
}
/// <summary>
/// The constructor
/// </summary>
/// <param name="configPath"></param>
private LogHelper(string configPath)
{
if (!string.IsNullOrEmpty(configPath))
{
log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(configPath));
}
else
{
log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}
}
}
}
The way to write a log by encapsulating the class is as follows
private static readonly ILog logs = LogHelper.GetInstance(); //LogManager.GetLogger(typeof(TEST));
protected void Button2_Click(object sender, EventArgs e)
{
logs.Fatal("Excption: So that's the hint LOG information ");
}
Log4net download address: http: / / xiazai ofstack. com tools / 201302 / tools/log4net - 1.2.11 - src. zip