Log4Net not logging errors to db

This is a short post about logging errors to db with log4net.

I've recently been bitten by a bug or better said miss configuration of log4net. I'm using log4net with database logging and file logging in a project and i was wandering why there are no error messages logged in the db when they are logged in the log files configured.

At one point it hit me: Transactions. Good ol` transactions. Distributed ones.

In this project i use a mix of NServiceBus based windows services and WCF services. They all use a variation of the Unit Of Work pattern to manage the atomicity of the operations. Since we use MSMQ messaging and NHibernate sessions they all fall under a distributed transaction. By default log4net AdoNet appender also uses a transaction to insert the log message in the db. And this transaction was enlisting in the ambient distributed transaction. Now in case of an error the unit of work get's aborted and the transactions get rolled back. This also includes log4net's transaction.

The solution is simple after you figure out the problem. Just add in the configuration section for the ADONetAppender. Ex:

1 <appender name="MySQLAppender" type="log4net.Appender.ADONetAppender">
2     <usetransactions value="false"/>
3     <!-- rest of the configuration -->
4 </appender>

Comments