Search code examples
c#.netconfigurationtrace

Finalizing a custom TraceListener with autoflush='false' (C#, .net)


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?


Solution

  • 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.