Configuring simple TraceSource for logging purposes

By Mirek on (tags: c#, logging, .net, TraceSource, tracing, categories: code)

In the previous post we saw how to use the built in Trace class for logging facilities. However the Trace class is not the latest Microsoft’s recommended way for logging your application. In this post I will try to present the quickest way of configuring TraceSource and use it for logging purposes.

TraceSource class allows you to create as many trace sources as you want and freely configure them to output tracing data to as many listeners as you need. However for the sake of simplicity we will try to configure only one TraceSource and make it saving the log information to the text log file.
First we need to create an instance of the TraceSource class.

public static TraceSource traceSource = new TraceSource("TraceSourceApp");

We have to give it a name which will be leater referenced in the configuration file. Of course there is a possibility to configure all this stuff in the code, but this is more convenient and useful to have the configuration in app.config or web.config file.

   1: <system.diagnostics>
   2:   <trace autoflush="true"/>
   3:   <sources >
   4:     <source name="TraceSourceApp"  switchName="myswitch" >
   5:       <listeners>
   6:         <add name="textWriterListener" traceOutputOptions="DateTime" 
   7:              type="System.Diagnostics.TextWriterTraceListener"
   8:              initializeData="myApp.log">
   9:         </add>
  10:         <remove name="Default" />
  11:       </listeners>
  12:     </source>
  13:   </sources>
  14:   <switches>
  15:     <add name="myswitch" value="Verbose" />
  16:   </switches>
  17: </system.diagnostics>

We have to add above section to the configuration file in order to have the TraceSource working properly. First (line 2) we define the autoflush property to true, which means all events traced will be immediately sent to the listeners, and saved to the log file in our case. Next, in line 4, we define the trace source which corresponds to the one created in the code by name.

Here we also have to define the TraceSwitch for the trace source and the collection of listeners. We only defined one listener, the TextWriterTraceListener, which writes the messages to the text file myApp.log and attach the date and time to each message. The default listener is removed here in line 10.

Next in switches node we define the trace source switches which simply define the maximum level of message types that will be processed by the trace sources. Here the value is set to the lowest level Verbose. That means all messages will be processed by TraceSourceApp and redirected to the listeners. Possible values for trace switch are defined in details here, but the most common are: Error, Warning, Information and Verbose.

For each listener we can also define the EventTypeFilter. For example if we want to have only errors to be written in system event log, and all trace to the text file, then we can define an event type filter with initializeData="Error"  on the event log listener so the only error messages will be accepted by this listener.

We can have a quite complex combination of event type filters together with custom trace listeners, trace sources and trace switches. However this, simple example would fit to most of applications I guess, so let’s see how we can use it in the code.

Having the static traceSource available we can:

1. Trace error

traceSource.TraceEvent(TraceEventType.Error, 0, "Fatal error occured");

2. Trace error message with exception details

try
{
   [...]
}
catch (Exception ex)
{
    traceSource.TraceEvent(TraceEventType.Error, 0, "Fatal error occured");
    traceSource.TraceData(TraceEventType.Error, 0, ex);
}

3. Trace warning messages

traceSource.TraceEvent(TraceEventType.Warning, 0, "Some strange warning message");

4. Trace a information

traceSource.TraceInformation("info message");

5. Trace debug information

traceSource.TraceEvent(TraceEventType.Verbose, 0, "Some debug message");

 

And we get that kind of log file

TraceSourceApp Information: 0 : info message
    DateTime=2012-10-17T12:09:27.0047762Z
TraceSourceApp Warning: 0 : Some strange warning message
    DateTime=2012-10-17T12:09:27.0047762Z
TraceSourceApp Verbose: 0 : Some debug message
    DateTime=2012-10-17T12:09:27.0047762Z
TraceSourceApp Error: 0 : Fatal error occured
    DateTime=2012-10-17T12:09:27.0447815Z
TraceSourceApp Error: 0 : System.NullReferenceException: Object reference not set to an instance of an object.
   at WPFTest.MainWindow.Button_Click_1(Object sender, RoutedEventArgs e) in d:\research\WPFTest\WPFTest\MainWindow.xaml.cs:line 40
    DateTime=2012-10-17T12:09:27.0467819Z

 

Piece of cake, isn’t it? Puszczam oczko