Log4Net logging utility is very useful for logging exceptions and also logging debug statements. However, there is a need for a good viewer application to view the logs. The project here is an application that allows a user to open and view log4net XML formatted output with color coded backgrounds. The application is written entirely in C# with Windows Presentation Foundation (WPF). So far, there isn't any C# version out there to view Log4Net output log files, so this is a cool utility to have when viewing Log4Net log files looking for bugs and traces. The reason WPF was chosen for this application is because first, WPF is new, second, to demonstrate how easy it is to write a WPF application, and third, to test how many dependencies a WPF application has compared to WinForm applications.
This application was inspired by the Java version Log4Net viewer Chainsaw. For some of us who don't have Java runtime installed on our PC, I thought it would be nice to have a C# version of the viewer.
The log4net viewer application reads and parses XML output generated by Log4Net logging utility. The output format must conform to the log4j schema. To generate log4j XML format out, set the application configuration of your app to use the XML layout schema. This configuration setting is in the “app.config” or “web.config” file.
First, create a
configSections and then a
log4net section. The following example uses a rolling file appender and log4j schema layout to set the XML log format.
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="c:\log\log.xml" />
<appendToFile value="true" />
<datePattern value="yyyyMMdd" />
<rollingStyle value="Date" />
<locationInfo value="true" />
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
The layout tag
log4net.layout.XmlLayout.SchemaLog4j tells log4net.dll to log in XML format. The XML format ensures that the viewer application can read the output content. The output content is parsed and loaded into a List View or Grid View control complete with sorting and background color codes for each log type. There is also a file drag and drop feature using the Grid View control.
Using the Application
You can drag and drop your log file into the viewing area, in this case the
ListView control. You may also use the menu file open or if you already opened a file previously, then use the most recent viewed menu item. Menu Item refresh will reload the current loaded log file. When a file is loaded, the application parses the XML in the log file.
Parsing the XML is very simple by using .NET XML text reader. XML text reader reads each event logged in the XML file with a structure like the following:
<log4j:event logger="MyApp" timestamp="1219434607289" level="INFO" thread="4656">
<log4j:message>Loading my data Compressed: 8,967,102
<log4j:data name="log4jmachinename" value="myMachine"/>
<log4j:data name="log4net:HostName" value="myHost"/>
<log4j:data name="log4net:UserName" value="myUser"/>
<log4j:data name="log4japp" value="myAppexe"/>
<log4j:locationInfo class="myClass" method="myMethod"
All elements in the XML are parsed and stored in a class object called
LogEntry. Each log event creates a
LogEntry object and is added to the collection
List<LogEntry> entries. The list collection is used as data bind for the List View control.
Log4net Wrapper Class
Included in this source zipped is the log4net helper wrapper class log.cs file. The log4net wrapper class provides a few
static methods to simplified logging exceptions. The wrapper
static method ensures that the log4net configuration is setup correctly.
Following is an example on how to use the wrapper class:
catch (Exception ex)
Simplified application exception logging is a good thing, however it is better when you have a log viewer.
- 8th November, 2008: Initial post
- 14th October, 2009: New version 126.96.36.199
- Release note
- New features
- Search text message
- Errors count dashboard
- Bug fixes