How to Control Debug and Trace using BooleanSwitch

In this example, an instance of BooleanSwitch is created as a static member of the class, and this variable is used to control whether output happens. If this code is run, it produces no output, but t

ITPro Today

July 21, 2004

1 Min Read
ITPro Today logo in a gray background | ITPro Today

In this example, an instance of BooleanSwitch is created as a static member of the class, and this variable is used to control whether output happens. If this code is run, it produces no output, but thedebugOutput variable can be controlled by setting an environment variable.

set _Switch_MyClassDebugOutput=1

The environment variable name is created by prepending “_Switch_” in front of the display name (first parameter) of the constructor for BooleanSwitch. Running the code after setting this variable produces the following output:

VerifyState Start 
VerifyState End 

The code in VerifyState shows two ways of using the variable to control output. The first usage passes the flag off to the WriteLineIf() function and is the simpler one to write. It’s a bit less efficient, however, since the function call to WriteLineIf() is made even if the variable is false. The second version, which tests the variable before the call, avoids the function call and is therefore slightly more efficient.

The value of a BooleanSwitch variable can also be set through the Windows Registry. For this example, a new DWORD value with the key

HKEY_LOCAL_MACHINESOFTWAREMicrosoftCOMPlusSwitchesMyClassDebugOutput 

is created, and the DWORD value is set to 0 or 1 to set the value of the BooleanSwitch.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like