Feel like a geek and get yourself Ema Personal Wiki for Android and Windows

13 July 2009

NHibernate and the annoying log4net dependency

A patch

NHibernate has always used log4net as logging library. If you wanted to use a different logging mechanism you couldn't. Fortunately a thin abstraction layer exists, .NET Common.Logging, which much resembles the log4net interface. Common.Logging can be configured to use NLog, log4net, EntLib or a custom built-in lightweight logging.

I created a patch for the 3.0 trunk. The patch contains all changes needed to remove the dependency on log4net and replace it with a dependency on Common.Logging.

The patch files can be downloaded from this location.

The binary files are included in the zip file:
  • lib\net\3.5
    • Common.Logging.dll (updated to 2.0)
    • Common.Logging.*.dll (several logging implementations)
    • Common.Logging.Extension.dll (for unit testing, an in-memory logging implementation)

I upgraded the unit-tests as well. There had to be a few changes:
  • Some of the tests test logging. Because of this, logging had to be moved to an in-memory logger (Common.Logging does not have appender functionality). Because of this, logging that is done by the unit tests itself about the results of unit tests, won't be logged to a logfile. If this is still needed, another mechanism for this has to be found.
  • Some unit tests are not applicable in my situation (SQL Server 2005). I did not test them.
  • Two unit tests now fail on purpose. This might be a bug: the mapping document is invalid, but there is no error thrown when LogLevel != Debug. The tests used to succeed because the loglevelwas other than Debug, but I changed that. Somebody should look into those. See NHibernate.Test.MappingTest.NonReflectiveBinderFixture and NHibernate.Test.NHSpecificTest.NH712.Fixture
At this moment the new logging mechanism makes a breaking change. The config should be updated so it will configure Common.Logging. I included a test project with settings for NLog, Log4Net and the built-in console logger.

You can follow this issue at the NHibernate Jira, issue 1554

No comments: