Monday, February 19, 2007

Seeing the log4net output when testing under TestDriven.NET

[Update 22.02.] It is also possible to use the log4net configuration for the service or executable that will be calling the dll under test. To to this three things have to be done:
  1. Link the App.config file of the service or executable to the UnitTests project
  2. Include a
    ILog sLogger = LogManager.GetLogger(typeof(IsmClientWatchdog));
    in the test class, even thought sLogger is never used therein.
  3. Add
    [assembly: log4net.Config.XmlConfigurator(Watch=true)]
    to the UnitTests project's AssemblyInfo.cs
Possibly 2. and 3. can be done with a single
XmlConfigurator.Configure()
[Original post]
I found a nice way to be able to see the log4net output when unit testing under TestDriven.NET:

log4net.Appender.ConsoleAppender app;

[TestFixtureSetUp]
public void Init()
{
app = new log4net.Appender.ConsoleAppender();
app.Layout = new log4net.Layout.PatternLayout("%d %C{1} [%-5p] : %m%n");
app.Threshold = log4net.Core.Level.All;
log4net.Config.BasicConfigurator.Configure(app);
}

[TestFixtureTearDown]
public void Dispose()
{
app.Threshold = log4net.Core.Level.Off;
}

No comments: