Thursday, October 22, 2009

.NET trace log implementation with EventLog and TextStream support

When you are developing an application you want to be able to log what is happening in your code.
In .NET you can accomplish that with the use of TraceListeners.
You use the Trace class to write messages during the execution of your application.
While in debug mode, these messages will be displayed in the Visual Studio output console and forwarded to the configured tracelisteners.
The TraceListener can then write the message to a destination like an EventLog, Xml file or a textfile.
One cool thing about TraceListeners is that you can configure them in your App.config or Web.config, so you can control their settings after you have built the application.

I think this is a great tool for implementing logging in your application, but the default implementation has a few setbacks:

1) TraceListeners intercept all trace messages and i would like to configure them so they intercept only those messages that have a specific priority level.
Eg. i want high priority messages to be logged in an eventlog and i want debug messages to be logged in a textfile.
So when i use a release build and something goes wrong, i can activate the TraceListener that writes debug info to a logfile and find out what is happening.

2) The EventLogTraceListener outputs to the Application log and i would like to have my events in a seperate log.

So to solve these problems i wrote my own tracelisteners.
It would be great if Microsoft was so kind to let us inherit from their default tracelistener implementations like EventLogTraceListener and TextStreamTraceListener, but for some reason they decided to seal these classes.
Therefore we have to inherit from the TraceListener class, do our custom stuff and pass the calls to an instance of, for example, EventLogTraceListener.
I wrote two custom tracelisteners: CustomEventLogTraceListener and CustomTextStreamTraceListener.

In my implementation i also wrote a CustomTrace class that acts as a wrapper for the Trace class and adds the possibility of specifying priority levels for trace messages.
The lowest priority is 1 and the highest is 9.
So instead of calling Trace.TraceError("This is wrong") you can call CustomTrace.TraceError("This is wrong", 9) which means this error is of priority level 9, so it is very important.

I created an example solution with the TraceListeners project and an example application.
It shows how to configure the listeners and how to send information (messages) to them.

I will be using this in all my .NET implementations from now on and i hope it comes in handy for you too!

/Ruud

Wednesday, October 21, 2009

Maintenance

Downloads and code highlighting component are temporarily disabled.
They are all hosted on the DotTech domain which is offline for maintenance.
I try to have them back online this weekend.

EDIT: Server is back up!

/Ruud

Tuesday, October 13, 2009

ScottGu's VS2010 and .NET 4 Series

I'd like to point out a nice series of blogposts from Scott Guthrie on Visual Studio 2010 and .NET 4.0
For those of you who don't know Scott Gu (you are a rare breed): he runs the development teams for ASP.NET, CLR, IIS7, Windows Presentation Foundation and some other MS projects.

His blog series about VS2010 and .NET 4.0 is good reading material for those who want to be somewhat prepared when the software is released.

I will update this post when he publishes more articles.
Enjoy!

/Ruud