摘自:http://www.cnblogs.com/huanghai223/archive/2012/02/21/2361529.html
第一步:
添加并应用Log4net.dll。然后在Web.config文件中添加下面的配置局
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
第二步:
新建Log4Net.config的配置文件,并在其中添加下面的配置信息:
<?xml version="1.0" encoding="UTF-8"?>
<log4net debug="false">
<!--按日期分割日志文件 一天一个-->
<appender name="LogFileAppenderByDate" type="log4net.Appender.RollingFileAppender" >
<!--是否续写-->
<param name="AppendToFile" value="true" />
<!--最小锁定模型以允许多个进程可以写入同一个文件-->
<param name="LockingModel" value="log4net.Appender.FileAppender.MinimalLock" />
<param name="StaticLogFileName" value="true" />
<!--保存路径-->
<param name="File" value="d:Log\" />
<param name="DatePattern" value="yyyy-MM-dd.LOG" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="时间:%d %n级别:%level %n类名:%c%n文件:%F 第%L行%n日志内容:%m%n-----------------------------------------%n%n" />
</layout>
</appender>
<!--按日志容量分割日志文件 10KB一个-->
<appender name="LogFileAppenderBySize" type="log4net.Appender.RollingFileAppender" >
<!--是否续写-->
<param name="AppendToFile" value="true" />
<!--最小锁定模型以允许多个进程可以写入同一个文件-->
<param name="LockingModel" value="log4net.Appender.FileAppender.MinimalLock" />
<param name="StaticLogFileName" value="true" />
<!--按照文件的大小进行变换日志文件-->
<param name="RollingStyle" value="Size" />
<param name="File" value="log.txt" />
<!--单个文件最大数量 好像只有在 按Size分割时有效-->
<param name="MaximumFileSize" value="200KB"/>
<!--保留的log文件数量 超过此数量后 自动删除之前的 好像只有在 按Size分割时有效-->
<param name="MaxSizeRollBackups" value="2" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="发生时间:%d %n事件级别:%level %n相关类名:%c%n程序文件:%F 第%L行%n日志内容:%m%n-----------------------------------------%n%n" />
</layout>
</appender>
<!--记录日志到数据库-->
<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" />
</parameter>
</appender>
<root>
<level value="INFO" />
<!--启用按日期分割-->
<appender-ref ref="LogFileAppenderByDate" />
<!--启用按容量分割-->
<!--<appender-ref ref="LogFileAppenderBySize" />-->
<!--启用保存到数据库-->
<!--<appender-ref ref="AdoNetAppender" />-->
</root>
</log4net>
<log4net debug="false">
<!--按日期分割日志文件 一天一个-->
<appender name="LogFileAppenderByDate" type="log4net.Appender.RollingFileAppender" >
<!--是否续写-->
<param name="AppendToFile" value="true" />
<!--最小锁定模型以允许多个进程可以写入同一个文件-->
<param name="LockingModel" value="log4net.Appender.FileAppender.MinimalLock" />
<param name="StaticLogFileName" value="true" />
<!--保存路径-->
<param name="File" value="d:Log\" />
<param name="DatePattern" value="yyyy-MM-dd.LOG" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="时间:%d %n级别:%level %n类名:%c%n文件:%F 第%L行%n日志内容:%m%n-----------------------------------------%n%n" />
</layout>
</appender>
<!--按日志容量分割日志文件 10KB一个-->
<appender name="LogFileAppenderBySize" type="log4net.Appender.RollingFileAppender" >
<!--是否续写-->
<param name="AppendToFile" value="true" />
<!--最小锁定模型以允许多个进程可以写入同一个文件-->
<param name="LockingModel" value="log4net.Appender.FileAppender.MinimalLock" />
<param name="StaticLogFileName" value="true" />
<!--按照文件的大小进行变换日志文件-->
<param name="RollingStyle" value="Size" />
<param name="File" value="log.txt" />
<!--单个文件最大数量 好像只有在 按Size分割时有效-->
<param name="MaximumFileSize" value="200KB"/>
<!--保留的log文件数量 超过此数量后 自动删除之前的 好像只有在 按Size分割时有效-->
<param name="MaxSizeRollBackups" value="2" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="发生时间:%d %n事件级别:%level %n相关类名:%c%n程序文件:%F 第%L行%n日志内容:%m%n-----------------------------------------%n%n" />
</layout>
</appender>
<!--记录日志到数据库-->
<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" />
</parameter>
</appender>
<root>
<level value="INFO" />
<!--启用按日期分割-->
<appender-ref ref="LogFileAppenderByDate" />
<!--启用按容量分割-->
<!--<appender-ref ref="LogFileAppenderBySize" />-->
<!--启用保存到数据库-->
<!--<appender-ref ref="AdoNetAppender" />-->
</root>
</log4net>
第三步:
在Global.asax文件中的Application_Start事件中添加如下代码:
protected void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(Server.MapPath("Log4Net.config")));
}
{
// Code that runs on application startup
log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(Server.MapPath("Log4Net.config")));
}
第四步:调用LOG4net 写日志
protected void Button2_Click(object sender, EventArgs e)
{
ILog logs = LogManager.GetLogger(typeof(TEST));
logs.Fatal("Excption:这里就是要提示的LOG信息");
}
{
ILog logs = LogManager.GetLogger(typeof(TEST));
logs.Fatal("Excption:这里就是要提示的LOG信息");
}
也可以通过写一个LogHelper.cs类进行封装:
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);
}
}
}
}
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);
}
}
}
}
通过封装类写日志的方法如下:
private static readonly ILog logs = LogHelper.GetInstance(); //LogManager.GetLogger(typeof(TEST));
protected void Button2_Click(object sender, EventArgs e)
{
logs.Fatal("Excption:这里就是要提示的LOG信息");
}
protected void Button2_Click(object sender, EventArgs e)
{
logs.Fatal("Excption:这里就是要提示的LOG信息");
}