views:

4556

answers:

2

This may be more of an OOP concept question, but here's what I'd like to do.

I have an application that outputs debug information using System.Diagnostics.Trace.WriteLine so it can be viewed with DebugView.

I'd like to override/extend (not sure of the proper terminology) this method to log the text to a file instead, or maybe in addition to the Trace output. This would allow me to write a new WriteLine method for my app, and I could leave all my other System.Diagnostics.Trace.WriteLine statements unchanged throughout the rest of the application.

So how would I go about changing the behavior of this method within my VB.Net app?

+4  A: 

Are you absolutely committed to still using Trace? If not, I'd use a more fully-featured logging system such as Log4Net.

However, if you really want to use Trace then you can reconfigure the TraceListeners used with an app.config file. The TraceListener MSDN docs give an example somewhat like this:

<configuration>
  <system.diagnostics>
    <trace autoflush="false" indentsize="4">
      <listeners>
        <add name="fileLogger" 
             type="System.Diagnostics.TextWriterTraceListener" 
             initializeData="LogFile.log" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

TextWriterTraceListener will dump logs to the given file. (There are other options available too.)

Alternatively, you can do this programmatically:

Trace.Listeners.Add(new TextWriterTraceListener("foo.log"));

Note that you may need to explicitly flush the traces before your app exits, either with:

Trace.Flush();

or the more complicated:

foreach (TraceListener listener in Trace.Listeners)
{
    listener.Flush();
}

(I only mention it because I had to when testing this!)

Jon Skeet
Isn't the iteration over the listeners + Flush equivalent to calling the static method Trace.Flush() ? Also, do you know how the client application can ensure that listeners are properly disposed?
Paolo Tedesco
You may well be right about Trace.Flush - not sure. As for disposing of the listeners - I'd hope that listeners don't need to be disposed in order to write their data.
Jon Skeet
Yup, Trace.Flush does the required business. Edited answer appropriately.
Jon Skeet
Yes, I was just wandering if it's the client's responsibility to dispose them. I'm trying to write a custom listener and some points are quite unclear (e.g. dispose never gets called).
Paolo Tedesco
I'd try to write the listener such that it doesn't matter - I think it's highly likely that you won't be disposed. Try not to rely on finalization either. That makes life somewhat harder, but there we go :( You might be able to listen to application termination (or AppDomain unloading) events and release resources then - but you should make sure that your listener doesn't die if it's then called after its own disposal!
Jon Skeet
Thank you very much for your comments :) I think I'll try to follow the pattern "do your best to cleanup and hope that the client behaves properly". I think in most cases autoflush will be set to true anyway.
Paolo Tedesco
+2  A: 

There is a TextWriterTraceListener which you can configure to output the trace log to a file. Configuration information can be found on the MSDN here:

http://msdn.microsoft.com/en-us/library/system.diagnostics.textwritertracelistener(VS.80).aspx

You can also dump to the event log or a variety of other places for a list of built in trace listeners you can look here:

http://msdn.microsoft.com/en-us/library/4y5y10s7(VS.80).aspx

jellomonkey