System.Diagnostics.Debug Class

Provides a set of methods and properties that help debug your code. This class cannot be inherited.

See Also: Debug Members

Syntax

public static class Debug

See Also

ConditionalAttribute

Remarks

If you use methods in the System.Diagnostics.Debug class to print debugging information and check your logic with assertions, you can make your code more robust without impacting the performance and code size of your shipping product.

This class provides methods to display an Debug.Assert(bool) dialog box, and to emit an assertion that will always fail. This class provides write methods in the following variations: Debug.Write(string), Debug.WriteLine(string), Debug.WriteIf(bool, string) and Debug.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 Trace Switches topic.

You can customize the tracing output's target by adding System.Diagnostics.TraceListener instances to or removing instances from the Debug.Listeners collection. The Debug.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, the System.Diagnostics.DefaultTraceListener class emits trace output.

Note:

Adding a trace listener to the Debug.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.Debug methods in try/catch blocks to detect and handle any exceptions from trace listeners.

You can modify the level of indentation using the Debug.Indent method or the Debug.IndentLevel property. To modify the indent spacing, use the Debug.IndentSize property. You can specify whether to automatically flush the output buffer after each write by setting the Debug.AutoFlush property to true.

To set the Debug.AutoFlush and Debug.IndentSize for System.Diagnostics.Debug, you can edit the configuration file corresponding to the name of your application. The configuration file should be formatted as shown in the following example.

Example

<configuration>
  <system.diagnostics>
    <trace autoflush="true" indentsize="7" />
  </system.diagnostics>
</configuration>

The System.Diagnostics.ConditionalAttribute attribute is applied to the methods of System.Diagnostics.Debug. Compilers that support System.Diagnostics.ConditionalAttribute ignore calls to these methods unless "DEBUG" 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.

Note:

In Visual Studio C# and Visual Basic 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. For information about conditional debugging in Visual C++, see Debug Class in Visual C++.

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

Requirements

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