This documentation is archived and is not being maintained.

Trace Class

Provides a set of methods and properties that help you trace the execution of your code. This class cannot be inherited.

Namespace: System.Diagnostics
Assembly: System (in system.dll)

Public NotInheritable Class Trace
Dim instance As Trace

public final class Trace
public final class Trace

You can use the properties and methods in the Trace class to instrument release builds. Instrumentation allows you to monitor the health of your application running in real-life settings. Tracing helps you isolate problems and fix them without disturbing a running system.


To enable tracing in C#, add the /d:TRACE flag to the compiler command line when you compile your code, or you can add #define TRACE to the top of your file. In Visual Basic, add the /d:TRACE=True flag to the compiler command line. To provide equivalent functionality in C++, you must enclose calls to methods of this class in a #ifdef TRACE... #endif block. This syntax is compiler-specific. If you are using a compiler other than the ones specified above, you must refer to the compiler's documentation to enable conditional compiling because of the conditional compilation attributes placed on the methods of the Trace class.

In Visual Studio 2005 projects, Trace is enabled by default. Therefore, code is generated for all Trace methods in both release and debug builds. This allows an end user to turn on tracing to help identify the problem without the program having to be recompiled. By contrast, Debug is disabled in release builds by default, so no executable code is generated for Debug methods. To disable Trace, see the Visual Studio 2005 documentation.

This class provides methods to display an Assert dialog box, and to emit an assertion that will always Fail. This class provides write methods in the following variations: Write, WriteLine, WriteIf, and WriteLineIf.

The BooleanSwitch and TraceSwitch classes provide means to dynamically control the tracing output. You can modify the values of these switches without recompiling your application. For information on using the configuration file to set a switch, see the Switch class and the How to: Configure Trace Switches topic.

You can customize the tracing output's target by adding TraceListener instances to or removing instances from the Listeners collection. By default, trace output is emitted using the DefaultTraceListener class.

The Trace class provides properties to get or set the level of Indent, the IndentSize, and whether to AutoFlush after each write.

To set the AutoFlush and IndentSize for Trace, you can edit the configuration file that corresponds to the name of your application. The configuration file should be formatted like the following example:

    <trace autoflush="false" indentsize="3" />

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: The .NET Compact Framework does not support tracing features that use a configuration file.

The following example uses Trace to indicate the beginning and the end of a program's execution. The example also uses Indent and Unindent to distinguish the tracing output.

Shared Sub Main()
    Trace.Listeners.Add(New TextWriterTraceListener(Console.Out))
    Trace.AutoFlush = True
    Trace.WriteLine("Entering Main")
    Console.WriteLine("Hello World.")
    Trace.WriteLine("Exiting Main")
End Sub 'Main


This type is thread safe.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0