Trace Trace Trace Trace Class

定义

提供一组方法和属性,帮助您跟踪代码的执行。Provides a set of methods and properties that help you trace the execution of your code. 此类不能被继承。This class cannot be inherited.

public ref class Trace sealed
public sealed class Trace
type Trace = class
Public NotInheritable Class Trace
继承
TraceTraceTraceTrace

示例

下面的示例使用Trace来指示程序执行的开始和结束。The following example uses Trace to indicate the beginning and the end of a program's execution. 该示例还使用Trace.IndentTrace.Unindent方法来区分跟踪输出。The example also uses the Trace.Indent and Trace.Unindent methods to distinguish the tracing output. 有关使用的Trace更完整示例, 请参阅如何:向应用程序代码添加跟踪语句。For a more complete example of the use of Trace, see How to: Add Trace Statements to Application Code.

// Specify /DTRACE when compiling.

#using <System.dll>
using namespace System;
using namespace System::Diagnostics;

int main()
{
   #if defined(TRACE)
   Trace::Listeners->Add( gcnew TextWriterTraceListener( Console::Out ) );
   Trace::AutoFlush = true;
   Trace::Indent();
   Trace::WriteLine( "Entering Main" );
   #endif
   Console::WriteLine( "Hello World." );
   #if defined(TRACE)
   Trace::WriteLine( "Exiting Main" );
   Trace::Unindent();
   #endif
   return 0;
}
// Specify /d:TRACE when compiling.
 
using System;
using System.Diagnostics;

class Test
{
    static void Main()
    {
       Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
       Trace.AutoFlush = true;
       Trace.Indent();
       Trace.WriteLine("Entering Main");
       Console.WriteLine("Hello World.");
       Trace.WriteLine("Exiting Main"); 
       Trace.Unindent();
    }
}
' Specify /d:TRACE=True when compiling.

Imports System.Diagnostics

Class Test
    
    Shared Sub Main()
    
        Trace.Listeners.Add(New TextWriterTraceListener(Console.Out))
        Trace.AutoFlush = True
        Trace.Indent()
        Trace.WriteLine("Entering Main")
        Console.WriteLine("Hello World.")
        Trace.WriteLine("Exiting Main")
        Trace.Unindent()
        
    End Sub

End Class

注解

您可以使用Trace类中的属性和方法来检测发布版本。You can use the properties and methods in the 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.

此类提供用于显示Assert对话框的方法, 并发出将始终Fail发出的断言。This class provides methods to display an Assert dialog box, and to emit an assertion that will always Fail. 此类提供以下变体中的编写方法Write: WriteLineWriteIf、和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 How to: Create, Initialize and Configure 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, trace output is emitted using the DefaultTraceListener class.

备注

如果跟踪侦听器使用的资源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. Trace块中try 调用方法以catch检测和处理跟踪侦听器中的任何异常可能会很有用。 /It may be useful to place calls to the Trace methods in try/catch blocks to detect and handle any exceptions from trace listeners.

备注

如果将跟踪侦听器添加到部分受信任的代码,则您将收到 SecurityException 异常,因为添加跟踪侦听器需要 UnmanagedCode 权限。If you add trace listeners to partially trusted code, you will get a SecurityException exception, because adding trace listeners requires UnmanagedCode permission. 若要跟踪正在 Visual Studio 的沙盒中运行的部分受信任的代码,请不要添加跟踪侦听器。To trace partially trusted code that is running in a sandbox in Visual Studio, do not add trace listeners. 请改为在Trace输出Debug窗口中查看和消息。Instead, view the Trace and Debug messages in the Output window.

类提供属性以获取或设置的Indent级别、 IndentSize和是否AutoFlush在每次写入后。 TraceThe Trace class provides properties to get or set the level of Indent, the IndentSize, and whether to AutoFlush after each write.

若要为AutoFlush设置IndentSizeTrace, 你可以编辑与你的应用程序的名称对应的配置文件。To set the AutoFlush and IndentSize for 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:

<configuration>  
  <system.diagnostics>  
    <trace autoflush="false" indentsize="3" />  
  </system.diagnostics>  
</configuration>  

特性应用于的Trace方法。 ConditionalAttributeThe ConditionalAttribute attribute is applied to the methods of Trace. 支持ConditionalAttribute忽略对这些方法的调用的编译器, 除非 "TRACE" 被定义为条件编译符号。Compilers that support ConditionalAttribute ignore calls to these methods unless "TRACE" 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 项目中,默认情况下,为调试版本定义“DEBUG”条件编译符号,并为调试版本和发行版本定义“TRACE”符号。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. 有关如何禁用此行为的信息, 请参阅 Visual Studio 文档。For information about how to disable this behavior, see the Visual Studio documentation.

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

ConditionalAttributeC++编译器不支持。ConditionalAttribute is not supported by the C++ compiler. 若要提供等效Trace的功能, 必须#if defined(TRACE) ... #endif/DTRACE块中包含对方法的调用, 并将选项添加到编译器命令行或添加#define TRACE到文件中。To provide equivalent functionality, you must enclose calls to the methods of 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.

属性

AutoFlush AutoFlush AutoFlush AutoFlush

获取或设置是否在每次写入后都在 Flush() 上调用 ListenersGets or sets whether Flush() should be called on the Listeners after every write.

CorrelationManager CorrelationManager CorrelationManager CorrelationManager

获取此跟踪线程的相关管理器。Gets the correlation manager for the thread for this trace.

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 trace output.

UseGlobalLock UseGlobalLock UseGlobalLock UseGlobalLock

获取或设置一个值,该值指示是否应当使用全局锁。Gets or sets a value indicating whether the global lock should be used.

方法

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.

Close() Close() Close() Close()

刷新输出缓冲区,然后关闭 ListenersFlushes the output buffer, and then closes the Listeners.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
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()

刷新输出缓冲区,并使放入缓冲区中的数据写入 ListenersFlushes the output buffer, and causes buffered data to be written to the Listeners.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
Indent() Indent() Indent() Indent()

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

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
Refresh() Refresh() Refresh() Refresh()

刷新跟踪配置数据。Refreshes the trace configuration data.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)
TraceError(String) TraceError(String) TraceError(String) TraceError(String)

使用指定的消息向 Listeners 集合中的跟踪侦听器中写入错误消息。Writes an error message to the trace listeners in the Listeners collection using the specified message.

TraceError(String, Object[]) TraceError(String, Object[]) TraceError(String, Object[]) TraceError(String, Object[])

使用指定的对象数组和格式设置信息向 Listeners 集合中的跟踪侦听器中写入错误消息。Writes an error message to the trace listeners in the Listeners collection using the specified array of objects and formatting information.

TraceInformation(String) TraceInformation(String) TraceInformation(String) TraceInformation(String)

使用指定消息,将信息性消息写入 Listeners 集合中的跟踪侦听器中。Writes an informational message to the trace listeners in the Listeners collection using the specified message.

TraceInformation(String, Object[]) TraceInformation(String, Object[]) TraceInformation(String, Object[]) TraceInformation(String, Object[])

使用指定的对象数组和格式设置信息向 Listeners 集合中的跟踪侦听器中写入信息性消息。Writes an informational message to the trace listeners in the Listeners collection using the specified array of objects and formatting information.

TraceWarning(String) TraceWarning(String) TraceWarning(String) TraceWarning(String)

使用指定的消息向 Listeners 集合中的跟踪侦听器中写入警告消息。Writes a warning message to the trace listeners in the Listeners collection using the specified message.

TraceWarning(String, Object[]) TraceWarning(String, Object[]) TraceWarning(String, Object[]) TraceWarning(String, Object[])

使用指定的对象数组和格式设置信息向 Listeners 集合中的跟踪侦听器中写入警告信息。Writes a warning message to the trace listeners in the Listeners collection using the specified array of objects and formatting information.

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

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

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(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(String) Write(String) Write(String) Write(String)

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

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

将类别名称和消息写入 Listeners 集合中的跟踪侦听器。Writes a category name and 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, 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) 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, 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(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) WriteLine(String) WriteLine(String) WriteLine(String)

将消息写入 Listeners 集合中的跟踪侦听器。Writes a message 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) 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, 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, 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.

安全性

SecurityPermission
用于处理非托管代码。for operating with unmanaged code. 安全操作: DemandSecurity action: Demand. 关联的枚举:UnmanagedCodeAssociated enumeration: UnmanagedCode

适用于

线程安全性

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

另请参阅