This project is read-only.

Getting Started

This project assumes that you are using the EventSource class in .NET 4.5 to implement Event Tracing for Windows (ETW), and that you are using the Semantic Logging Application Block included with Enterprise Library 6 to capture events and write them to a file. The Log4NetEventTextFormatter included in this project will format those events in Log4Net XML format so they can be consumed by any Log4Net viewer, such as Log4View or others.

To begin, we'll assume you've created a subclass of EventSource something like the following (taken from the Sample project in the Source Code):

class SampleEventSource : EventSource
  public static SampleEventSource Log = new SampleEventSource();

  [Event(1, Message = "Starting up.")]
  public void ApplicationStarting()

  [Event(2, Message = "Shutting down.")]
  public void ApplicationStopping()

  [Event(3, Message = "Here's something amazing.")]
  public void SomeOtherEvent(int param)
    this.WriteEvent(3, param);

  [Event(4, Message = "Oops a problem!")]
  public void Error(string exception)
    this.WriteEvent(4, exception);

  public void Error(Exception exception)

With SampleEventSource defined, the next step is to configure a listener to write events to a flat file using Log4Net formatting:

static void Main(string[] args)
var formatter = new Log4NetEventTextFormatter();
var listener = new ObservableEventListener();
listener.EnableEvents(SampleEventSource.Log, EventLevel.LogAlways, Keywords.All);
listener.LogToFlatFile("test.xml", formatter, true);
// ...

With this code in place you can begin logging events by calling into SampleEventSource as follows:


The resulting test.xml file will then contain the following:

<event logger="ApplicationStarting" timestamp="2013-05-02T13:09:03.8296469-04:00" level="INFO" thread="9" domain="Sample.vshost.exe" username="BLACK\Michael"><message>Starting up.</message><properties><data name="log4net:HostName" value="Black" /></properties></event>

Logging Exceptions

Log4Net's XML schema provides special treatment for exceptions via its <exception> element. To enable this, Log4NetEventTextFormatter examines the arguments passed to WriteEvent and looks for a string property named exception. If found, it will write that property's value into the Log4Net <exception> element. For example, consider the following code:

  throw new Exception("Uh oh, watch out");
catch (Exception e)

When run, this code will produce the following output in test.xml:

<event logger="Error" timestamp="2013-05-02T13:09:03.8646531-04:00" level="INFO" thread="9" domain="Sample.vshost.exe" username="BLACK\Michael"><message>Oops a problem!</message><properties><data name="exception" value="System.Exception: Uh oh, watch out&#xD;&#xA;   at Sample.Program.Main(String[] args) in d:\users\Michael\Documents\Visual Studio Projects\SemanticLog4Net\Sample\Program.cs:line 28" /></properties><exception>System.Exception: Uh oh, watch out
   at Sample.Program.Main(String[] args) in d:\users\Michael\Documents\Visual Studio Projects\SemanticLog4Net\Sample\Program.cs:line 28</exception></event>
<event logger="ApplicationStopping" timestamp="2013-05-02T13:09:03.8656512-04:00" level="INFO" thread="9" domain="Sample.vshost.exe" username="BLACK\Michael"><message>Shutting down.</message><properties><data name="log4net:HostName" value="Black" /></properties></event>

Last edited May 2, 2013 at 6:53 PM by korggy, version 4


No comments yet.