Log4net日志记录组件的使用步骤详解和下载
第一步:
添加并应用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/"时间:%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=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" />
<!--单个文件最大数量 好像只有在 按size分割时有效-->
<param name="maximumfilesize" value=http://www.cnblogs.com/huanghai223/archive/2012/02/21/"200kb"/>
<!--保留的log文件数量 超过此数量后 自动删除之前的 好像只有在 按size分割时有效-->
<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" />
<!--启用按容量分割-->
<!--<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")));
}
第四步:调用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下载地址: