How to: Create and Initialize Trace Listeners 

The Debug and Trace classes send messages to objects called listeners that receive and process these messages. One such listener, the DefaultTraceListener, is automatically created and initialized when tracing or debugging is enabled; For more information, see Trace Listeners. If you want Trace or Debug output to be directed to any additional sources, you must create and initialize additional trace listeners.

The listeners you create should reflect your individual needs. For example, you might want a text record of all trace output. In this case, you would create a listener that wrote all output to a new text file when enabled. On the other hand, you might only want to view output during application execution. In that case, you might create a listener that directed all output to a console window. The EventLogTraceListener can direct trace output to an event log, and the TextWriterTraceListener can write trace output to a stream.

To create and initialize your trace listener

  1. Declare your trace listener. If the particular listener you are creating requires any other objects, declare them as well. The following example shows how to create a listener that writes to a text file:

    ' Creates the text file that the trace listener will write to.
    Dim myTraceLog As New System.IO.FileStream("C:\myTraceLog.txt", _
       IO.FileMode.OpenOrCreate)
    ' Creates the new trace listener
    Dim myListener As New TextWriterTraceListener(myTraceLog)
    
    // Creates the text file that the trace listener will write to.
    System.IO.FileStream myTraceLog = new 
       System.IO.FileStream("C:\\myTraceLog.txt", 
       System.IO.FileMode.OpenOrCreate);
    // Creates the new trace listener.
    System.Diagnostics.TextWriterTraceListener myListener = 
       new System.Diagnostics.TextWriterTraceListener(myTraceLog);
    
  2. Emit the trace output.

    • If you want your listener to receive all trace output, add your trace listener to the Listeners collection.

      The following example shows how to add your listener to the Listeners collection:

      Trace.Listeners.Add(myListener)
      
      System.Diagnostics.Trace.Listeners.Add(myListener);
      

      - or -

    • If you do not want your listener to receive trace output, do not add it to the Listeners collection. You can emit output through a listener independent of the Listeners collection by calling the listener's own output methods. The following example shows how to write a line to a listener that is not in the Listeners collection:

      myListener.WriteLine( _
         "This output will not go to the Listeners collection")
      
      myListener.WriteLine( 
         "This output will not go to the Listeners collection");
      
  3. If your listener is not a member of the Listeners collection, it may be necessary to call the Flush method to record your output.

    ' Flushes the buffers of all listeners in the Listeners collection.
    Trace.Flush()
    ' Flushes only the buffer of myListener.
    myListener.Flush()
    
    // Flushes the buffers of all listeners in the Listeners collection.
    System.Diagnostics.Trace.Flush();
    // Flushes only the buffer of myListener.
    myListener.Flush();
    

See Also

Tasks

How to: Trace Code in an Application
How to: Add Trace Statements to Application Code

Concepts

Trace Listeners
Trace Switches

Other Resources

Tracing and Instrumenting Applications