Switch 类

定义

提供一个用于创建新的调试和跟踪开关的抽象基类。Provides an abstract base class to create new debugging and tracing switches.

public ref class Switch abstract
public abstract class Switch
type Switch = class
Public MustInherit Class Switch
继承
Switch
派生

示例

下面的示例演示如何定义一个具有四Switch个跟踪级别的新类,这些跟踪可用于跟踪调用堆栈。The following example shows how to define a new Switch class with four levels of tracing that can be used to trace a call stack. 您可以使用开关来检测您的应用程序,以便在每次输入或退出该方法时进行记录。You can use the switch to instrument your application to log each time the method is entered or exited.

第一个示例创建用于设置交换机级别的枚举。The first example creates the enumeration used to set the level of the switch.

// The following are possible values for the new switch.
public enum class 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,
}
' The following are possible values for the new switch.
Public Enum MethodTracingSwitchLevel
    Off = 0
    EnteringMethod = 1
    ExitingMethod = 2
    Both = 3
End Enum 'MethodTracingSwitchLevel

下面的示例创建新的开关。The following example creates the new switch. 此代码实现了Level一个属性以设置新开关的值。The code implements a Level property to set the value of the new switch. Level调用受保护的SwitchSetting属性,该属性将值分配给新的开关。Level calls the protected property SwitchSetting that assigns the value to the new switch. 此示例还实现了两个取值函数属性以获取开关的分配值。This example also implements two assessor properties to get the assigned value of the switch.

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

public:
    MyMethodTracingSwitch( String^ displayName, String^ description )
       : Switch( displayName, description )
    {}

    property MethodTracingSwitchLevel Level
    {
        MethodTracingSwitchLevel get()
        {
            return level;
        }

       void set( MethodTracingSwitchLevel value )
       {
           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:
    property bool OutputExit
    {
        bool get()
        {
            return outExit;
        }
    }

    property bool OutputEnter
    {
        bool get()
        {
            return outEnter;
        }
    }
};


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创建一个新开关。The following example creates a new switch in Main. 它将创建一个新的开关,并为其分配一个值。It creates a new switch and assigns it a value. 然后,它会输出用于输入和离开方法的调试消息,具体取决于开关设置。Then, depending on the switch settings, it outputs debugging messages for entering and leaving the method.

public ref class Class1
{
private:

    /* Create an instance of MyMethodTracingSwitch.*/
    static MyMethodTracingSwitch^ mySwitch =
        gcnew 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(gcnew 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.*/
    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

注解

开关提供一种有效的机制,用于在运行时使用外部设置控制跟踪和调试输出。A switch provides an efficient mechanism for controlling tracing and debugging output at run time using external settings. Switch类实现开关的默认行为,使您可以在运行时更改交换机级别。The Switch class implements default behavior for switches, allowing you to change the switch level at run time.

此类是BooleanSwitch SourceSwitchTraceSwitch类的基类。This class is the base class for the BooleanSwitch, SourceSwitch and the TraceSwitch classes. 这些开关满足大多数调试和跟踪需求。These switches meet most debugging and tracing needs. 有关跟踪开关的详细信息,请参阅跟踪开关For more information about trace switches, see Trace Switches.

您必须启用跟踪或调试才能使用交换机。You must enable tracing or debugging to use a switch. 以下语法是编译器特定的。The following syntax is compiler specific. 如果使用C#或 Visual Basic 以外的编译器,请参阅编译器的文档。If you use compilers other than C# or Visual Basic, refer to the documentation for your compiler.

  • 若要在中C#启用调试, /d:DEBUG请在编译代码时将标志添加到编译器命令行,或者将添加#define DEBUG到文件的顶部。To enable debugging in C#, add the /d:DEBUG flag to the compiler command line when you compile your code, or you can add #define DEBUG to the top of your file. 在 Visual Basic 中,将/d:DEBUG=True标志添加到编译器命令行。In Visual Basic, add the /d:DEBUG=True flag to the compiler command line.

  • 若要在中C#使用启用跟踪, /d:TRACE请在编译代码时将标志添加到编译器命令行,或#define TRACE将添加到文件的顶部。To enable tracing using in C#, add the /d:TRACE flag to the compiler command line when you compile your code, or add #define TRACE to the top of your file. 在 Visual Basic 中,将/d:TRACE=True标志添加到编译器命令行。In Visual Basic, add the /d:TRACE=True flag to the compiler command line.

若要设置交换机的级别,请编辑与应用程序名称对应的配置文件。To set the level of your switch, edit the configuration file that corresponds to the name of your application. 在此文件中,你可以添加开关并设置其值、删除开关或清除之前由应用程序设置的所有开关。Within this file, you can add a switch and set its value, remove a switch, or clear all the switches previously set by the application. 配置文件的格式应类似于以下示例:The configuration file should be formatted like the following example:

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

此示例配置节定义BooleanSwitch mySwitch DisplayName 属性设置true为的,并将值设置为。EnabledThis example configuration section defines a BooleanSwitch with the DisplayName property set to mySwitch and the Enabled value set to true. 在应用程序中,可以通过创建一个BooleanSwitch具有相同名称的来使用已配置的开关值,如下面的代码示例中所示。Within your application, you can use the configured switch value by creating a BooleanSwitch with the same name, as shown in the following code example.

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

public:
    static void Main( )
    {
        //...
        Console::WriteLine("Boolean switch {0} configured as {1}",
            boolSwitch->DisplayName, ((Boolean^)boolSwitch->Enabled)->ToString());
        if (boolSwitch->Enabled)
        {
            //...
        }
    }
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设置与所提供SourceSwitch TraceSwitch的开关级别不同的机制,可以从Switch继承。If you need trace levels, or mechanisms for setting switch levels different from those provided by BooleanSwitch, SourceSwitch and TraceSwitch, you can inherit from Switch. 从此类继承时,必须实现SwitchSetting方法。When inheriting from this class, you must implement the SwitchSetting method.

构造函数

Switch(String, String)

初始化 Switch 类的新实例。Initializes a new instance of the Switch class.

Switch(String, String, String)

通过指定开关的显示名称、说明和默认值来初始化 Switch 类的新实例。Initializes a new instance of the Switch class, specifying the display name, description, and default value for the switch.

属性

Attributes

获取在应用程序配置文件中定义的自定义开关特性。Gets the custom switch attributes defined in the application configuration file.

Description

获取开关说明。Gets a description of the switch.

DisplayName

获取用于标识该开关的名称。Gets a name used to identify the switch.

SwitchSetting

获取或设置此开关的当前设置。Gets or sets the current setting for this switch.

Value

获取或设置开关的值。Gets or sets the value of the switch.

方法

Equals(Object)

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

(继承自 Object)
GetHashCode()

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

(继承自 Object)
GetSupportedAttributes()

获取开关支持的自定义特性。Gets the custom attributes supported by the switch.

GetType()

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

(继承自 Object)
MemberwiseClone()

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

(继承自 Object)
OnSwitchSettingChanged()

SwitchSetting 属性更改时调用。Invoked when the SwitchSetting property is changed.

OnValueChanged()

Value 属性更改时调用。Invoked when the Value property is changed.

ToString()

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

(继承自 Object)

适用于

另请参阅