Debug Debug Debug Debug Class

定义

提供一组有助于调试代码的方法和属性。Provides a set of methods and properties that help debug your code.

public ref class Debug abstract sealed
public static class Debug
type Debug = class
Public Class Debug
继承
DebugDebugDebugDebug

示例

下面的示例使用Debug指示程序执行的开始和结束。The following example uses Debug to indicate the beginning and end of a program's execution. 该示例还使用IndentUnindent来区分跟踪输出。The example also uses Indent and Unindent 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;
}
// 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();
    }
}
' Specify /d:DEBUG=True when compiling.

Imports System.Data
Imports System.Diagnostics

Class Test

    Shared Sub 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()
        
    End Sub
    
End Class

注解

如果使用类中的Debug方法来打印调试信息并使用断言检查逻辑, 则可以使代码更可靠, 而不会影响发运产品的性能和代码大小。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.

此类提供用于显示Assert对话框的方法, 并发出将始终失败的断言。This class provides methods to display an Assert dialog box, and to emit an assertion that will always fail. 此类提供以下变体中的写入方法Write: WriteLine WriteIf 、和WriteLineIfThis class provides write methods in the following variations: Write, WriteLine, WriteIf and WriteLineIf.

BooleanSwitchTraceSwitch类提供了动态控制跟踪输出的方法。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. 有关使用配置文件设置开关的信息, 请参阅Switch类和跟踪开关主题。For information on using the configuration file to set a switch, see the Switch class and the Trace Switches topic.

您可以通过将实例添加TraceListenerListeners集合或从集合中删除实例来自定义跟踪输出的目标。You can customize the tracing output's target by adding TraceListener instances to or removing instances from the Listeners collection. 集合由DebugTrace类共享; 将跟踪侦听器添加到任一类会将侦听器添加到这两个类中。 ListenersThe Listeners collection is shared by both the Debug and the Trace classes; adding a trace listener to either class adds the listener to both. 默认情况下, DefaultTraceListener类发出跟踪输出。By default, the DefaultTraceListener class emits trace output.

备注

如果跟踪侦听器使用的资源Listeners不可用, 则在跟踪时将跟踪侦听器添加到集合可能导致引发异常。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. Debug块中try 调用方法以catch检测和处理跟踪侦听器中的任何异常可能会很有用。 /It may be useful to place calls to the Debug methods in try/catch blocks to detect and handle any exceptions from trace listeners.

您可以使用Indent方法IndentLevel或属性修改缩进级别。You can modify the level of indentation using the Indent method or the IndentLevel property. 若要修改缩进间距, 请IndentSize使用属性。To modify the indent spacing, use the IndentSize property. 可以通过将AutoFlush属性设置为来true指定是否在每次写入后自动刷新输出缓冲区。You can specify whether to automatically flush the output buffer after each write by setting the AutoFlush property to true.

若要为AutoFlush设置IndentSizeDebug, 你可以编辑与应用程序名称对应的配置文件。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>  

特性应用于的Debug方法。 ConditionalAttributeThe ConditionalAttribute attribute is applied to the methods of Debug. 支持ConditionalAttribute忽略对这些方法的调用的编译器, 除非将 "DEBUG" 定义为条件编译符号。Compilers that support ConditionalAttribute ignore calls to these methods unless "DEBUG" is defined as a conditional compilation symbol. 请参阅编译器的文档, 以确定是否ConditionalAttribute支持以及用于定义条件编译符号的语法。Refer to a compiler's documentation to determine whether ConditionalAttribute is supported and the syntax for defining a conditional compilation symbol.

备注

在 Visual Studio C#和 Visual Basic 项目中, 默认情况下, 为调试版本定义 "debug" 条件编译符号, 同时为调试版本和发布版本定义 "TRACE" 符号。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. 有关如何禁用此行为的信息, 请参阅 Visual Studio 文档。For information about how to disable this behavior, see the Visual Studio documentation. 有关 Visual C++中的条件调试的信息, 请参阅DebugC++类 (/cli)For information about conditional debugging in Visual C++, see Debug Class (C++/CLI).

若要在中C#定义 "调试" 条件编译符号, 请/d:DEBUG在使用命令行编译代码时将选项添加到编译器命令行, 或将添加#define DEBUG到文件的顶部。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. 在 Visual Basic 中, 将/d:DEBUG=True选项添加到编译器命令行或添加#Const DEBUG=True到文件。In Visual Basic, add the /d:DEBUG=True option to the compiler command line or add #Const DEBUG=True to the file.

属性

AutoFlush AutoFlush AutoFlush AutoFlush

获取或设置一个值,通过该值指示每次写入后是否应在 Flush() 上调用 ListenersGets or sets a value indicating whether Flush() should be called on the Listeners after every write.

IndentLevel IndentLevel IndentLevel IndentLevel

获取或设置缩进级别。Gets or sets the indent level.

IndentSize IndentSize IndentSize IndentSize

获取或设置缩进的空格数。Gets or sets the number of spaces in an indent.

Listeners Listeners Listeners Listeners

获取监视调试输出的侦听器集合。Gets the collection of listeners that is monitoring the debug output.

方法

Assert(Boolean) Assert(Boolean) Assert(Boolean) Assert(Boolean)

检查条件 ;如果条件为 false,则显示一个消息框,其中显示调用堆栈。Checks for a condition; if the condition is false, displays a message box that shows the call stack.

Assert(Boolean, String) Assert(Boolean, String) Assert(Boolean, String) Assert(Boolean, String)

检查条件;如果条件为 false,则输出指定消息并显示指示调用堆栈的消息框。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) Assert(Boolean, String, String) Assert(Boolean, String, String) Assert(Boolean, String, String)

检查条件 ;如果条件为 false,则输出两条指定消息,并显示一个消息框,其中显示调用堆栈。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, Object[]) Assert(Boolean, String, String, Object[]) Assert(Boolean, String, String, Object[]) Assert(Boolean, String, String, Object[])

检查条件;如果条件为 false,则输出两条指定消息(简单消息和格式化消息),并显示一个消息框,其中会显示调用堆栈。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() Close() Close() Close()

刷新输出缓冲区,然后对所有 Listeners 调用 Close 方法。Flushes the output buffer and then calls the Close method on each of the Listeners.

Fail(String) Fail(String) Fail(String) Fail(String)

发出指定的错误消息。Emits the specified error message.

Fail(String, String) Fail(String, String) Fail(String, String) Fail(String, String)

发出错误消息及详细的错误消息。Emits an error message and a detailed error message.

Flush() Flush() Flush() Flush()

刷新输出缓冲区并使放入缓冲区的数据写入 Listeners 集合。Flushes the output buffer and causes buffered data to write to the Listeners collection.

Indent() Indent() Indent() Indent()

将当前的 IndentLevel 增加 1。Increases the current IndentLevel by one.

Print(String) Print(String) Print(String) Print(String)

将后跟行结束符的消息写入 Listeners 集合中的跟踪侦听器。Writes a message followed by a line terminator to the trace listeners in the Listeners collection.

Print(String, Object[]) Print(String, Object[]) Print(String, Object[]) Print(String, Object[])

将后跟行结束符的格式化字符串写入 Listeners 集合中的跟踪侦听器。Writes a formatted string followed by a line terminator to the trace listeners in the Listeners collection.

Unindent() Unindent() Unindent() Unindent()

将当前的 IndentLevel 减少 1。Decreases the current IndentLevel by one.

Write(String, String) Write(String, String) Write(String, String) Write(String, String)

将类别名称和消息写入 Listeners 集合中的跟踪侦听器。Writes a category name and message to the trace listeners in the Listeners collection.

Write(Object, String) Write(Object, String) Write(Object, String) Write(Object, String)

将类别名称和对象的 ToString() 方法值写入 Listeners 集合中的跟踪侦听器。Writes a category name and the value of the object's ToString() method to the trace listeners in the Listeners collection.

Write(Object) Write(Object) Write(Object) Write(Object)

将对象的 ToString() 方法的值写入 Listeners 集合中的跟踪侦听器中。Writes the value of the object's ToString() method to the trace listeners in the Listeners collection.

Write(String) Write(String) Write(String) Write(String)

将消息写入 Listeners 集合中的跟踪侦听器。Writes a message to the trace listeners in the Listeners collection.

WriteIf(Boolean, Object) WriteIf(Boolean, Object) WriteIf(Boolean, Object) WriteIf(Boolean, Object)

如果条件为 true,则将对象的 ToString() 方法值写入 Listeners 集合中的跟踪侦听器。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) WriteIf(Boolean, String) WriteIf(Boolean, String) WriteIf(Boolean, String)

如果条件为 true,则将消息写入 Listeners 集合中的跟踪侦听器。Writes a message to the trace listeners in the Listeners collection if a condition is true.

WriteIf(Boolean, Object, String) WriteIf(Boolean, Object, String) WriteIf(Boolean, Object, String) WriteIf(Boolean, Object, String)

如果条件为 true,则将类别名称和对象的 ToString() 方法值写入 Listeners 集合中的跟踪侦听器。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) WriteIf(Boolean, String, String) WriteIf(Boolean, String, String) WriteIf(Boolean, String, String)

如果条件为 true,则将类别名称和消息写入 Listeners 集合中的跟踪侦听器。Writes a category name and message to the trace listeners in the Listeners collection if a condition is true.

WriteLine(Object) WriteLine(Object) WriteLine(Object) WriteLine(Object)

将对象的 ToString() 方法的值写入 Listeners 集合中的跟踪侦听器中。Writes the value of the object's ToString() method to the trace listeners in the Listeners collection.

WriteLine(String) WriteLine(String) WriteLine(String) WriteLine(String)

将后跟行结束符的消息写入 Listeners 集合中的跟踪侦听器。Writes a message followed by a line terminator to the trace listeners in the Listeners collection.

WriteLine(Object, String) WriteLine(Object, String) WriteLine(Object, String) WriteLine(Object, String)

将类别名称和对象的 ToString() 方法值写入 Listeners 集合中的跟踪侦听器。Writes a category name and the value of the object's ToString() method to the trace listeners in the Listeners collection.

WriteLine(String, Object[]) WriteLine(String, Object[]) WriteLine(String, Object[]) WriteLine(String, Object[])

将后跟行结束符的格式化消息写入 Listeners 集合中的跟踪侦听器。Writes a formatted message followed by a line terminator to the trace listeners in the Listeners collection.

WriteLine(String, String) WriteLine(String, String) WriteLine(String, String) WriteLine(String, String)

将类别名称和消息写入 Listeners 集合中的跟踪侦听器。Writes a category name and message to the trace listeners in the Listeners collection.

WriteLineIf(Boolean, Object, String) WriteLineIf(Boolean, Object, String) WriteLineIf(Boolean, Object, String) WriteLineIf(Boolean, Object, String)

如果条件为 true,则将类别名称和对象的 ToString() 方法值写入 Listeners 集合中的跟踪侦听器。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, Object) WriteLineIf(Boolean, Object) WriteLineIf(Boolean, Object) WriteLineIf(Boolean, Object)

如果条件为 true,则将对象的 ToString() 方法值写入 Listeners 集合中的跟踪侦听器。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) WriteLineIf(Boolean, String) WriteLineIf(Boolean, String) WriteLineIf(Boolean, String)

如果条件为 true,则将消息写入 Listeners 集合中的跟踪侦听器。Writes a message to the trace listeners in the Listeners collection if a condition is true.

WriteLineIf(Boolean, String, String) WriteLineIf(Boolean, String, String) WriteLineIf(Boolean, String, String) WriteLineIf(Boolean, String, String)

如果条件为 true,则将类别名称和消息写入 Listeners 集合中的跟踪侦听器。Writes a category name and message to the trace listeners in the Listeners collection if a condition is true.

适用于

线程安全性

此类型是线程安全的。This type is thread safe.

另请参阅