Wednesday, February 15, 2006 7:50 PM Erwyn van der Meer

Rolling File Trace Listener Extension for Enterprise Library 2.0

[Update 2006-04-18: Removed obsolete information from this blog entry. The latest information about the RollingFileTraceListener and the download link can always be found in this article on my blog.]

In January 2006 Microsoft released version 2.0 of the Enterprise Library. The Logging block now uses the trace listeners infrastructure of the .NET Framework 2.0 instead of "logging sinks". Read more about this change. The separate Configuration block is gone. Enterprise Library now builds on System.Configuration.

A problem with the 1.0 and 1.1 releases of the Enterprise Library was that these releases didn't ship with roll-over capabilities for log files. Log files continue to grow unchecked. Hisham Baz developed an extension for these versions called the Rolling File Sink. This custom logging sink allows you to set size and age limits for log files. I've blogged about a problem I had with this extension here and here.

Inspired by Olaf Conijn's Enterprise Library Extensions, I decided to create an EntLib 2.0 version of the Rolling File Sink. My version is called the Rolling File Trace Listener. You can find the documentation and the download link for the sources in this article on my blog. The source code and documentation is provided "AS-IS". Feel free to change and/or extend it.

The VS 2005 solution and projects are set up in such a way that is is easy to add additional extensions. They follow the structure of the EntLib 2.0 sources. My extension also comes with a test application to stress test the log file roll-over and the thread safety of the code. It logs 10,000 messages. First from a single thread and after that from dozens of threads simultaneously. Both test cases take a couple of seconds to run. You can edit the app.config file of the test application (either by hand or by using the Enterprise Library Configuration tool) to try out different settings.


Filed under:

# re: Rolling File Trace Listener Extension for Enterprise Library 2.0

Wednesday, February 15, 2006 6:12 PM by MuchNeeded

Thanks for this much needed TraceListener. You can be sure that someone will be testing it and putting it to good use eventually. I will provide you with any feedback as needed.

# re: Rolling File Trace Listener Extension for Enterprise Library 2.0

Thursday, February 16, 2006 5:36 AM by Alois Kraus

This is the number one most needed things that I miss in the released Enterprise Library. Very nice code by the way. You did invest quite some time in developing it.

Thanks for sharing it,

Alois Kraus

# re: Rolling File Trace Listener Extension for Enterprise Library 2.0

Friday, February 17, 2006 7:52 AM by Robert Jaakke

How funny, I was looking for this on the 14th of February and could find one so I started building my own. Just when i wanted to do a forum post about it I found this link.

I created a workspace for mine: http://www.gotdotnet.com/Workspaces/Workspace.aspx?id=113101bc-f269-4bb4-be05-3a43008f7019

# re: Rolling File Trace Listener Extension for Enterprise Library 2.0

Monday, February 20, 2006 2:27 PM by Erwyn van der Meer

@Robert. I am glad my test application inspired you to create a better test application for your extension. The code looks quite familiar ;)

# re: Rolling File Trace Listener Extension for Enterprise Library 2.0

Tuesday, February 21, 2006 5:41 AM by Robert Jaakke

Erwyn,

All i had was a sinle-threaded testing app and to be honest i haven't done much multi-threaded programming.

I hope you don't mind?

# re: Rolling File Trace Listener Extension for Enterprise Library 2.0

Tuesday, February 21, 2006 6:23 AM by Erwyn van der Meer

Robert, I don't mind if people modify or extend the code. I released the code into the public domain, so people are free to do this. But of course I would appreciate it if some form of acknowledgement is given if the modified code is released publicly.

# re: Rolling File Trace Listener Extension for Enterprise Library 2.0

Wednesday, February 22, 2006 3:49 AM by Vladmir

The idea is really really great, I'm wondering why patterns&practices group didn't create such trace listener, do they really think, that single trace file scenario is real?!
Anyway Erwyn, I just wanted to point out, that, I have not managed to hook up file formaters to this listener, output always contains full set of data whatever formater is. Am I missing something?

# re: Rolling File Trace Listener Extension for Enterprise Library 2.0

Thursday, February 23, 2006 7:13 AM by Erwyn van der Meer

Vladimir. Did you use the EntLib Configuration tool to hook up a formatter? I have tested the RollingFileTraceListener with a text formatter with a custom template that outputs less data. This works just fine. Make sure you change the application config file that is in the directory of the running application.

# re: Rolling File Trace Listener Extension for Enterprise Library 2.0

Tuesday, February 28, 2006 2:51 AM by Vladimir

Hi!
Hmm, this really is strange, I've created a new application, then I've created a new configuration file for it, using EntLib configuration tool. In configuration I've defined single RollingFileTrace listener and single Text formater for it with the following template:
Event Type: {severity}
Event ID: {eventid}{dictionary({newline}{key}: {value})}
DateTime: {timestamp}
Event Description: {message}

and all that is happening - I still receive full set of fields in the log file :/ can this be due to:
{dictionary({newline}{key}: {value})}
?

Well, I'll look into the source code, maybe this way I will find the cause of a problem.

# re: Rolling File Trace Listener Extension for Enterprise Library 2.0

Tuesday, February 28, 2006 9:53 AM by ai

@Vladmir, may sound silly but have you selected the right TextFormatter in the RollingFile TraceListener.

# re: Rolling File Trace Listener Extension for Enterprise Library 2.0

Wednesday, March 01, 2006 11:33 PM by Vladimir

Well I have only 2 formaters, both with the same formatting options :)

# re: Rolling File Trace Listener Extension for Enterprise Library 2.0

Tuesday, March 07, 2006 5:03 AM by Vladimirs

It seems to be, that when configuring listener using Enterprise Library Configuration tool "Formatter" setting is not saved into .config file, however if You put it there manually everything works perfectly.

# re: Rolling File Trace Listener Extension for Enterprise Library 2.0

Tuesday, March 07, 2006 6:30 AM by Vladimirs

Hi! It's me again :) seems, that I've found the way how to make everything work! Well it could be either workaround or a solution (hope Erwyn will clarify :))) ). So:
in RollingFileTraceListenerNode.cs I
1. removed "private string _formatterName;"
2. in constructor removed "this._formatterName = traceListenerData.Formatter;"
3. in "public override TraceListenerData TraceListenerData" replaced "this._formatterName" with "base.Formatter.Name"
now formatter name is always retreived from base, which is FlatFileTraceListenerNode.
......
But You know, I still have a feeling that I've missed something :/

# re: Rolling File Trace Listener Extension for Enterprise Library 2.0

Tuesday, March 07, 2006 2:59 PM by Erwyn van der Meer

Thank you very much Vladimir. You did indeed find the source of the problem with the formatter name not being saved correctly in the Enterprise Library Configuration Tool. I fixed this bug in a slightly different way than what you suggested because "this.Formatter" can be null. I updated the article to point to the new version 0.5.1.0.

# re: Rolling File Trace Listener Extension for Enterprise Library 2.0

Wednesday, March 08, 2006 7:43 AM by Vladimir

Well You are welcome! I'm glad to contribute to the development of such great extension!!!

# re: Rolling File Trace Listener Extension for Enterprise Library 2.0

Wednesday, March 08, 2006 2:40 PM by Alois Kraus

Hi Erwyn,

I did extend the sink with some small improvements.
1. Environment Variable expansion for the file name. So you can configure %LOGDIR%\LogFile.txt
2. ProcessID Expansion to support multi process logging
LogFile_$PID$.txt which is also expanded.

3. Call Flush after each write to ensure that all content is written to disk even when a Environment.FailFast() was called to deal with a fatal error inside the CLR.

Just some ideas to make a good sink even better.

Yours,

Alois Kraus

# Rolling, Rolling, Rolling...

Sunday, April 02, 2006 7:31 PM by Tom Hollander's blog

Ever since we first released Enterprise Library, a lot of people have wanted to be able to log information...

# re: Rolling File Trace Listener - Enterprise Library 2.0

Sunday, April 02, 2006 7:59 PM by @baz

# Version 0.5.2.0 of the RollingFileTraceListener extension

Friday, April 21, 2006 12:29 PM by Erwyn van der Meer

I forgot to mention this on my blog, but last Tuesday I released a new version 0.5.2.0 of my RollingFileTraceListener...