IWebLog.cs
using System;
using log4net;
namespace log4net.Ext.Web
{
public interface IWebLog : ILog
{
void Info(string clientIP, string requestUrl, object message);
void Info(string clientIP, string requestUrl, object message, Exception t);
void Warn(string clientIP, string requestUrl, object message);
void Warn(string clientIP, string requestUrl, object message, Exception t);
void Error(string clientIP, string requestUrl, object message);
void Error(string clientIP, string requestUrl, object message, Exception t);
void Fatal(string clientIP, string requestUrl, object message);
void Fatal(string clientIP, string requestUrl, object message, Exception t);
}
} log4net的配置,ConnectionType需要根据数据库驱动配置正确,如果你使用的是其它SQLiteADO.NET驱动:
log.xml
<?xml version="1.0" encoding="utf-8"?>
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="0" />
<connectionType value="System.Data.SQLite.SQLiteConnection, System.Data.SQLite, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
<connectionString value="Data Source=|DataDirectory|log.s3db;Version=3;" />
<commandText value="INSERT INTO Log (Date, Level, Logger, ClientIP, RequestUrl, Message, Exception) VALUES (@Date, @Level, @Logger, @ClientIP, @RequestUrl, @Message, @Exception)" />
<parameter>
<parameterName value="@Date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@Level" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@Logger" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@ClientIP" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{ClientIP}" />
</layout>
</parameter>
<parameter>
<parameterName value="@RequestUrl" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{RequestUrl}" />
</layout>
</parameter>
<parameter>
<parameterName value="@Message" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@Exception" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%exception" />
</layout>
</parameter>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="AdoNetAppender"/>
</root>
</log4net> 使用:
Global.asax
private static readonly IWebLog WebLog = WebLogManager.GetLogger("Application_Error");
void Application_Start(object sender, EventArgs e)
{
//在应用程序启动时运行的代码
log4net.Config.DOMConfigurator.Configure(new FileInfo(Server.MapPath("~/log.xml")));
}
void Application_End(object sender, EventArgs e)
{
//在应用程序关闭时运行的代码
}
void Application_Error(object sender, EventArgs e)
{
//在出现未处理的错误时运行的代码
Exception exception = Server.GetLastError().InnerException;
WebLog.Error(Request.UserHostAddress, Request.Url.ToString(), exception.Message, exception);
}
protected void Page_Load(object sender, EventArgs e)
{
throw new Exception("the message.");
}