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
学习使用log4net的文章 www.ondotnet.com/pub/a/dotnet/2003/06/16/log4net.html
2472-Log.rar