NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Switch Class

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

Object
   Switch

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

Remarks

A switch provides an efficient mechanism for enabling or disabling tracing and debug output at run time using external settings. The Switch class implements default behavior for switches, such as switch management mechanisms that allow you to automatically change the switch level at run time, cataloging, and integration with registry and environmental variables.

This class is the base class for BooleanSwitch and TraceSwitch. These two switches should meet most of your debugging and tracing needs.

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

For more information, see <DebugTopicTBD>.

Requirements

Namespace: System.Diagnostics

Assembly: System.dll

Example [Visual Basic]

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 method entry, exit, or both entry and exit.

[Visual Basic]

Enum MethodTracingSwitchLevel
   Off = 1
   EnteringMethod = 2
   ExitingMethod = 3
   Both = 4
End Enum
      
Class MethodTracingSwitch 
   Inherits SwitchBase
   Private outExit As Boolean
   Private outEnter As Boolean

   Public New(displayName As String, description As String) 
   MyBase.New(displayName, description)
   End Sub       
   
   Public Property Let Level As MethodTracingSwitchLevel
      SetSwitchSettings value  
   End Property
   
   Protected Override Sub SetSwitchSetting(value As Integer) 
      outExit = False
      outEnter = False
      If value = EnteringMethod Or value = Both
         outEnter = True
      Endif
      If value = ExitingMethod Or value = Both
         outExit = True
      Endif
   End Sub
   
   Public Property Get OutputExit
      OutputExit = outExit
   End Property
   
   Public Property Get OutputEnter
      OutputEnter = outEnter
   End Property
 
End Class

The following example implements the new switch in MyMethod. It outputs debugging messages when entering and leaving the method.

[Visual Basic]

Dim methodTracer As New MethodTracingSwitch( _
   ?methods?,?tracing entering entering and exiting?) 

Public Sub MyMethod(param As Integer) As Boolean
   Debug.WriteLineIf methodTracer.OutputEntering, _
      ?Entering MyMethod(? & param & ?)? 
   ?
   MyMethod = result
   ?
   Debug.WriteLineIf methodTracer.OutputExiting, _
      ?Exiting MyMethod = ? & result
End Sub

See Also

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