System.Diagnostics.Switch Class

Assembly: System.dll
Namespace: System.Diagnostics
Summary
Provides an abstract base class to create new debugging and tracing switches.
C# Syntax:
public abstract class Switch
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.

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 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 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 Switch.SwitchSetting method.
Example
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 MethodTracingSwitchLevel {
    Off = 0,
    EnteringMethod = 1,
    ExitingMethod = 2,
    Both = 3,
 }

    

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 Switch.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
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;
         }
     }
 }

    
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 class MyClass {
    /* Create an instance of MyMethodTracingSwitch. This switch is set
     * by using values stored in the registry or in environmental variables. */
    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;
    }
 }

    
See also:
System.Diagnostics Namespace | BooleanSwitch | TraceSwitch | Debug | Trace

System.Diagnostics.Switch Member List:

Public Properties
Description Read-only

Gets a description of the switch.
DisplayName Read-only

Gets a name used to identify the switch.
Public Methods
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Constructors
ctor #1 Initializes a new instance of the Switch class.
Protected Properties
SwitchSetting Read-write

Gets or sets the current setting for this switch.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.
OnSwitchSettingChanged Raises the SwitchSettingChanged event.

Hierarchy:


System.Diagnostics.Switch Member Details

ctor #1
Summary
Initializes a new instance of the Switch class.
C# Syntax:
protected Switch(
   string displayName,
   string description
);
Parameters:

displayName

The name of the switch.

description

The description for the switch.

Remarks
When you create a new Switch object, the value of the displayName parameter is used to find initial switch settings. The default value is an empty string ("").

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 in your class.

Notes to inheritors: To set the value of the switch, use the Switch.SwitchSetting method during initialization by the constructor.
See also:
Switch | BooleanSwitch | TraceSwitch | Debug | Trace

Return to top


Property: Description (read-only)
Summary
Gets a description of the switch.
C# Syntax:
public string Description {get;}
Remarks
This property should indicate the function of the switch. For example, "Enables tracing for a directory watcher component."
See also:
Switch | BooleanSwitch | TraceSwitch | Debug | Trace

Return to top


Property: DisplayName (read-only)
Summary
Gets a name used to identify the switch.
C# Syntax:
public string DisplayName {get;}
Remarks
When you create a new Switch object, the Switch.DisplayName finds initial switch settings. For more information, see the Switch.#ctor constructor and the TraceSwitch Configuration topic in the Visual Studio documentation.
See also:
Switch | BooleanSwitch | TraceSwitch | Debug | Trace

Return to top


Property: SwitchSetting (read-write)
Summary
Gets or sets the current setting for this switch.
C# Syntax:
protected int SwitchSetting {get; set;}
See also:
Switch | BooleanSwitch | TraceSwitch | Debug | Trace

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~Switch();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: OnSwitchSettingChanged()
Summary
Raises the SwitchSettingChanged event.
C# Syntax:
protected virtual void OnSwitchSettingChanged();
Remarks
The SwitchSettingChanged event is raised any time the Switch.SwitchSetting property is changed. It is invoked the first time a switch reads its value from the configuration file and is invoked again each time the switch's value is changed.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.