Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

C# WPF Log4Net Viewer

0.00/5 (No votes)
15 Oct 2009 3  
Log4Net XML log viewer

Introduction

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.

LogViewer.JPG

Background

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.

Requirement

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.

<configuration>
  <configSections>
    <section  name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, 
	log4net"/>
  </configSections>
  <log4net>
    <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" />
      <layout type="log4net.Layout.XmlLayoutSchemaLog4j">
        <locationInfo value="true" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingFileAppender" />
    </root>
  </log4net>
</configuration>

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

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 
	Uncompress: 117,241,190</log4j:message>
  <log4j:properties>
    <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:properties>
  <log4j:locationInfo class="myClass" method="myMethod" 
		file="C:\myApp\myFile.cs" line="266"/>
</log4j:event>

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:

try
{
…
}
catch (Exception ex)
{
  Log.Error(ex);
}

Conclusion

Simplified application exception logging is a good thing, however it is better when you have a log viewer.

History

  • 8th November, 2008: Initial post
  • 14th October, 2009: New version 2.1.0.0
    • Release note
      • New features
        • Search text message
        • Errors count dashboard
      • Bug fixes
        • Filters

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here