first step:
Add and apply Log4net.dll. Then add the following configuration innings in the Web.config file
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
Step two:
New Log4Net.config configuration file, and add the following configuration information:
<? xml Version = "1.0" encoding = "UTF-8"?>
<log4net Debug = "false">
<-! split log files by date day a ->
<appender name = "LogFileAppenderByDate" of the type = "log4net .Appender.RollingFileAppender ">
<-! whether writing ->
<param name =" AppendToFile "value =" to true "/>
<-! minimum lock model to allow multiple processes can write to the same file - >
<param name = "LockingModel" value = "log4net.Appender.FileAppender.MinimalLock" />
<param name = "StaticLogFileName" value = "to true" />
<-! save path ->
<param name = "File "value =" D: \ \\ the Log "/>
<param name =" DatePattern "value =" the MM-dd-YYYY.LOG" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Date" />
<layout type = "log4net.Layout.PatternLayout">
<param name = "ConversionPattern" value = "Time:% d% n level:% level% n Class Name:% c% n File:% F% L first row% n log content:% m% n -----------------------------------------% % n-n-"/>
</ layout>
</ the appender>
<-! log file by dividing a log size 10KB ->
<the appender name =" LogFileAppenderBySize "type =" log4net.Appender.RollingFileAppender ">
<! - whether writing ->
<param name = "AppendToFile" value = "to true" />
<-! minimum lock model to allow multiple processes can write to the same file ->
<param name = "LockingModel" value = "log4net.Appender.FileAppender.MinimalLock" />
<param name = "StaticLogFileName" value = "to true"/>
<! - the log file is converted according to the size of the file ->
<param name = "RollingStyle" value = "Size" />
<param name = "File" value = "log.txt" />
<-! The maximum number of individual files if only effective when segmented by Size ->
<param name = "MaximumFileSize" value = "200KB" />
< ! - if only effective when segmented by Size before automatically deleted after the number of log files to retain more than this amount ->
<param name = "MaxSizeRollBackups" value = "2" />
<param name = "StaticLogFileName" value = "to false" />
<layout type = "log4net.Layout.PatternLayout">
<name = "ConversionPattern" value = "param occurrence time:% d% n-level events:% level% n class name:% c% n procedures file:% F% L line of% n log contents:% m% n --------------------------------- % n-n-% -------- "/>
</ layout>
</ the appender>
<-! log to the database ->
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<!--缓冲大小-->
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Data Source=.\SQLEXPRESS;Initial Catalog=CVDS;User ID=sa;Password=sasa" />
<commandText value="INSERT INTO Log([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout "/> <-! Split enabled by date -> <Level value =" INFO "/> <root> </ appender>
</ the Parameter>
<REF-REF = the appender "LogFileAppenderByDate" />
<-! split enabled by volume ->
<-! <REF-REF = the appender "LogFileAppenderBySize" /> ->
<-! enabled to save the database - >
<-! <REF-REF = the appender "AdoNetAppender" /> ->
</ the root>
</ log4net>
third step:
Add the following code in the Global.asax file Application_Start event:
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 Four: Write call log LOG4net
void the Button2_Click protected (SENDER Object, EventArgs E)
{
the ILog logs LogManager.GetLogger = (typeof (the TEST));
logs.Fatal ( "Excption: this is information to be presented to LOG");
}
It may also be encapsulated by writing a LogHelper.cs categories:
using System;
using System.Collections.Generic;
using System.Web;
using log4net;
namespace SBIT.Web.Class
{
/// <summary>
/// 日志辅助类
/// </summary>
public class LogHelper
{
private static ILog log;
private static LogHelper logHelper = null;
/// <summary>
/// 初始化
/// </summary>
/// <returns></returns>
public static ILog GetInstance()
{
logHelper = new LogHelper(null);
return log;
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="configPath"></param>
/// <returns></returns>
public static ILog GetInstance(string configPath)
{
logHelper = new LogHelper(configPath);
return log;
}
/// <summary>
/// 构造函数
/// </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 method of the write log by encapsulating classes are as follows:
logs the ILog static Readonly = Private LogHelper.GetInstance (); //LogManager.GetLogger(typeof(TEST));
protected void the Button2_Click (SENDER Object, EventArgs E)
{
logs.Fatal ( "Excption: this is information to be presented to LOG" );
}