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
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 'Main

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 方法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.

您可以在TraceListener Listeners集合中加入實例或移除實例, 以自訂追蹤輸出的目標。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.

若要設定AutoFlushIndentSizeTrace, 您可以編輯對應至應用程式名稱的設定檔。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 組建定義 "DEBUG" 條件式編譯符號, 並針對 debug 和 release 組建定義 "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#定義「追蹤」條件式編譯符號, /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

取得監視追蹤輸出的接聽項 (Listener) 的集合。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 增加一級。Increases the current IndentLevel by one.

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

建立目前 Object 的淺層複本 (Shallow Copy)。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 減少一級。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.

另請參閱