blog.vorpal.cc

Hi! My name is David Hogue. I write code in Bend, Oregon.

April 21, 2006

How To Get log4net Messages to Show Up In NUnit’s Console.Out

Filed under: — David @ 3:48 pm

I've been using log4net (a port of log4j to .net) for a while now. I've always loved how configurable it was: I could have it send me email, log to xml, a database, whatever.

I've also been using NUnit to do TDD. Sometimes when a test crashes I would love to have the log output to work with. Often, since I didn't have the logs, I would use TestDriven.NET's test with debugger feature and start debugging. Lately I've been trying to get away from that as I think it will lead to better tests.

Anyway, I needed a way to view the log in NUnit's gui or the TestDriven.NET window. I figured I would share what I came up with in the hopes that someone will find it useful. It's written in vb.net because that is what the project was using. It's all in code since I didn't want my tests dependant on an external config file.

Note: If you have log4net configured this code will alter the logging configuration for the rest of the test run.

VB.NET:
  1. Imports NUnit.Framework
  2. Imports log4net
  3.  
  4. <testfixture ()> _
  5. Public Class Log4NetInNUnit
  6.  
  7.     Private appender As Appender.ConsoleAppender
  8.     Private Shared log As ILog = LogManager.GetLogger(GetType(ExcelBugs))
  9.  
  10.     <testfixturesetup ()> _
  11.     Public Sub InitLog()
  12.         appender = New Appender.ConsoleAppender()
  13.         appender.Layout = New Layout.SimpleLayout()
  14.         appender.Threshold = log4net.Core.Level.All
  15.         Config.BasicConfigurator.Configure(appender)
  16.     End Sub
  17.  
  18.     <testfixtureteardown ()> _
  19.     Public Sub CleanupLog()
  20.         appender.Threshold = log4net.Core.Level.Off
  21.     End Sub
  22.  
  23.     <test ()> _
  24.     Public Sub TestLogs()
  25.         log.Info("test")
  26.     End Sub
  27. End Class

5 Responses to “How To Get log4net Messages to Show Up In NUnit’s Console.Out”

  1. Thanks for this post. It was useful to me :)

  2. Thanks a lot, very useful to me as well!!

  3. This is exactly what I was looking for. Thanks for sharing.

  4. [...] This blog has basic instructions of how to integrate log4net (in VB.Net though). [...]

  5. I tried to call “XmlConfigurator.Configure(new FileInfo(log4net config file path))” which turn out not to work.
    This approach works very well~ Thanks

Leave a Reply