在asp.net下将log4net配置成可log到ms sql
2024-07-10 12:57:24
供稿:网友
log4net是一个优秀的开源日志项目,现将本人使用写成一个demo,如下
0:准备
去http://logging.apache.org/,可下载log4net,最新为1.2版本,
建立一个asp.net web 项目,并将log4net.dll添加到引用
1:建立数据表
create table [dbo].[log] (
[id] [int] identity (1, 1) not null,
[date] [datetime] not null,
[thread] [varchar] (255) not null,
[level] [varchar] (50) not null,
[logger] [varchar] (255) not null,
[message] [varchar] (4000) not null,
[exception] [varchar] (2000) null
)
2:编写配置文件
<configuration>
<configsections>
<section name="log4net" type="log4net.config.log4netconfigurationsectionhandler, log4net" />
</configsections>
<system.web>
<!--
......
-->
</system.web>
<log4net>
<root>
<level value="all" />
<appender-ref ref="adonetappender" />
</root>
<!--
<logger name="testapp.logging">
<level value="all"/>
<appender-ref ref="adonetappender" />
</logger>
-->
<appender name="adonetappender" type="log4net.appender.adonetappender">
<buffersize value="10" />
<connectiontype value="system.data.sqlclient.sqlconnection, system.data, version=1.0.3300.0, culture=neutral, publickeytoken=b77a5c561934e089" />
<connectionstring value="server=localhost;database=demo;user id=sa;password=123456" />
<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>
</log4net>
</configuration>
特别说明<buffersize value="10" />,这是log信息记录的缓冲大小,(比如只有log信息记录达到10,
才会将log信息记录写入数据库)
3:在global.asax.cs中
protected void application_start(object sender, eventargs e)
{
//读取配置信息
log4net.config.domconfigurator.configure();
}
4:一个使用log的demo类
using system;
namespace log
{
/// <summary>
/// mylog 的摘要说明。
/// </summary>
public class mylog
{
private static readonly log4net.ilog log = log4net.logmanager.getlogger(typeof(mylog));
public static void loginfo()
{
log.info("log日志");
}
public static void logerror()
{
log.info("log错误");
}
}
}
5:资源:
my blog:http://tomch.donews.net/tomch/posts/22457.aspx
主站 logging.apache.org/
学习使用log4net的文章 www.ondotnet.com/pub/a/dotnet/2003/06/16/log4net.html