I had the need to add some custom properties in my log4net messages.

I used the log4net contexts like this:


log4net.ThreadContext.Properties[MessageIdKey] = messageId;

And then updated my rolling file appender:


<!-- Rolling File Appender -->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd-MM-yyyy HH:mm:ss,fff} (%logger) [%2thread]     [%property{MessageId}] %5level %message%newline" />
</layout>
</appender>

and my AdoNetAppender:


<!-- ADO Appender -->
 <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
 <bufferSize value="5" />
 <lossy value="false" />
 <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
 <connectionString value="data source=localhost;initial catalog=MyDb;integrated security=true;" />
 <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception],[MessageId]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @messageId)" />

 ......

 <parameterName value="@messageId" />
 <dbType value="String" />
 <size value="255" />
 <layout type="log4net.Layout.PatternLayout">
 <conversionPattern value="%property{MessageId}" />
 </layout>
 </parameter>

 </appender>

LEAVE A REPLY

Please enter your comment!
Please enter your name here