第一步:
添加并应用Log4net.dll。然后在Web.config文件中添加下面的配置局
复制代码 代码如下:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
第二步:
新建Log4Net.config的配置文件,并在其中添加下面的配置信息:
复制代码 代码如下:
<"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/"时间:%d %n级别:%level %n类名:%c%n文件:%F 第%L行%n日志内容:%m%n-----------------------------------------%n%n" />
</layout>
</appender>
<>
<appender name="LogFileAppenderBySize" 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="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" />
<>
<param name="MaximumFileSize" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"200KB"/>
<>
<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/"发生时间:%d %n事件级别:%level %n相关类名:%c%n程序文件:%F 第%L行%n日志内容:%m%n-----------------------------------------%n%n" />
</layout>
</appender>
<>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"1" />
<>
<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" />
<>
<appender-ref ref="LogFileAppenderByDate" />
<>
<>
<>
<>
</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")));
}
第四步:调用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