Watching the logs run with log4net

time to read 18 min | 3402 words

Logging is fairly critical to many applications, and it is especially critical when you are trying to understand in a complex application. I'm using log4net for logging, because it is so simple to start with and it can scale up to very complex needs.

The problem that I had was watching the logs. Specifically, watching the logs run in real time. The application is heavily mutli threaded, and run as a windows service. It is pretty hard to get what is going on there unless you watch the progress of the logs. At first, I simply logged to a file, and read it in notepad. This approach doesn't really scale well.

Next, I tried log4net viewer, which is really simple to get working. Just start the application, and put the following in your log4net config:

<appender name="UdpAppender"

                type="log4net.Appender.UdpAppender">

       <param name="RemoteAddress"

                 value="127.0.0.1" />

       <param name="RemotePort"

                 value="8080" />

       <layout type="log4net.Layout.XmlLayout">

              <param name="Prefix"

                        value="" />

       </layout>

</appender>

Here is what it looks like:

(Image from clipboard).png

The problem is that it is very simple, and it has several bugs that I couldn't live with (specifically, it doesn't display the information correctly if you are using sorting or filtering).

I then turned to log4j's chainsaw client, which is far more impressive feature wise. This is a java UI client, but it is very sophisticated. Almost to the point of being of no use to me, actually.

First, you need to put this in your log4net config:

<appender name="UdpAppender"

                type="log4net.Appender.UdpAppender">

       <param name="RemoteAddress"

                 value="127.0.0.1" />

       <param name="RemotePort"

                 value="8080" />

       <layout type="log4net.Layout.XmlLayoutSchemaLog4j, log4net" />

</appender>

Then, create a file called log4net.config.xml with the following content:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration

       xmlns:log4j="http://jakarta.apache.org/log4j/"

       debug="false">

       <plugin name="UDPReceiver"

                     class="org.apache.log4j.net.UDPReceiver">

              <param name="Port"

                        value="8080" />

       </plugin>

</log4j:configuration>

On startup, the chainsaw will request a configuration file:

(Image from clipboard).png

Specify the location of the log4net.config.xml file that you creted previous, select don't show me this again, and click OK.

Then, you get the main UI, you need to click on the chainsawl-log tab:

(Image from clipboard).png

You should get a screen similar to this:

(Image from clipboard).png

The part marked in red is the loggers tree, which will become important in just a minute. Right now it displays just chainsaw' own internal logs.

Now, start an application that will log to the UDP Appender, and watch the tree on the right, here is how it looks like after I run a bit of NHibernate's tests:

(Image from clipboard).png

Right click on the part of the tree that interests you and select focus on '...'

You can double click on the tab header to get the tab in a seperate window (useful for utilizing screen real estate):

(Image from clipboard).png

Chainsaw how powerful filterring and coloring features, and it is pretty good in allow me to check what my application is doing in real time.