Log4Net Troubleshooting

When Log4Net doesn’t work, it can be a very frustrating experience.  Unlike your typical application library, log4net doesn’t throw exceptions when it fails.  Well that is to be expected and makes a lot of sense since it is a logging library.  I wouldn’t want my application to fail because it had trouble logging a message.

Unfortunately, the downside of this is that problems with log4net aren’t immediately apparent.  99.9% of the time, when Log4Net doesn’t work, it is a configuration issue.  Here are a couple of troubleshooting tips that have helped me out.

Enable Internal Debugging

This tip is straight from the Log4Net FAQ, but not everyone notices it. To enable internal debugging, add the following app setting to your App.config (or Web.config for web applications) file.

<add key="log4net.Internal.Debug" value="true"/>

This will write internal log4net messages to the console as well as the System.Diagnostics.Trace system.  You can easily output the log4net internal debug messages by adding a trace listener.  The following snippet is taken from the log4net FAQ and goes in your <configuration> section of your application config file.

<system.diagnostics>
  <trace autoflush="true">
    <listeners>
      <add 
        name="textWriterTraceListener" 
        type="System.Diagnostics.TextWriterTraceListener" 
        initializeData="C:\tmp\log4net.txt" />
    </listeners>
  </trace>
</system.diagnostics>

Passing Nulls For Value Types Into AdoNetAppender

Another common problem I’ve dealt with is logging using the AdoNetAppender. In particular, attempting to log a null value into an int parameter (or other value type), assuming your stored procedure allows null for that parameter.

The key here is to use the RawPropertyLayout for that parameter. Here is a snippet from a log4net.config file that does this.

<parameter>
  <parameterName value="@BlogId" />
  <dbType value="Int32" />
  <layout type="log4net.Layout.RawPropertyLayout">
    <key value="BlogId" />
  </layout>
</parameter>

Hopefully this helps you with your log4net issues.

Related Posts

tags:

What others have said

Requesting Gravatar... Ben Scheirman Sep 27, 2006 4:52 PM
# re: Log4Net Troubleshooting
I've found that another log4net annoyance (not really log4net's fault, but annoying none-the-less) is that it's database commands will auto-enlist inside of a TransactionScope (or COM+ distributed transaction).

With my data access unit tests I've been regularly just newing up a TransactionScope on Setup and disposing it on TearDown. This is great to keep the db in a known state every time, but bad not having ANY logging for my tests. I've resorted to using file appenders for my tests, which is hard to sift through.

Thoughts?
Requesting Gravatar... Haacked Sep 27, 2006 5:26 PM
# re: Log4Net Troubleshooting
Interesting. I use RollBack attribute on many tests in MbUnit which does the same thing.

However, I generally don't use the AdoNetAppender in my config file for my unit tests. I use the ConsoleAppender there, so I've never noticed this issue.
Requesting Gravatar... Christopher Steen Sep 27, 2006 10:51 PM
# Link Listing - September 27, 2006
Accessing and Updating Data in ASP.NET 2.0: Retrieving XML Data with XmlDataSource Control [Via: ] Vista...
Requesting Gravatar... Charlie Calvert's Community Blog Sep 28, 2006 9:14 PM
# Community Convergence V
Welcome to the fifth Community Convergence update. This file is published on my blog, and shortly thereafter...
Requesting Gravatar... Laurent Nullens Oct 03, 2006 3:36 AM
# re: Log4Net Troubleshooting
Instead of writing the trace messages to a file you can also use DebugView from sysinternals. This tool saves me a lot of debug time...
Requesting Gravatar... Salo Oct 23, 2006 4:17 PM
# re: Log4Net Troubleshooting
Thanks for the tip on the null values for ADO.
Requesting Gravatar... Sameer Alibhai Feb 13, 2008 6:32 AM
# re: Log4Net Troubleshooting
Sweet, thanks for that tip regaridng the nulls. Too bad I can't search google for "(null)" since brackets and special characters are not indexed.
Requesting Gravatar... kevin Feb 19, 2008 5:27 PM
# Log4Net can get the StoredProcedure's return value?
thanks for the tip.I use log4net to access database and record message into several tables, but how can I get the row ID which I just recorded in the table?
Requesting Gravatar... Rathish Nair Jun 16, 2008 11:56 PM
# re: Log4Net Troubleshooting

Thanks for your info.

I could sole my issues with log4net.

Regards.
Requesting Gravatar... Max Oct 08, 2008 3:16 AM
# re: Log4Net Troubleshooting
Thank you very much for your tips!

They are very useful!

What do you have to say?

(will show your gravatar)
Please add 6 and 8 and type the answer here: