Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

TraceListener Class

Provides the abstract (MustInherit in Visual Basic) base class for the listeners who monitor trace and debug output.

For a list of all members of this type, see TraceListener Members.

System.Object
   System.MarshalByRefObject
      System.Diagnostics.TraceListener
         System.Diagnostics.DefaultTraceListener
         System.Diagnostics.EventLogTraceListener
         System.Diagnostics.TextWriterTraceListener

[Visual Basic]
MustInherit Public Class TraceListener
   Inherits MarshalByRefObject
   Implements IDisposable
[C#]
public abstract class TraceListener : MarshalByRefObject,
   IDisposable
[C++]
public __gc __abstract class TraceListener : public
   MarshalByRefObject, IDisposable
[JScript]
public abstract class TraceListener extends MarshalByRefObject
   implements IDisposable

Thread Safety

This type is safe for multithreaded operations.

Remarks

For examples of how to implement a TraceListener, see the DefaultTraceListener, TextWriterTraceListener and the EventLogTraceListener classes.

You must enable tracing or debugging to use a switch. The following syntax is compiler specific. If you use compilers other than C# or Visual Basic, refer to the documentation for your compiler.

  • To enable debugging in C#, add the /d:DEBUG flag to the compiler command line when you compile your code, or you can add #define DEBUG to the top of your file. In Visual Basic, add the /d:DEBUG=True flag to the compiler command line.
  • To enable tracing using in C#, add the /d:TRACE flag to the compiler command line when you compile your code, or add #define TRACE to the top of your file. In Visual Basic, add the /d:TRACE=True flag to the compiler command line.

To set the level of your listener, edit the configuration file that corresponds to the name of your application. Within this file, you can add a listener, set its type and set its parameter, remove a listener, or clear all the listeners previously set by the application. The configuration file should be formatted similar to the following example:

For this code example to run, you must provide the fully qualified assembly name. For information about how to obtain the fully qualified assembly name, see Assembly Names.

<configuration>
<system.diagnostics>
    <switches>
        <add name="MagicTraceSwitch" value="3" />
    </switches>
    <trace autoflush="false" indentsize="4">
        <listeners>
           <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\myListener.log" />
           <!-- You must supply a valid fully qualified assembly name here. --> 
           <remove type="Assembly text name, Version, Culture, PublicKeyToken"/> 
        </listeners>
    </trace>
</system.diagnostics>
 </configuration>

Notes to Inheritors:  Inherit from this class to implement a custom listener for the Debug and Trace classes. At a minimum, you must implement the Write and WriteLine methods. Additionally, you can implement the Fail, Close and Flush methods.

Requirements

Namespace: System.Diagnostics

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System (in System.dll)

See Also

TraceListener Members | System.Diagnostics Namespace | DefaultTraceListener | EventLogTraceListener | TextWriterTraceListener | Debug | Trace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.