Paolo Tedesco
Paolo Tedesco

Reputation: 57222

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?

Upvotes: 2

Views: 2698

Answers (3)

jmservera
jmservera

Reputation: 6682

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.

Upvotes: 4

fretje
fretje

Reputation: 8382

You should override the Close method in your custom tracelistener, and Flush and Dispose your underlaying StreamWriter there.

Upvotes: 0

KevB
KevB

Reputation: 3690

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

Upvotes: 0

Related Questions