views:

571

answers:

2

I'm writing a custom tracelistener to write trace messages to a file adding rotation (for example changing the file name each day).
In the override of the Flush method, I call Flush on the underlying StreamWriter, but if I set autoflush='false' in the application configuration, Flush does not get called when the application is terminated.

What is the correct method to finalize a custom trace listener when the application exits?

EDIT: I overrode the Dispose(bool disposing) method, since the base TraceListener class already implements the disposable pattern, but, as far as I can say, the method is not called. If I implement an explicit destructor, the destructor is called, but at that point I shouldn't assume that the stream reference is still valid, right?

A: 

I would suggest that you do this in the dispose method of your custom TraceListener class just before you close the underlying StreamWriter.

KevB
+1  A: 

Hi, if your software does not call to Trace.Close on termination then you cannot ensure any flush is done. See this article for more details.

jmservera