Is Trace.Close() really needed?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm building a Windows service that writes log messages via TraceListeners.
I assume Trace.Close() is useful for garbage collection, but I'm not sure
where to call it in the Windows service. Any thoughts?
 
Marc,

If you want to flush the contents of the trace to the file, then yes,
you do. And no, Close is not useful for garbage collection, as the
instances are not released.

In your windows service, you should probably call it when the service
shuts down, and when it pauses.

If you call Close, and you initialized the text file trace listener with
a filename, then it will reopen the file and write to it when another call
to Trace is made. If you pass a stream, then the stream is closed, and it
doesn't write to the stream again.

Hope this helps.
 
Thank you. I appreciate your insight. I will definitely use the file name
approach rather than the stream approach since it sounds more flexible.

Nicholas Paldino said:
Marc,

If you want to flush the contents of the trace to the file, then yes,
you do. And no, Close is not useful for garbage collection, as the
instances are not released.

In your windows service, you should probably call it when the service
shuts down, and when it pauses.

If you call Close, and you initialized the text file trace listener with
a filename, then it will reopen the file and write to it when another call
to Trace is made. If you pass a stream, then the stream is closed, and it
doesn't write to the stream again.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Marc said:
I'm building a Windows service that writes log messages via
TraceListeners.
I assume Trace.Close() is useful for garbage collection, but I'm not sure
where to call it in the Windows service. Any thoughts?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top