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.445

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.

Volgende: Using jQuery in MS Dynamics CRM 06-'10 Using jQuery in MS Dynamics CRM

Comments


By Tweakers user Arethusa, Tuesday 8 June 2010 17:31

Thank you for this post. I find it helpfull since I'm also working with log4net :)

By Tweakers user Coltrui, Tuesday 8 June 2010 21:02

Call me stupid, but wasn't the analogy with the String-parameter pretty obvious? :)

By Tweakers user ViperNL, Tuesday 8 June 2010 22:15

Hey Coltrui, you're stupid! :9

By Tweakers user Haan, Wednesday 9 June 2010 09:07

Coltrui wrote on Tuesday 08 June 2010 @ 21:02:
Call me stupid, but wasn't the analogy with the String-parameter pretty obvious? :)
quote:
The actual solution is off course very simple, once you know to do it
But the whole logging to custom columns was still new to me, so the configuration for boolean fields wasn't so obvious ;)

[Comment edited on Wednesday 9 June 2010 09:08]


By Robert, Thursday 12 January 2012 13:44

Hi All,

I m new to this log4Net.
I have implimented to log to DB. Its working but DB will reflected after some time.
If, project is on running mode, it will not reflect on DB.
It will be reflected, once I stop running the project.

Could anyone please help me. ?

By Tweakers user Haan, Thursday 12 January 2012 14:06

Hi Robert, you can have a look at the the documentation page: http://logging.apache.org...ease/config-examples.html

You're interested in the buffer size parameter, set it to 1 and messages are written to database directly (though this may have an impact on performance when many log messages are written to database)

Comments are closed