建立一个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>
-->
<--apperder name 千万注意这里的name不能为AdoNetAppender (???? 可能会重复)
-->
<appender name="ADONetAppender_test" 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信息记录写入数据库)
《——————————以下是保存到文件————————》
<!--log4net 保存到文件配置 BEGIN--><!--
<log4net debug="true">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="log//log4net.log"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
</appender>
<logger name="File">
<level value="All" />
<appender-ref ref="LogFileAppender" />
</logger>
</log4net>
-->
<!--log4net 保存到文件 配置 EN-->
3:在Global.asax.cs中
protected void Application_Start(Object sender, EventArgs e)
{
//读取配置信息
// log4net.Config.DOMConfigurator.Configure();
log4net.Config.XmlConfigurator.Configure();
}
4:一个使用log的demo类
namespace log4net.CommonComponent
{
/** <summary>
/// 类,事件日志类。
/// 单态封装log4net 自定义
/// </summary>
public class myEventLog
{
/*以下是将信息保存到文件上*/
/*private static log4net.ILog log = null;
private static object lockHelper = new object();
public static log4net.ILog Log
{
get
{
if (log == null)
lock (lockHelper)
if (log == null)
log = log4net.LogManager.GetLogger("File");
return log;
}
}
*/
/*保存到数据库*/
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(myEventLog));
public static void LogInfo()
{
log.Info("log日志");
}
public static void LogError()
{
log.Info("log错误");
}
}
}