Home OFC Tech (blog) Configure Log4Net with NUnit

Configure Log4Net with NUnit

E-mail Print
The Problem:

When you use the NUnit grapical interface to run unit tests, it short circuits the output from log4net.  This happens because NUnit itself is using log4net, and by default it turns the log outputs off.  The end result of this is that you go through all this trouble to get log4net working correctly, and your log entries mysteriously fail to show up when you run the unit tests.  This affect both console logging, and logging to files.

 

The resolution:

There is definitely some information out there, but most of what I found was a touch cryptic.  The purpose of this entry is share a plain English way of working around this problem.

 

Step 1: Add an application config file and rename it to something like "Test.config".  Don't be hard headed like me and ask "Why do I need to rename this file".  Just do it and make sure that the config file you add has the appropriate build action set so it gets copied to the bin directory as content.

Nunit_testconfig

 

Step 2: Add your log4net configuration entry to the "Test.config" file.  If you're reading this article, you should already know how to do this.  But if you don't, please refer to the log4net setup documentation.

 

Step 3: Add code to your unit test project to configure log4net at runtime (i.e. after the NUnit GUI has messed with the configuration).  The code snippet below does it in the constructor of one of my test classes.  This has the obviously limitation of only firing our configuration code once a test in this particular class is fired.  Optional: If you want a more global version of this approach that applies to all of your tests classes, embed the runtime log4net configuration in a class marked as being a [SetupFixture] instead of placing in an actual test class.

 

[TestFixture]
public class LoggingTests
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

public LoggingTests()
{
FileInfo fileInfo = new FileInfo(@"C:\MOHQ\Middleware\UnitTests\bin\Debug\Test.config");
log4net.Config.XmlConfigurator.Configure(fileInfo);
}

[Test]
public void BasicLogTest()
{
log.Error("Hmm, Write my log entry already");
}

[Test]
public void DatabaseLogTest()
{

}
}

 

At this point your done.  Any log4net entries you have configured should now show up in the appropriate logs.


blog comments powered by Disqus
Last Updated ( Monday, 05 April 2010 14:55 )  

ID Card Printing

WorkForce ID cards are custom designed to reflect you unique business, brand or identity. Click to view our product brochure

WorkForceID_Marketing_v1.11

We also have specific offerings tailored to small businesses and individual entrepreneurs.


Certifications

Oracle Partner Logo

MS Partner Logo

Our Partners

The Gravely Group
INCREASE CDC
ECDI - Columbus
DMW Computers

Our Products

Sample imageNeighborhood Shield Drive
WorkForce ID
My Property Tag

Our Services

Application Development
Website Design
Flash Multimedia Design
Ecommerce & Storefronts
Card Printing & Plastics