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.

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


[Visual Basic]
NotInheritable Public Class Trace
public sealed class Trace
public __gc __sealed class Trace
public class Trace

Thread Safety

This type is safe for multithreaded operations.


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.

Note   To enable debugging 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 funtionality in the Managed Extensions for 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 .NET 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 .NET 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 TraceSwitch Configuration topic in the Visual Studio .NET documentation.

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" />


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.

[Visual Basic] 
Public 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

public static int Main(string[] args)
   Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
   Trace.AutoFlush = true;
   Trace.WriteLine("Entering Main");
   Console.WriteLine("Hello World.");
   Trace.WriteLine("Exiting Main"); 
   return 0;

int main() {
   Trace::Listeners->Add(new TextWriterTraceListener(Console::Out));
   Trace::AutoFlush = true;
   Trace::WriteLine(S"Entering Main");
   Console::WriteLine(S"Hello World.");
   Trace::WriteLine(S"Exiting Main");
   return 0;

import System
import System.Diagnostics

Trace.Listeners.Add(new TextWriterTraceListener(Console.Out))
Trace.AutoFlush = true
Trace.WriteLine("Start program")
Console.WriteLine("Hello World.")
Trace.WriteLine("End program")


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

Trace Members | System.Diagnostics Namespace | Debug | Switch | BooleanSwitch | TraceSwitch | TraceListener | DefaultTraceListener | EventLogTraceListener | TraceListenerCollection | ConditionalAttribute