Switch 类

提供一个 abstract 基类,以创建新的调试和跟踪开关。

**命名空间:**System.Diagnostics
**程序集:**System(在 system.dll 中)

语法

声明
Public MustInherit Class Switch
用法
Dim instance As Switch
public abstract class Switch
public ref class Switch abstract
public abstract class Switch
public abstract class Switch

备注

开关提供了使用外部设置在运行时控制跟踪和调试输出的有效机制。Switch 类实现开关的默认行为,并允许您在运行时更改开关级别。

此类为 BooleanSwitchSourceSwitchTraceSwitch 类的基类。这些开关可满足多数调试和跟踪的需要。如果您要创建自己的开关,则它们必须为 static

必须启用跟踪或调试才能使用开关。下面的语法是编译器特定的语法。如果使用 C# 或 Visual Basic 之外的编译器,请参见相应编译器的文档。

  • 若要在 C# 中启用调试,请在编译代码时将 /d:DEBUG 标志添加到编译器命令行,或者可以将 #define DEBUG 文件添加到文件的顶部。在 Visual Basic 中,将 /d:DEBUG=True 标志添加到编译器命令行。

  • 若要在 C# 中启用跟踪,请在编译代码时将 /d:TRACE 标志添加到编译器命令行,或者将 #define TRACE 添加到文件的顶部。在 Visual Basic 中,将 /d:TRACE=True 标志添加到编译器命令行。

若要设置开关级别,请编辑与应用程序的名称相对应的配置文件。在该文件中,可以添加开关并设置其值,移除开关或清除以前由应用程序设置的所有开关。应像下面的示例这样对配置文件进行格式化:

<configuration>
  <system.diagnostics>
    <switches>
      <add name="mySwitch" value="10" />
      <add name="myNewSwitch" value="20" />
      <remove name="mySwitch" />
      <clear/>
    </switches>
  </system.diagnostics>
</configuration>

提示

若要提高性能,可以在您的类中将 Switch 成员设置为 static

给继承者的说明 如果需要跟踪级别或者需要设置不同于 BooleanSwitchSourceSwitchTraceSwitch 所提供的开关级别的机制,可以从 Switch 继承。在从该类继承时,必须实现 SwitchSetting 方法。

示例

下面的示例显示如何定义可以用来跟踪调用堆栈的具有四个跟踪级别的新 Switch 类。可以使用开关配置应用程序使它在每次进入或退出方法时进行记录。

第一个示例创建用来设置开关级别的枚举。

' 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 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 class MethodTracingSwitchLevel
{
   Off = 0,
   EnteringMethod = 1,
   ExitingMethod = 2,
   Both = 3
};

// The following are possible values for the new switch.
public class MethodTracingSwitchLevel
{
    private int member;

    MethodTracingSwitchLevel()
    {
        member = 0;
    }//MethodTracingSwitchLevel

    MethodTracingSwitchLevel(int n)
    {
        member = n;
    }//MethodTracingSwitchLevel

    public int get_Member()
    {
        return member;
    }//get_Member

    public static int off = 0;
    public static int enteringMethod = 1;
    public static int exitingMethod = 2;
    public static int both = 3;
} //MethodTracingSwitchLevel

下面的示例将创建新开关。该代码实现 Level 属性来设置新开关的值。Level 调用受保护的属性 SwitchSetting,该属性将值赋给新开关。此示例还实现两个评估器属性来获取该开关的赋值。

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 'New
    
    
    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 'SetSwitchSetting
    
    
    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 'MyMethodTracingSwitch
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 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 extends Switch
{
    protected boolean outExit;
    protected boolean outEnter;
    protected MethodTracingSwitchLevel level;

    public MyMethodTracingSwitch(String displayName, String description)
    {
      super(displayName, description);
    } //MyMethodTracingSwitch

    /** @property
     */
    public MethodTracingSwitchLevel get_Level()
    {
        return level;
    }//get_Level

    /** @property 
     */
    public void set_Level(MethodTracingSwitchLevel value)
    {
        SetSwitchSetting(value.get_Member());
    }//set_Level

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

        level = new 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;
        }
    } //SetSwitchSetting

    /** @property 
     */
    public boolean get_OutputExit()
    {
        return outExit;
    }//get_OutputExit

    /** @property 
     */
    public boolean get_OutputEnter()
    {
        return outEnter;
    }//get_OutputEnter
} //MyMethodTracingSwitch

下面的示例在 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()
        ' Write a diagnostic message if the switch is set to entering.
        Debug.WriteLineIf(mySwitch.OutputEnter, "Entering Main")
        
        ' Insert code to handle processing.
        ' Write another diagnostic message if the switch is set to exiting.
        Debug.WriteLineIf(mySwitch.OutputExit, "Exiting Main")
    End Sub
End Class 'MyClass
public class MyClass {
    /* Create an instance of MyMethodTracingSwitch.*/
    static MyMethodTracingSwitch mySwitch = 
       new MyMethodTracingSwitch("Methods", "Trace entering and exiting method");
 
    public static int Main(string[] args) {
       // Write a diagnostic message if the switch is set to entering.
       Debug.WriteLineIf(mySwitch.OutputEnter, "Entering Main");
 
       // Insert code to handle processing.
 
       // Write another diagnostic message if the switch is set to exiting.
       Debug.WriteLineIf(mySwitch.OutputExit, "Exiting Main");
       return 0;
    }
 }
public ref class MyClass
{
private:

   /* Create an instance of MyMethodTracingSwitch.*/
   static MyMethodTracingSwitch^ mySwitch = gcnew MyMethodTracingSwitch( "Methods","Trace entering and exiting method" );

public:
   static int main()
   {
      
      // Write a diagnostic message if the switch is set to entering.
      Debug::WriteLineIf( mySwitch->OutputEnter, "Entering Main" );
      
      // Insert code to handle processing.
      // Write another diagnostic message if the switch is set to exiting.
      Debug::WriteLineIf( mySwitch->OutputExit, "Exiting Main" );
      return 0;
   }

};

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

    public static void main(String[] args)
    {
        // Write a diagnostic message if the switch is set to entering.
        Debug.WriteLineIf(mySwitch.get_OutputEnter(), "Entering main");
        // Insert code to handle processing.
        // Write another diagnostic message if the switch is set to exiting.
        Debug.WriteLineIf(mySwitch.get_OutputExit(), "Exiting main");
        return;
    } //main
} //MyClass

继承层次结构

System.Object
  System.Diagnostics.Switch
     System.Diagnostics.BooleanSwitch
     System.Diagnostics.SourceSwitch
     System.Diagnostics.TraceSwitch

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Switch 成员
System.Diagnostics 命名空间
BooleanSwitch 类
TraceSwitch
Debug 类
Trace