Logging TraceListener

I'm working on a console app that needs to provide integrated logging of its own output. Sure, you could do a standard console output redirect, but I wanted the app to be responsible for logging its own output. I decided to write my own TraceListener that automatically creates IIS-style cyclic logfiles using the Trace method, like so:

This is a companion discussion topic for the original blog entry at: http://www.codinghorror.com/blog/2005/03/logging-tracelistener.html

Well, if it has the word “Enterprise” in it, it’s already kind of suspect :wink:

I haven’t worked with it much. I hear it’s very heavy and extremely configuration intensive.

Great bit of code.

I was thinking of doing the same sort of thing using the Logging block in the Enterprise Library. I found a ‘rolling file sink’ here:


Are you a fan of the Enterprise Library stuff yourself?

Ooh! Danke!

Hi. Thats some nice formatting for the code especially with the print and view plane. Are you using some formatting tool readily available or are you formatting this yourself?

If you click the “?” you’ll find a link to the source of the code. It is very cool – although the size of the code I posted is probably pushing the boundaries of what is sensible to do via JavaScript.

Very good!!! I had to put a synclock in the OpenLogFile. Could it be necessary in case of multi-threads applications?

SyncLock Me
_sw = File.AppendText(FileName)
_sw.AutoFlush = _AutoFlush
End SyncLock


First off thanks for sharing this code.

I tried using it in VS2005 and the close method kept erroring out on the closing of the stream. Not sure how to resolve this


Your code has me very useful, only has a small error in the following piece of code: “(? <=” & Name & “=) d +” you forgot the “” before the “d +” would look like this: '(<= “& name &” =) \ d + "