Recently a colleague of mine ran into the troublesome TextWriterTraceListener. As the name suggests, it's a trace listener that writes trace messages to a file. The way you use it, as any other listener, is as follows: either you instantiate the listener in code and add it to the static Trace.Listeners collection, or let the runtime do the same thing for you by saying so in App.config -- then you dispatch trace messages to this and other trace listeners hooked up, thru the static Trace class.
The TextWriterTraceListener and the Trace class work together just fine, as long as you don't try to use them from a finalizer when your application is terminating. You say "Trace.Writeline("Hi John, got some bad news for you!")" in the finalizer of your class which happens to run -- among other cases -- during application shutdown, and doh! -- you get a marvelous error saying that the FILE has already been closed. Isn't it just nice?
(The listener references a FileStream which has a finalizer of its own and finalizers run in no particular order: chances are your finalizer (calling into Trace) will be out of luck, calling into a dead trace listener. There are several ways in which you can make that listener do a better job, but I ended up not using Trace and TextWriterTraceListener at all. This is definitely a design flaw in both Trace and that particular listener. It should be fixed from the inside, I don't want to work around this).
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment