Log4Net,如何添加自定义字段到我的日志logging
我使用log4net.Appender.AdoNetAppender appender。
我的log4net表是以下字段[Date],[Thread],[Level],[Logger],[Message],[Exception]
我需要添加另一个字段到log4net表(例如SalesId),但是如何在我的xml和代码中指定loggingError或Info消息时logging“SalesId”?
例如log.Info("SomeMessage", SalesId)
这是log4net xml
<appender name="SalesDBAppender" type="log4net.Appender.AdoNetAppender"> <bufferSize value="1" /> <connectionType value ="System.Data.SqlClient.SqlConnection" /> <connectionString value="Data Source=..." /> <commandText value="INSERT INTO Log4Net ([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>
1)修改命令文本: INSERT INTO Log4Net ([Date],[Thread],[Level],[Logger],[Message],[Exception],[MyColumn]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @CustomColumn)
2)添加自定义列的参数定义:
<parameter> <parameterName value="@CustomColumn"/> <dbType value="String" /> <size value="255" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%property{CustomColumn}" /> </layout> </parameter>
3)然后使用log4net的上下文之一将值传递给参数:
// thread properties... log4net.LogicalThreadContext.Properties["CustomColumn"] = "Custom value"; log.Info("Message"); // ...or global properties log4net.GlobalContext.Properties["CustomColumn"] = "Custom value";