Logging with log4net in NUnit

There seems to be quite some confusion when logging with your favorite log4net logger while testing you code with NUnit (especially when using the NUnit GUI Runner).

Well, I just stumpled once again over some code where I not only wanted to look at my asserts, but also check the debug-logging. So added a log4net.config to my testing assembly and I also added the obligatory assembly configuration attribute to the testing assembly:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

But there was no logfile to be found ๐Ÿ™ I later figured, that the first logger is not being called in my testing assembly, but in my core assembly, which is the code under test. And because this assembly had no log4net config no logging repository was set up.

OK, so just added the config attribute to the core assembly, and off you go to the next round of testing. But still no logging … what the heck?!&%$ร‚ยง”

Taking a closer look at the NUnit 2.4.7 release notes reveals, that NUnit is not using log4net for it’s internal logging anymore, but rather intercepts all logging attempts (via reflection). These are than re-routed to the log tab in the GUI runner. And indeed – at least ERROR messages are being logged (actually, this was done all the time already). But what about my DEBUG-leve I setup in my log4net.config?

Time to do some more digging. The documentation for the configuration files shows how it’s done (just add a app.config to your testing project if you don’t have one already):

<?xml version="1.0" encoding="utf-8" ?>
<sectionGroup name="NUnit">
<section name="TestCaseBuilder" type="System.Configuration.NameValueSectionHandler"/>
<section name="TestRunner" type="System.Configuration.NameValueSectionHandler"/>
<add key="DefaultLogThreshold" value="Debug" />
<add key="log4net.Internal.Debug" value="false"/>

This will instruct the interceptor of log4net to also watch out for Debug messages and add them to the log-tab. Just for the heck of it, I enabled the log4net internal logging in the app-settings (well actually I did turn it off, but I had it enabled in some point in time).

Leave a Comment.