Let op: Tweakers stopt per 2023 met Tweakblogs. In
dit artikel
leggen we uit waarom we hiervoor hebben gekozen.
Using the log4net AdoNetAppender to log to a bit(boolean) column
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:
(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):
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:
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.
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.
06-'10 Using jQuery in MS Dynamics CRM
Comments
Thank you for this post. I find it helpfull since I'm also working with log4net 
Call me stupid, but wasn't the analogy with the String-parameter pretty obvious? 
Hey Coltrui, you're stupid! 
quote:Coltrui wrote on Tuesday 08 June 2010 @ 21:02:
Call me stupid, but wasn't the analogy with the String-parameter pretty obvious?
But the whole logging to custom columns was still new to me, so the configuration for boolean fields wasn't so obviousThe actual solution is off course very simple, once you know to do it
[Comment edited on woensdag 9 juni 2010 09:08]
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. ?
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. ?
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)
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