I use the log4net.Appender.AdoNetAppender appender.
My log4net table are the following fields [Date],[Thread],[Level],[Logger],[Message],[Exception]
I would need to add another field to the log4net table (e.g SalesId), but how would I specify in my xml and in code to log the "SalesId" when logging a Error or Info message?
e.g. log.Info("SomeMessage", SalesId)
Here's the 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) 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:
3) Then use one of log4net’s contexts to transfer values to the parameter:
Three types of logging context available in Log4Net.
Log4Net.GlobalContext :- This context shared across all application threads and domains.If two threads set the same property on GlobalContext, One Value will override the other.
Log4Net.ThreadContext :- This context scope limited to calling thread. Here two threads can set same property to different values without overriding to each other.
Log4Net.ThreadLogicalContext :- This context behaves similarly to the ThreadContext. if you're working with a custom thread pool algorithm or hosting the CLR, you may find some use for this one.
Add the following code to your program.cs file:
2) Add the parameter definition for the custom column:
Here is a working version with some personalized preferences. I added a custom column for storing a generated exception code.
1) Add your custom column(exceptionCode here) to Log4net config:
2) Log4NetExtentedLoggingCustomParameters.cs
3) Log4NetExtentedLoggingPatternConverter.cs
4) Log4NetExtentedLoggingPatternLayout.cs
5) Logger.cs // Enjoy your logger with new column! :)
references:
http://blog.stvjam.es/2014/01/logging-custom-objects-and-fields-with