locked
trace Listeners RRS feed

  • Question

  • It looks like there are two methods for setting up trace listeners, Method 1 is:

     

    Declare an instance of TraceSource class

    Name the TraceSource

    Call the respective methods.

    Example:

     

    TextWriterTraceListener txtTracer = new TextWriterTraceListener(@"c:\newtxtlistener.txt");

    TraceSource DemoTrace = new TraceSource("DemoApp");

    DemoTrace.Listeners.Add(txtTracer);

    DemoTrace.Switch = new SourceSwitch("DemoApp.Switch", "Information");

    DemoTrace.TraceInformation("Before writing to Console");

    Console.WriteLine("Writing to the Console");

    DemoTrace.TraceInformation("After writing to Console");

     

    Or use Listener Objects

     

    Example:

     

    Trace.Listeners.Clear();

    Trace.AutoFlush = true;

    Trace.Listeners.Add(new TextWriterTraceListener(@"c:\newtxtlistener.txt"));

    Trace.WriteLine("This is a test");

     

     

    Does anyone know when to use one or the other, or is it just preference?

     

    Thanks

     

    Friday, July 13, 2007 3:44 PM

Answers

  • In the first case you are creating a new TraceSource.

    In the second case you are using the default TraceSource.

     

    The consideration should be for how you plan to use this application and particulary what flexibility do you want to have with regards to tracing. All the tracing options can be configured as well using the config file without the need to recomiple. Using the default TraceSource is the simple way to go but has some lack of flexibility. You will not be able to configure more than one TraceSource for your application if you go this way.

    Friday, July 13, 2007 3:50 PM