Click here to Skip to main content
14,662,846 members
Articles » Languages » C# » Applications
Posted 8 Nov 2008


165 bookmarked

C# WPF Log4Net Viewer

Rate this:
4.87 (29 votes)
Please Sign up or sign in to vote.
4.87 (29 votes)
15 Oct 2009CPOL
Log4Net XML log viewer


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" />
      <layout type="log4net.Layout.XmlLayoutSchemaLog4j">
        <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 
	Uncompress: 117,241,190</log4j:message>
    <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" 

		file="C:\myApp\myFile.cs" line="266"/>

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
    • Release note
      • New features
        • Search text message
        • Errors count dashboard
      • Bug fixes
        • Filters


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ken C. Len
Software Developer (Senior) LEN Associates Inc.
United States United States
Years of software consulting and software development using Microsoft development products such as Microsoft Content Management System, SQL Server Reporting Service, ASP.Net C# VB.Net, HTML and javascript web development, Visual Studio add-on development, C++ MFC/ATL and COM+ development, and ActiveX components.

Comments and Discussions

SuggestionLog4Net .log and .txt file Viewer online Pin
Member 1167065416-May-20 4:46
MemberMember 1167065416-May-20 4:46 
QuestionMore Viewer Pin
kiquenet.com12-Jun-15 1:08
professionalkiquenet.com12-Jun-15 1:08 
QuestionIncluded in this source zipped is the log4net helper wrapper class log.cs file. Pin
Member 108071751-Jun-14 4:00
MemberMember 108071751-Jun-14 4:00 
GeneralMy vote of 5 Pin
Júlio Nobre31-Oct-12 13:03
MemberJúlio Nobre31-Oct-12 13:03 
QuestionIs it posible to run this LogViewer project on the internet? Pin
Member 361632614-Aug-12 8:59
MemberMember 361632614-Aug-12 8:59 
AnswerRe: Is it posible to run this LogViewer project on the internet? Pin
Ken C. Len15-Aug-12 0:51
MemberKen C. Len15-Aug-12 0:51 
GeneralRe: Is it posible to run this LogViewer project on the internet? Pin
openshac8-Feb-13 5:44
Memberopenshac8-Feb-13 5:44 
Questionlog.xml Pin
chaitu19850214-Mar-12 0:21
Memberchaitu19850214-Mar-12 0:21 
AnswerRe: log.xml Pin
Ken C. Len19-Mar-12 14:13
MemberKen C. Len19-Mar-12 14:13 
QuestionProvide Log4Net Viewer as asp .net website Pin
londhess20-Feb-12 0:43
Memberlondhess20-Feb-12 0:43 
GeneralExcellent App! Pin
jsdesai18-Jan-12 0:43
Memberjsdesai18-Jan-12 0:43 
GeneralMy vote of 2 Pin
mungflesh3-Jan-11 23:24
Membermungflesh3-Jan-11 23:24 
GeneralExtended version Published on CodePlex Pin
KNH Prod30-Jun-10 2:04
MemberKNH Prod30-Jun-10 2:04 
QuestionExtended New Version of LogViewer / how to publish it ? Pin
KNH Prod25-Jun-10 4:28
MemberKNH Prod25-Jun-10 4:28 
Generaladding Context Properties Pin
Claudio Nardi18-May-10 23:28
MemberClaudio Nardi18-May-10 23:28 
AnswerRe: adding Context Properties Pin
Ken C. Len19-May-10 8:07
MemberKen C. Len19-May-10 8:07 
GeneralEncoding Fix Pin
teichgraf29-Oct-09 5:12
Memberteichgraf29-Oct-09 5:12 
GeneralExcellent! Pin
Frank Gennaro 24872199-Sep-09 22:40
MemberFrank Gennaro 24872199-Sep-09 22:40 
GeneralGreat project! Pin
Daniel Carvalho Liedke5-Jun-09 11:50
MemberDaniel Carvalho Liedke5-Jun-09 11:50 
QuestionCan you update app? Pin
abmv28-May-09 3:03
professionalabmv28-May-09 3:03 
AnswerRe: Can you update app? Pin
Remy Baudet30-Jan-10 14:08
MemberRemy Baudet30-Jan-10 14:08 
GeneralLoved it a lot Pin
jpsstavares18-Nov-08 3:40
Memberjpsstavares18-Nov-08 3:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.