Debug Class
Updated: May 2011
Provides a set of methods and properties that help debug your code. This class cannot be inherited.
Assembly: System (in System.dll)
The Debug type exposes the following members.
| Name | Description | |
|---|---|---|
![]() ![]() ![]() | AutoFlush | Gets or sets a value indicating whether Flush should be called on the Listeners after every write. |
![]() ![]() ![]() | IndentLevel | Gets or sets the indent level. |
![]() ![]() ![]() | IndentSize | Gets or sets the number of spaces in an indent. |
![]() ![]() ![]() | Listeners | Gets the collection of listeners that is monitoring the debug output. |
| Name | Description | |
|---|---|---|
![]() ![]() ![]() ![]() | Assert(Boolean) | Checks for a condition; if the condition is false, displays a message box that shows the call stack. |
![]() ![]() ![]() ![]() | Assert(Boolean, String) | Checks for a condition; if the condition is false, outputs a specified message and displays a message box that shows the call stack. |
![]() ![]() ![]() ![]() | Assert(Boolean, String, String) | Checks for a condition; if the condition is false, outputs two specified messages and displays a message box that shows the call stack. |
![]() ![]() | Assert(Boolean, String, String, array<Object>) | Checks for a condition; if the condition is false, outputs two messages (simple and formatted) and displays a message box that shows the call stack. |
![]() ![]() ![]() | Close | Flushes the output buffer and then calls the Close method on each of the Listeners. |
![]() ![]() ![]() | Fail(String) | Emits the specified error message. |
![]() ![]() ![]() | Fail(String, String) | Emits an error message and a detailed error message. |
![]() ![]() ![]() | Flush | Flushes the output buffer and causes buffered data to write to the Listeners collection. |
![]() ![]() | Indent | Increases the current IndentLevel by one. |
![]() ![]() | Print(String) | Writes a message followed by a line terminator to the trace listeners in the Listeners collection. |
![]() ![]() | Print(String, array<Object>) | Writes a formatted string followed by a line terminator to the trace listeners in the Listeners collection. |
![]() ![]() | Unindent | Decreases the current IndentLevel by one. |
![]() ![]() ![]() | Write(Object) | Writes the value of the object's ToString method to the trace listeners in the Listeners collection. |
![]() ![]() ![]() | Write(String) | Writes a message to the trace listeners in the Listeners collection. |
![]() ![]() ![]() | Write(Object, String) | Writes a category name and the value of the object's ToString method to the trace listeners in the Listeners collection. |
![]() ![]() ![]() | Write(String, String) | Writes a category name and message to the trace listeners in the Listeners collection. |
![]() ![]() ![]() | WriteIf(Boolean, Object) | Writes the value of the object's ToString method to the trace listeners in the Listeners collection if a condition is true. |
![]() ![]() ![]() | WriteIf(Boolean, String) | Writes a message to the trace listeners in the Listeners collection if a condition is true. |
![]() ![]() ![]() | WriteIf(Boolean, Object, String) | Writes a category name and the value of the object's ToString method to the trace listeners in the Listeners collection if a condition is true. |
![]() ![]() ![]() | WriteIf(Boolean, String, String) | Writes a category name and message to the trace listeners in the Listeners collection if a condition is true. |
![]() ![]() ![]() ![]() | WriteLine(Object) | Writes the value of the object's ToString method to the trace listeners in the Listeners collection. |
![]() ![]() ![]() ![]() | WriteLine(String) | Writes a message followed by a line terminator to the trace listeners in the Listeners collection. |
![]() ![]() ![]() | WriteLine(Object, String) | Writes a category name and the value of the object's ToString method to the trace listeners in the Listeners collection. |
![]() ![]() | WriteLine(String, array<Object>) | Writes a formatted message followed by a line terminator to the trace listeners in the Listeners collection. |
![]() ![]() ![]() | WriteLine(String, String) | Writes a category name and message to the trace listeners in the Listeners collection. |
![]() ![]() ![]() | WriteLineIf(Boolean, Object) | Writes the value of the object's ToString method to the trace listeners in the Listeners collection if a condition is true. |
![]() ![]() ![]() | WriteLineIf(Boolean, String) | Writes a message to the trace listeners in the Listeners collection if a condition is true. |
![]() ![]() ![]() | WriteLineIf(Boolean, Object, String) | Writes a category name and the value of the object's ToString method to the trace listeners in the Listeners collection if a condition is true. |
![]() ![]() ![]() | WriteLineIf(Boolean, String, String) | Writes a category name and message to the trace listeners in the Listeners collection if a condition is true. |
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 affecting 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 ways to dynamically control the tracing output. You can modify the values of these switches without recompiling your application. For information about 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. The Listeners collection is shared by both the Debug and the Trace classes; adding a trace listener to either class adds the listener to both. By default, the DefaultTraceListener class emits trace output.
Note |
|---|
Adding a trace listener to the 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 Debug methods in try/catch blocks to detect and handle any exceptions from trace listeners. |
You can modify the level of indentation by using the Indent method or the IndentLevel property. To modify the indentation amount, use the IndentSize property. You can specify whether to automatically flush the output buffer after each write operation 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 as shown in 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.
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 (C++/CLI). |
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.
| Topic | Location |
|---|---|
| How to: Create and Initialize Trace Listeners | .NET Framework: Debugging |
| How to: Compile Conditionally with Trace and Debug | .NET Framework: Debugging |
| How to: Configure Trace Switches | .NET Framework: Debugging |
| How to: Trace Code in an Application | .NET Framework: Debugging |
| How to: Add Trace Statements to Application Code | .NET Framework: Debugging |
| How to: Create and Initialize Trace Listeners | .NET Framework: Debugging |
| How to: Compile Conditionally with Trace and Debug | .NET Framework: Debugging |
| How to: Configure Trace Switches | .NET Framework: Debugging |
| How to: Trace Code in an Application | .NET Framework: Debugging |
| How to: Add Trace Statements to Application Code | .NET Framework: Debugging |
The following example uses Debug to indicate the beginning and end of a program's execution. The example also uses the Indent and Unindent methods to distinguish the tracing output.
// Specify /DDEBUG when compiling. #using <System.dll> using namespace System; using namespace System::Diagnostics; int main( void ) { #if defined(DEBUG) Debug::Listeners->Add( gcnew TextWriterTraceListener( Console::Out ) ); Debug::AutoFlush = true; Debug::Indent(); Debug::WriteLine( "Entering Main" ); #endif Console::WriteLine( "Hello World." ); #if defined(DEBUG) Debug::WriteLine( "Exiting Main" ); Debug::Unindent(); #endif return 0; }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

