Using the log4net AdoNetAppender to log to a bit(boolean) column

By Haan on Tuesday 8 June 2010 16:52 - Comments (6)
Category: log4net, Views: 10.447

Many .Net developers are using log4net as logging framework. To log messages to a MSSQL database, you can use the AdoNetAppender class that is in the log4net framework.

There are many examples of how to implement this appender, so setting up basic logging using the AdoNetAppender is pretty easy. Adding extra string columns is still relatively easy, just add a parameter in the log4net config:

XML:
1
2
3
4
5
6
7
8
<parameter>
       <parameterName value="@myNewParam" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{myNewParam}" />
        </layout>
</parameter>



(don't forget to create a nvarchar(50) column in the Log table ;) )

Assign text to the property before logging a message to make it actually show up in your log table (this part is a bit harder to find):

C#:
1
2
log4net.GlobalContext.Properties["myNewParam"] = "test";
Logger.Debug("test debug message");



But today I spent a few hours figuring out how to log a boolean value to the Log table. I couldn't find any information on the Internet, so I put in this blog where it may help others who run into the same issue.

The actual solution is off course very simple, once you know to do it ;)
You follow the same procedure as for adding string columns, with the following changes:
- create a bit column in the Log table, for example 'myBooleanParam'
- configure the parameter as follows:

XML:
1
2
3
4
5
6
7
<parameter>
        <parameterName value="@myBooleanParam" />
        <dbType value="Boolean" />        
        <layout type="log4net.Layout.PatternLayout" >
          <conversionPattern value="%property{myBooleanParam}" />
        </layout>
</parameter>



Now you can assign 'true' or 'false' to the myBooleanParam property like in the example above and it will show up as 1 or 0 in the Log table.