Export (0) Print
Expand All
0 out of 3 rated this helpful - Rate this topic

Switch Class

Provides an abstract base class to create new debugging and tracing switches.

Namespace: System.Diagnostics
Assembly: System (in system.dll)

public abstract class Switch
public abstract class Switch
public abstract class Switch

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, SourceSwitch and the TraceSwitch classes. These switches meet most debugging and tracing needs. If you create your own 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 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>
NoteNote

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, SourceSwitch and TraceSwitch, you can inherit from Switch. When inheriting from this class, you must implement the SwitchSetting method.

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 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

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.

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 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

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 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 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

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

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.