Export (0) Print
Expand All

Debug Class

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

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

public sealed class Debug
public final class Debug
public final class Debug
Not applicable.

If you use methods in the 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 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 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, the DefaultTraceListener class emits trace output.

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

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

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

The ConditionalAttribute attribute is applied to the methods of Debug. Compilers that support ConditionalAttribute ignore calls to these methods unless "DEBUG" is defined as a conditional compilation symbol. Refer to a compiler's documentation to determine whether ConditionalAttribute is supported and the syntax for defining a conditional compilation symbol.

To define the "DEBUG" conditional compilation symbol in C# and J#, add the /d:DEBUG option to the compiler command line when you compile your code 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.

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

In Visual Studio 2005 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 on how to disable this behavior, see the Visual Studio 2005 documentation.

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

// Specify /d:DEBUG when compiling.

using System;
using System.Data;
using System.Diagnostics;

class Test
{
    static void Main()
    {
       Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
       Debug.AutoFlush = true;
       Debug.Indent();
       Debug.WriteLine("Entering Main");
       Console.WriteLine("Hello World.");
       Debug.WriteLine("Exiting Main"); 
       Debug.Unindent();
    }
}

System.Object
  System.Diagnostics.Debug

This type is thread safe.

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft