System.Diagnostics.Trace Class

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

See Also: Trace Members


public sealed class Trace

See Also



You can use the properties and methods in the System.Diagnostics.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.

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

The System.Diagnostics.BooleanSwitch and System.Diagnostics.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 System.Diagnostics.Switch class and the How to: Configure Trace Switches topic.

You can customize the tracing output's target by adding System.Diagnostics.TraceListener instances to or removing instances from the Trace.Listeners collection. The Trace.Listeners collection is shared by both the System.Diagnostics.Debug and the System.Diagnostics.Trace classes; adding a trace listener to either class adds the listener to both. By default, trace output is emitted using the System.Diagnostics.DefaultTraceListener class.


Adding a trace listener to the Trace.Listeners collection can cause an exception to be thrown while tracing, if a resource used by the trace listener is not available. The conditions and the exception thrown depend on the trace listener and cannot be enumerated in this topic. It may be useful to place calls to the System.Diagnostics.Trace methods in try/catch blocks to detect and handle any exceptions from trace listeners.


If you add trace listeners to partially trusted code, you will get a System.Security.SecurityException exception, because adding trace listeners requires System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode permission. To trace partially trusted code that is running in a sandbox in Visual Studio, do not add trace listeners. Instead, view the System.Diagnostics.Trace and System.Diagnostics.Debug messages in the Output window.

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

To set the Trace.AutoFlush and Trace.IndentSize for System.Diagnostics.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 System.Diagnostics.ConditionalAttribute attribute is applied to the methods of System.Diagnostics.Trace. Compilers that support System.Diagnostics.ConditionalAttribute ignore calls to these methods unless "TRACE" is defined as a conditional compilation symbol. Refer to a compiler's documentation to determine whether System.Diagnostics.ConditionalAttribute is supported and the syntax for defining a conditional compilation symbol.


In Visual Studio projects, by default, the "DEBUG" conditional compilation symbol is defined for debug builds, and the "TRACE" symbol is defined for both debug and release builds. For information about how to disable this behavior, see the Visual Studio documentation.

To define the "TRACE" conditional compilation symbol in C#, add the /d:TRACE option to the compiler command line when you compile your code using a command line, or add #define TRACE to the top of your file. In Visual Basic, add the /d:TRACE=True option to the compiler command line or add #Const TRACE=True to the file.

System.Diagnostics.ConditionalAttribute is not supported by the C++ compiler. To provide equivalent functionality, you must enclose calls to the methods of System.Diagnostics.Trace in an #if defined(TRACE) ... #endif block, and add the /DTRACE option to the compiler command line or add #define TRACE to the file.


Namespace: System.Diagnostics
Assembly: System (in System.dll)
Assembly Versions: 1.0.3300.0, 1.0.5000.0,,