第一步:
添加並應用Log4net.dll。然後在Web.config文件中添加下面的配置局
復制代碼 代碼如下:
<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=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"true" />
<!--最小鎖定模型以允許多個進程可以寫入同一個文件-->
<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" />
<!--保存路徑-->
<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/"
第三步:
在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")));
}
第四步:調用LOG4net 寫日志
復制代碼 代碼如下:
protected void Button2_Click(object sender, EventArgs e)
{
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);
}
}
}
}
通過封裝類寫日志的方法如下
復制代碼 代碼如下:
private static readonly ILog logs = LogHelper.GetInstance(); //LogManager.GetLogger(typeof(TEST));
protected void Button2_Click(object sender, EventArgs e)
{
logs.Fatal("Excption:這裡就是要提示的LOG信息");
}
Log4net下載地址:http://xiazai.jb51.net/tools/201302/tools/log4net-1.2.11-src.zip