[Mono-dev] how to fix TextWriterTraceListener disposal problem

Atsushi Eno atsushieno at veritas-vos-liberabit.com
Thu Apr 14 23:36:09 EDT 2011


Hey,

(2011/04/15 6:31), Jonathan Pryor wrote:
> On Apr 13, 2011, at 4:06 AM, Atsushi Eno wrote:
>> Do you have any ideas how to solve this problem?
> Is it actually a problem? :-)

Sadly yes... it blocks us from creating useful WCF diagnostic xml logs.

> The reason I ask is that I vaguely recall testing the same behavior under .NET at the time (circa 2002?), and .NET behaved the same way -- the stream was not flushed.

I've never seen such a problem in .NET 3.0, 3.5 and 4.0. MS probably 
fixed it.

> Furthermore, the only solution I know of to fix this (or knew of circa 2002), was to use a finalizer on TraceListener, which MSDN states TraceListener does not provide.

As mentioned in my first post, the finalizer approach doesn't work. The 
Stream object could be already finalized before the TextWriter is disposed.

> Consequently, I believe that TraceListener is not supposed to flush the stream at all, and if you really want the stream to be flushed you should set Trace.AutoFlush=true, either programmatically or via .config file; MSDN kindly provides the .config file snippet here:
>
> 	http://msdn.microsoft.com/en-us/library/system.diagnostics.tracelistener.aspx
>

Autoflush does not work for XmlWriterTraceListener, because its 
Dispose() calls XmlWriter.Close() which gives additional outputs 
(enclosing EndElements) to the TextWriter. Here again, the TextWriter 
could be GC-ed before XmlWriter is being disposed.

Though if there is no viable approach, I'd take another approach, to 
create my own critical-finalizable Stream, TextWriter and XmlWriter 
which refers each other. I wanted to confirm there is no easier way, as 
it is messy :/

Atsushi Eno



More information about the Mono-devel-list mailing list