最近在研究这个东东,按照标准的功能,Log4net只提供下面信息的记录:
CREATE TABLE [dbo].[A_System_Logging] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[Date] [datetime] NOT NULL ,
[Thread] [varchar] (255) NOT NULL ,
[Level] [varchar] (20) NOT NULL ,
[Logger] [varchar] (255) NOT NULL ,
[Message] [varchar] (4000) NOT NULL,
[Exception] [varchar] (2000) NOT NULL,
) ON [PRIMARY]
而我们经常需要记录一些额外的信息,比如用户的ID...我在网上搜索了一下,好多人文章都非常复杂,难以理解,如果真的仿照去做,又恐很难实现。
于是在国外网站搜索了一下,问题立马解决,非常简单,易于理解,一下就成功了:
1) Modify the command text: INSERT
INTO Log4Net ([Date],[Thread],[Level],[Logger],[Message],[Exception],[MyColumn]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @CustomColumn)
2) Add the parameter definition for the custom column:
<parameter>
<parameterName value="@CustomColumn"/>
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{CustomColumn}" />
</layout>
</parameter>
3) Then you use one of log4net’s contexts to transfer values to the parameter:
// thread properties
log4net.LogicalThreadContext.Properties["CustomColumn"] = "Custom value";
log.Info("Message");
// or global properties
log4net.GlobalContext.Properties["CustomColumn"] = "Custom value";
...
看见没,只要把参数加入context中即可,无需额外的类实现,一下就成功了。
详细参照下面链接:
http://stackoverflow.com/questions/12139486/log4net-how-to-add-a-custom-field-to-my-logging
原文地址:http://blog.csdn.net/hailanzhijia/article/details/44980649