TraceSwitch 類別

定義

提供多層切換控制追蹤與偵錯的輸出,而不需要重新編譯程式碼。

public ref class TraceSwitch : System::Diagnostics::Switch
public class TraceSwitch : System.Diagnostics.Switch
type TraceSwitch = class
    inherit Switch
Public Class TraceSwitch
Inherits Switch
繼承
TraceSwitch

範例

下列程式代碼範例會建立新的 TraceSwitch ,並使用 參數來判斷是否要列印錯誤訊息。 參數會在類別層級建立。 MyMethod如果屬性設定為 TraceLevel.Error 或更新版本,則會Level寫入第一個錯誤訊息。 不過,如果 Level 小於 TraceLevel.VerboseMyMethod則不會寫入第二個錯誤訊息。

   // Class-level declaration.
   /* Create a TraceSwitch to use in the entire application.*/
private:
   static TraceSwitch^ mySwitch = gcnew TraceSwitch( "General", "Entire Application" );

public:
   static void MyMethod()
   {
      // Write the message if the TraceSwitch level is set to Error or higher.
      if ( mySwitch->TraceError )
         Console::WriteLine( "My error message." );
      
      // Write the message if the TraceSwitch level is set to Verbose.
      if ( mySwitch->TraceVerbose )
         Console::WriteLine( "My second error message." );
   }

   static void main()
   {
      // Run the method that prints error messages based on the switch level.
      MyMethod();
   }
//Class-level declaration.
/* Create a TraceSwitch to use in the entire application.*/
static TraceSwitch mySwitch = new TraceSwitch("General", "Entire Application");

static public void MyMethod()
{
    // Write the message if the TraceSwitch level is set to Error or higher.
    if (mySwitch.TraceError)
        Console.WriteLine("My error message.");

    // Write the message if the TraceSwitch level is set to Verbose.
    if (mySwitch.TraceVerbose)
        Console.WriteLine("My second error message.");
}

public static void Main(string[] args)
{
    // Run the method that prints error messages based on the switch level.
    MyMethod();
}
' Class-level declaration.
' Create a TraceSwitch to use in the entire application. 
Private Shared mySwitch As New TraceSwitch("General", "Entire Application")    

Public Shared Sub MyMethod()
    ' Write the message if the TraceSwitch level is set to Error or higher.
    If mySwitch.TraceError Then
        Console.WriteLine("My error message.")
    End If 
    ' Write the message if the TraceSwitch level is set to Verbose.
    If mySwitch.TraceVerbose Then
        Console.WriteLine("My second error message.")
    End If
End Sub

Public Shared Sub Main()
    ' Run the method that prints error messages based on the switch level.
    MyMethod()
End Sub

備註

您可以使用追蹤參數,根據其重要性篩選掉訊息。 類別 TraceSwitch 提供 TraceErrorTraceWarningTraceInfoTraceVerbose 屬性,以測試參數的層級。 屬性 Level 會取得或設定 參數的 TraceLevel

您可以在程式代碼中建立 , TraceSwitch 並直接設定層級來檢測特定程式代碼區段。

僅限 .NET Framework 應用程式中,您也可以透過應用程式組態檔設定的層級TraceSwitch,然後在應用程式中使用設定TraceSwitch的層級。 在應用程式組態檔中,您可以新增或移除參數、設定參數的值,或清除應用程式先前設定的所有參數。 組態檔的格式應該如下列範例所示:

<configuration>  
  <system.diagnostics>  
    <switches>  
      <add name="mySwitch" value="1" />  
    </switches>  
  </system.diagnostics>  
</configuration>  

此組態區段會 TraceSwitchDisplayNamemySwitch定義 ,並將 Level 設定為 1,其對應至 列舉值 TraceLevel.Error

注意

您也可以使用文字來指定參數的值。 例如,針對, trueBooleanSwitch或代表列舉值的文字,例如 ErrorTraceSwitch<add name="mySwitch" value="Error" /> 這一行相當於 <add name="mySwitch" value="1" />

在您的應用程式中,您可以建立 TraceSwitch 具有相同名稱的 ,以使用已設定的交換器層級,如下列範例所示:

private:
    static TraceSwitch^ appSwitch = gcnew TraceSwitch("mySwitch",
        "Switch in config file");

public:
    static void Main(array<String^>^ args)
    {
        //...
        Console::WriteLine("Trace switch {0} configured as {1}",
        appSwitch->DisplayName, appSwitch->Level.ToString());
        if (appSwitch->TraceError)
        {
            //...
        }
    }
private static TraceSwitch appSwitch = new TraceSwitch("mySwitch",
    "Switch in config file");

public static void Main(string[] args)
{
    //...
    Console.WriteLine("Trace switch {0} configured as {1}",
    appSwitch.DisplayName, appSwitch.Level.ToString());
    if (appSwitch.TraceError)
    {
        //...
    }
}
Private Shared appSwitch As new TraceSwitch("mySwitch", _
    "Switch in config file")

Public Shared Sub Main(args As String())
    '...
    Console.WriteLine("Trace switch {0} configured as {1}",
    appSwitch.DisplayName, appSwitch.Level.ToString())
    If appSwitch.TraceError = True  Then
        '...
    End If
End Sub

在 .NET Core 和 .NET 5+ 應用程式中, Level 新參數的 預設為 TraceLevel.Off

在 .NET Framework 應用程式中,switch Level 屬性預設為組態檔中指定的值。 如果建 TraceSwitch 構函式在組態檔中找不到初始交換器設定, Level 則新參數預設為 TraceLevel.Off

您必須啟用追蹤或偵錯,才能使用參數。 下列語法是編譯程式特有的。 如果您使用 C# 或 Visual Basic 以外的編譯程式,請參閱編譯程式的檔。

  • 若要在 C# 中啟用偵錯,請在編譯程式程式代碼時,將旗標新增 /d:DEBUG 至編譯程式命令行,或新增 #define DEBUG 至檔案頂端。 在 Visual Basic 中,將 /d:DEBUG=True 旗標新增至編譯程式命令行。

  • 若要在 C# 中啟用追蹤,請在編譯程式程式程式代碼時,將旗標新增 /d:TRACE 至編譯程式命令行,或新增 #define TRACE 至檔案頂端。 在 Visual Basic 中,將 /d:TRACE=True 旗標新增至編譯程式命令行。

注意

在隔離中使用 TraceSwitch 類別時,不需要這些偵錯和追蹤編譯程序參數。 它們只需要與 Trace 條件式編譯的 或 Debug 方法搭配使用。

如需偵測應用程式的詳細資訊,請參閱 DebugTrace。 如需設定和使用追蹤參數的詳細資訊,請參閱 追蹤參數

注意

若要改善效能,您可以在類別中建立 TraceSwitch 成員 static

建構函式

TraceSwitch(String, String)

使用指定的顯示名稱與說明將 TraceSwitch 類別的新執行個體初始化。

TraceSwitch(String, String, String)

使用指定的參數顯示名稱、說明與預設值,將 TraceSwitch 類別的新執行個體初始化。

屬性

Attributes

取得在應用程式組態檔中定義的自訂參數屬性。

(繼承來源 Switch)
DefaultValue

取得建構函式中指派的預設值。

(繼承來源 Switch)
Description

取得切換控制的描述。

(繼承來源 Switch)
DisplayName

取得用來識別切換控制的名稱。

(繼承來源 Switch)
Level

取得或設定決定參數允許之訊息的追蹤層級。

SwitchSetting

取得或設定這個切換控制的目前設定。

(繼承來源 Switch)
TraceError

取得值,表示此參數是否允許錯誤處理訊息。

TraceInfo

取得值,表示此參數是否允許告知性訊息。

TraceVerbose

取得值,表示此參數是否允許所有訊息。

TraceWarning

取得值,表示此參數是否允許警告訊息。

Value

取得或設定參數的值。

(繼承來源 Switch)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetSupportedAttributes()

取得由參數支援的自訂屬性。

(繼承來源 Switch)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
OnSwitchSettingChanged()

更新並修正此參數的層級。

OnValueChanged()

SwitchSetting 屬性設定成與 Value 屬性相等的整數。

OnValueChanged()

Value 屬性變更時叫用。

(繼承來源 Switch)
Refresh()

重新整理追蹤組態資料。

(繼承來源 Switch)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱