DotCom’s Blog Space

Rolling File TraceListener

by DotCom on Apr.04, 2008, under Engineers Corner

Ever been in a situation you needed to use the logging mechanisms of .Net’s System.Diagnostics and the file output from TextWriterTraceListener became huge and theres not a lot of control over it.   Well recently I created a custom listener to overcome this problem.   To use the custom class all you have to do is specify the FQN and the DLL it lives in, in the type field of the listener in the config.   This is a modified version of TextWriterTraceListener as just inheriting the class still was not allowing the filename attributes to be accessed.  Two added attributes that you can specify in your config file is maxfilesize and numrollovers to have more control over the files and there size.   Default in the code is 10000000 and 10 respectively.  The main difference in this custom class is the method: IsFileSizeReached(string filename) This is used and checked from the Ensure Writer and checks to see if you had specified a file as output.  To use custom attributes if you so choose to change this you can look at the overridden method: GetSupportedAttributes()

I have attached the class file for anyone who wishes to have a sample or play around with it.

Another thing to note is that this class still allows you to pass custom stream or textwriters for those that use those.

RollingTextWriterTraceListener.txt


Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...