public abstract class Switch
|
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>
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.
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; } }
Description | Read-only Gets a description of the switch. |
DisplayName | Read-only Gets a name used to identify the switch. |
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. |
SwitchSetting | Read-write Gets or sets the current setting for this switch. |
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:
displayName
description
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>
public string Description {get;}
|
public string DisplayName {get;}
|
protected int SwitchSetting {get; set;}
|
~Switch(); |
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
protected virtual void OnSwitchSettingChanged(); |
public virtual string ToString(); |