Export (0) Print
Expand All

Switch Class

Provides an abstract (MustInherit in Visual Basic) base class to create new debugging and tracing switches.

For a list of all members of this type, see Switch Members.

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

[Visual Basic]
MustInherit Public Class Switch
[C#]
public abstract class Switch
[C++]
public __gc __abstract class Switch
[JScript]
public abstract class Switch

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

A switch provides an efficient mechanism for controlling tracing and debugging output at run time using external settings. The Switch class implements default behavior for switches, allowing you to change the switch level at run time.

This class is the base class for the BooleanSwitch and the TraceSwitch classes. These switches meet most debugging and tracing needs. If you create switches, they must be static (Shared in Visual Basic) .

You must enable tracing or debugging to use a switch. The following syntax is compiler specific. If you use compilers other than C# or Visual Basic, refer to the documentation for your compiler.

  • 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. In Visual Basic, add the /d:DEBUG=True flag to the compiler command line.
  • 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. 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="10" />
 <add name="myNewSwitch" value="20" />
 <remove name="mySwitch" />
 <clear/>
 </switches>
 </system.diagnostics>
 </configuration>
Note   To improve performance, you can make Switch members static (Shared in Visual Basic) in your class.

Notes to Inheritors:  If you need trace levels, or mechanisms for setting switch levels different from those provided by BooleanSwitch and TraceSwitch, you can inherit from Switch. When inheriting from this class, you must implement the SwitchSetting method.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic, C#, C++] The first example creates the enumeration used to set the level of the switch.

[Visual Basic] 
' The following are possible values for the new switch.
Public Enum MethodTracingSwitchLevel
    Off = 0
    EnteringMethod = 1
    ExitingMethod = 2
    Both = 3
End Enum 'MethodTracingSwitchLevel


[C#] 
// The following are possible values for the new switch.
 public enum MethodTracingSwitchLevel {
    Off = 0,
    EnteringMethod = 1,
    ExitingMethod = 2,
    Both = 3,
 }

[C++] 
// The following are possible values for the new switch.
public __value enum MethodTracingSwitchLevel {
   Off = 0,
   EnteringMethod = 1,
   ExitingMethod = 2,
   Both = 3,
};

[Visual Basic, C#, C++] The following example creates the new switch. The code implements a Level property to set the value of the new switch. 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.The following example creates a new switch in

[Visual Basic] 
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


[C#] 
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;
         }
     }
 }

[C++] 
public __gc class MyMethodTracingSwitch:public Switch {
protected:
   bool outExit;
   bool outEnter;
   MethodTracingSwitchLevel level;

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

   __property MethodTracingSwitchLevel get_Level(){
      return level;
   }
   __property void set_Level( 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 get_OutputExit(){
      return outExit;
   }

   __property bool get_OutputEnter(){
      return outEnter;
   }

};

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.

[Visual Basic] 
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

[C#] 
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;
    }
 }

[C++] 
public __gc class MyClass {
   /* Create an instance of MyMethodTracingSwitch.*/
   static MyMethodTracingSwitch* mySwitch = 
      new MyMethodTracingSwitch(S"Methods", S"Trace entering and exiting method");
public:
   static int main() {
      // Write a diagnostic message if the switch is set to entering.
      Debug::WriteLineIf(mySwitch->OutputEnter, S"Entering Main");

      // Insert code to handle processing.

      // Write another diagnostic message if the switch is set to exiting.
      Debug::WriteLineIf(mySwitch->OutputExit, S"Exiting Main");
      return 0;
   }
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Diagnostics

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

Switch Members | System.Diagnostics Namespace | BooleanSwitch | TraceSwitch | Debug | Trace

Show:
© 2014 Microsoft