Home > .Net, Dev, Uncategorized > How to use Log4Net in Unittests

How to use Log4Net in Unittests

Curious why you unittest don’t log any messages using Log4Net? And you do have the configuration set in your app.config file? This is propably the result because your log4net config file isn’t deployed in the TestResult directory.

To enable Log4Net in your MSTest unittests :

  • Add your Log4Net file to the deployment files
  • Read the Log4Net configuration in your Assembly Initialize method (see below)

Add The Log4Net file to the deployment files

  1. Double click on the LocalTestRun.testrunconfig File
  2. Select deployment items
  3. Click Add
  4. Browse to your Log4Net config file.
    (This can be the same file as in your web or Windows project. In this way you use the same configuration as in your normal project.)

Reading Log4Net configuration
You can read the Log4Net configuration in you class initialization method but then you will have to put this information in every unittest class in you assembly. A better way is to put this code in the AssemblyInitialize method. The AssemblyInitialize method is just like the ClassInitialize method but this method will be executed only once before the execution of the unittest in your assembly.

Create a new class, for example TestAssemblyInit.cs and add the code below

[AssemblyInitialize()]
public static void MyTestInitialize(TestContext testContext)
{
// Take care the log4net.config file is added to the deployment files of the testconfig
FileInfo fileInfo;
string fullPath = Path.Combine(System.Environment.CurrentDirectory, "log4net.config");
fileInfo = new FileInfo(fullPath);

// Reload the configuration
log4net.Config.XmlConfigurator.Configure(fileInfo);

// test to see if it works
Log.Info("check");
}

Categories: .Net, Dev, Uncategorized Tags: , ,
  1. JeroenH
    April 1st, 2009 at 08:30 | #1

    You could probably also use
    [assembly: log4net.Config.XmlConfigurator()]
    in the AssemblyInfo.cs file.

    This has worked for me, though you sometimes indeed still need to explicitly deploy the log4net.dll to the unit test’s bin directory, if you do not call methods in the log4net assembly directly from the unit test code.

  2. April 3rd, 2009 at 00:28 | #2

    @JeroenH
    The problem is that the log4net config isn’t deployed in the test files directory, no matter what setting you configure for this file. In this way the configuration isn’t read and no messages are written to your log.

  3. March 31st, 2010 at 08:40 | #3

    You can add the code to your existing test classes. I was a bit confused because I thought I needed to add a NEW class to put the code in.

    It turns out all I had to do was add the lines of code above inside the Test Class or Test Classes already existing in the project.

    Do not forget to add a a using statement to System.IO as well as using statements to log4net.Config; and log4net; for the class to behaive properly.

    I am using VS2010 and .net 4.0 and logging is working now.

    Thanks again.

  1. No trackbacks yet.