MSSql数据库日志
建立日志记录表,这个随便了。现在提供官网示例中的数据库
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
)
(一)输出源设置(Appenders)
<appender xmlns="" name="apenderforLog" type="log4net.Appender.AdoNetAppender">
<bufferSize value="100" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
<connectionString value="database=selfTest;server=.;initial catalog=selfTest;User ID=sa;Password=***" />
<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>
(二)说明一下
·connectionType 连接类型
·connectionString连接字符串
·commandText日志写入语句
然后提供各个参数,并为每个参数赋值
(三)测试
mylog.Info("开始记录日志……");
mylog.Error("用户多次登录不成功!");
mylog.Info("日志追加结束.");
结果在数据库中可以看到
Id Date Thread Level Logger Message Exception
1 2009-12-11 09:16:44.610 TestRunnerThread INFO Log4netTest.UTest 开始记录日志……
2 2009-12-11 09:16:44.657 TestRunnerThread ERROR Log4netTest.UTest 用户多次登录不成功!
3 2009-12-11 09:16:44.657 TestRunnerThread INFO Log4netTest.UTest 日志追加结束.