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.

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.

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

2. Trace error message with exception details

3. Trace warning messages

4. Trace a information

5. Trace debug information

 

And we get that kind of log file

 

Piece of cake, isn’t it?