通过


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

以下示例在 中创建一个新开关。 它创建新的开关并为其分配值。 然后,根据开关设置,它将输出调试消息以输入和离开方法。

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 类实现开关的默认行为,使你可以在运行时更改交换机级别。

此类是 BooleanSwitchSourceSwitchTraceSwitch 类的基类。 这些开关满足大多数调试和跟踪需求。 有关跟踪开关的详细信息,请参阅 跟踪开关

必须启用跟踪或调试才能使用开关。 以下语法特定于编译器。 如果使用 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>

本示例配置节定义BooleanSwitch属性集设置为mySwitchEnabled和值设置为 trueDisplayName . 在应用程序中,可以通过创建 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)
    {
        //...
    }
}

实施者说明

如果需要跟踪级别或机制来设置开关级别与所提供的 BooleanSwitch级别不同的级别, SourceSwitchTraceSwitch则可以继承自 Switch。 从此类继承时,必须实现 SwitchSetting 该方法。

构造函数

名称 说明
Switch(String, String, String)

初始化类的新实例 Switch ,指定开关的显示名称、说明和默认值。

Switch(String, String)

初始化 Switch 类的新实例。

属性

名称 说明
Attributes

获取应用程序配置文件中定义的自定义开关属性。

DefaultValue

获取构造函数中分配的默认值。

Description

获取开关的说明。

DisplayName

获取用于标识开关的名称。

SwitchSetting

获取或设置此开关的当前设置。

Value

获取或设置开关的值。

方法

名称 说明
Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetSupportedAttributes()

获取开关支持的自定义属性。

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
OnSwitchSettingChanged()

更改 SwitchSetting 属性时调用。

OnValueChanged()

更改 Value 属性时调用。

Refresh()

刷新跟踪配置数据。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

活动

名称 说明
Initializing

在需要初始化时 Switch 发生。

适用于

另请参阅