共用方式為


Switch 類別

定義

提供一個抽象基底類別以建立新的除錯與追蹤交換器。

public ref class Switch abstract
public abstract class Switch
type Switch = class
Public MustInherit Class Switch
繼承
Switch
衍生

範例

以下範例展示了如何定義 Switch 一個帶有四層追蹤的新類別,可用來追蹤呼叫堆疊。 你可以用開關來監控應用程式,每次輸入或退出方法時都會記錄。

第一個範例建立了用來設定開關電平的枚舉。

// The following are possible values for the new switch.
public enum MethodTracingSwitchLevel
{
    Off = 0,
    EnteringMethod = 1,
    ExitingMethod = 2,
    Both = 3,
}
' The following are possible values for the new switch.
Public Enum MethodTracingSwitchLevel
    Off = 0
    EnteringMethod = 1
    ExitingMethod = 2
    Both = 3
End Enum 'MethodTracingSwitchLevel

以下範例會產生新的交換器。 程式碼實作了一個 Level 屬性來設定新交換器的值。 Level 呼叫將該值指派給新交換器的受保護屬性 SwitchSetting 。 此範例同時實作兩個評估器屬性以取得分配值。

public class MyMethodTracingSwitch : Switch
{
    protected bool outExit;
    protected bool outEnter;
    protected MethodTracingSwitchLevel level;

    public MyMethodTracingSwitch(string displayName, string description) :
        base(displayName, description)
    {
    }

    public MethodTracingSwitchLevel Level
    {
        get
        {
            return level;
        }
        set
        {
            SetSwitchSetting((int)value);
        }
    }

    protected void SetSwitchSetting(int value)
    {
        if (value < 0)
        {
            value = 0;
        }
        if (value > 3)
        {
            value = 3;
        }

        level = (MethodTracingSwitchLevel)value;

        outEnter = false;
        if ((value == (int)MethodTracingSwitchLevel.EnteringMethod) ||
            (value == (int)MethodTracingSwitchLevel.Both))
        {
            outEnter = true;
        }

        outExit = false;
        if ((value == (int)MethodTracingSwitchLevel.ExitingMethod) ||
            (value == (int)MethodTracingSwitchLevel.Both))
        {
            outExit = true;
        }
    }

    public bool OutputExit
    {
        get
        {
            return outExit;
        }
    }

    public bool OutputEnter
    {
        get
        {
            return outEnter;
        }
    }
}
Public Class MyMethodTracingSwitch
    Inherits Switch
    Protected outExit As Boolean
    Protected outEnter As Boolean
    Protected myLevel As MethodTracingSwitchLevel
    
    Public Sub New(displayName As String, description As String)
        MyBase.New(displayName, description)
    End Sub


    Public Property Level() As MethodTracingSwitchLevel
        Get
            Return myLevel
        End Get
        Set
            SetSwitchSetting(CInt(value))
        End Set
    End Property


    Protected Sub SetSwitchSetting(value As Integer)
        If value < 0 Then
            value = 0
        End If
        If value > 3 Then
            value = 3
        End If

        myLevel = CType(value, MethodTracingSwitchLevel)

        outEnter = False
        If value = CInt(MethodTracingSwitchLevel.EnteringMethod) Or _
            value = CInt(MethodTracingSwitchLevel.Both) Then

            outEnter = True
        End If

        outExit = False
        If value = CInt(MethodTracingSwitchLevel.ExitingMethod) Or _
            value = CInt(MethodTracingSwitchLevel.Both) Then

            outExit = True
        End If
    End Sub


    Public ReadOnly Property OutputExit() As Boolean
        Get
            Return outExit
        End Get
    End Property


    Public ReadOnly Property OutputEnter() As Boolean
        Get
            Return outEnter
        End Get
    End Property
End Class

以下範例在 中 Main建立一個新的交換器。 它會建立一個新的交換器並賦予一個值。 接著,根據開關設定,它會輸出除錯訊息,用於輸入和離開方法。

public class Class1
{
    /* Create an instance of MyMethodTracingSwitch.*/
    static MyMethodTracingSwitch mySwitch =
        new MyMethodTracingSwitch("Methods", "Trace entering and exiting method");

    public static void Main()
    {
        // Add the console listener to see trace messages as console output
        Trace.Listeners.Add(new ConsoleTraceListener(true));
        Debug.AutoFlush = true;

        // Set the switch level to both enter and exit
        mySwitch.Level = MethodTracingSwitchLevel.Both;

        // Write a diagnostic message if the switch is set to entering.
        Debug.WriteLineIf(mySwitch.OutputEnter, "Entering Main");

        // Insert code to handle processing here...

        // Write another diagnostic message if the switch is set to exiting.
        Debug.WriteLineIf(mySwitch.OutputExit, "Exiting Main");
    }
}
Public Class Class1
    ' Create an instance of MyMethodTracingSwitch.
    Private Shared mySwitch As New _
        MyMethodTracingSwitch("Methods", "Trace entering and exiting method")

    Public Shared Sub Main()
        ' Add the console listener to see trace messages as console output
        Trace.Listeners.Add(New ConsoleTraceListener(True))
        Debug.AutoFlush = True

        ' Set the switch level to both enter and exit
        mySwitch.Level = MethodTracingSwitchLevel.Both

        ' Write a diagnostic message if the switch is set to entering.
        Debug.WriteLineIf(mySwitch.OutputEnter, "Entering Main")

        ' Insert code to handle processing here...

        ' Write another diagnostic message if the switch is set to exiting.
        Debug.WriteLineIf(mySwitch.OutputExit, "Exiting Main")
    End Sub
End Class

備註

開關提供了一種高效的機制,可以在執行時透過外部設定控制追蹤與除錯輸出。 這個 Switch 類別實作了交換器的預設行為,允許你在執行時改變交換器的電平。

此類別是 、 、 SourceSwitchTraceSwitch 類別的基底類別BooleanSwitch。 這些交換器滿足大部分除錯與追蹤需求。 欲了解更多關於追蹤交換器的資訊,請參見 追蹤交換器

你必須啟用追蹤或除錯才能使用開關。 以下語法是編譯器專屬的。 如果你使用非 C# 或 Visual Basic 的編譯器,請參考你編譯器的文件。

  • 要啟用 C# 除錯,編譯時在編譯器命令列加入 /d:DEBUG 旗標,或者你可以在檔案頂端新增 #define DEBUG 標記。 在 Visual Basic 中,將旗標加入 /d:DEBUG=True 編譯器命令列。

  • 要啟用 C# 追蹤,編譯程式碼時在編譯器命令列加旗 /d:TRACE 標,或在檔案頂端加標 #define TRACE 示。 在 Visual Basic 中,將旗標加入 /d:TRACE=True 編譯器命令列。

要在 .NET Framework 應用程式中設定交換器的等級,請編輯與應用程式名稱對應的設定檔。 在這個檔案中,你可以新增一個開關並設定其值、移除開關,或清除應用程式先前設定的所有開關。 設定檔格式應如以下範例:

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

此範例配置區段定義 a,屬性DisplayName設為 ,EnabledmySwitch值設為 trueBooleanSwitch 在你的應用程式中,你可以透過建立同名的 交換 BooleanSwitch 器值,如以下程式碼範例所示。

private static BooleanSwitch boolSwitch = new BooleanSwitch("mySwitch",
    "Switch in config file");

public static void Main()
{
    //...
    Console.WriteLine("Boolean switch {0} configured as {1}",
        boolSwitch.DisplayName, boolSwitch.Enabled.ToString());
    if (boolSwitch.Enabled)
    {
        //...
    }
}

給實施者的注意事項

如果你需要追蹤電平,或設定開關電平的機制,與 和 不同BooleanSwitchSourceSwitchTraceSwitch,你可以繼承自 。Switch 繼承自此類別時,必須實作該 SwitchSetting 方法。

建構函式

名稱 Description
Switch(String, String, String)

初始化類別的新實例 Switch ,指定交換器的顯示名稱、描述及預設值。

Switch(String, String)

初始化 Switch 類別的新執行個體。

屬性

名稱 Description
Attributes

取得應用程式設定檔中自訂的交換器屬性。

DefaultValue

會得到建構子中指定的預設值。

Description

取得交換器的描述。

DisplayName

會用一個名稱來識別交換器。

SwitchSetting

取得或設定這個開關的當前設定。

Value

取得或設定交換器的值。

方法

名稱 Description
Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetSupportedAttributes()

取得交換器支援的自訂屬性。

GetType()

取得目前實例的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
OnSwitchSettingChanged()

當屬性變更時 SwitchSetting 會被啟動。

OnValueChanged()

當屬性變更時 Value 會被啟動。

Refresh()

刷新追蹤設定資料。

ToString()

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

(繼承來源 Object)

事件

名稱 Description
Initializing

Switch 需要初始化時會發生。

適用於

另請參閱