See log4net log messages in real-time

This post shows how to make a simple addition to your log4net.config file to make it possible to view log4net messages in real-time.

  • 18 October 2011
  • 0

Different goals of log4net logging

I recently put out a post on how to configure log4net for more readable log files, which is helpful for creating neatly organized log files with one file per day.

Although that’s helpful when you want to go back and check what has happened at a particular time, it doesn’t do much for figuring out what a website is doing right now.

In a default EPiServer setup, log4net is configured to use the fileLogAppender to output logging information to a text file on disk. With some modifications (see the post I mentioned earlier) that’s fine for staging and production environments.

However, in development and debugging scenarios it’s quite useful to be able to follow the log4net logging in real-time without having to constantly refresh the contents of a log file.

Luckily, log4net supports having multiple appenders active simultaneously.

Using OutputDebugStringAppender

The OutputDebugStringAppender appender type is used to send all log4net messages to the debugger. Essentially, it’s as if you’d take all log4net messages and emit them using Debug.Write().

Here’s how to add OutputDebugStringAppender to your log4net.config file:

<appender name="OutputDebugStringAppender" type="log4net.Appender.OutputDebugStringAppender" >
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="[MySite] %level %date{HH:mm:ss,fff} - %message%n" />

Note that I’ve added a message prefix of “[MySite]” to make our application’s debug messages easier to find (more on this below). You can obviously adjust the conversionPattern to fit your preferences (here’s some information on available conversionPattern options).

Listening to log4net in real-time

I use a tool called DebugView for displaying debug messages.

Make sure to run DebugView as an administrator (this is key, DebugView won’t display anything otherwise). Also, make sure no other debuggers are attached (such as the Visual Studio debugger) as they will “hijack” any debug messages being emitted, causing DebugView not to show any messages.

Capture settings vary depending on the context in which the application you want to debug is running, so make sure to check both the Capture Win32 and the Capture Global Win32 options on the Capture menu:

Configuring Capture settings for DebugView

DebugView can potentially show a lot of debug messages from other applications. This can cause quite a bit of noise making it difficult to spot the messages coming from your particular application, so you may want to add a filter for the message prefix we added in log4net.config earlier (available under Edit -> Filter/Highlight):

Setting message filters in DebugView

Now if we browse our website we’ll see debug messages pop up in DebugView in real-time:

Log4net messages in real-time in DebugView

Thanks Thomas Krantz for finding the OutputDebugStringAppender!