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.
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:
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
is created, and the DWORD value is set to 0 or 1 to set the value of the BooleanSwitch.
set _Switch_MyClassDebugOutput=1
VerifyState Start
VerifyState End
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\COMPlus\Switches\MyClassDebugOutput
How to Control Debug and Trace using BooleanSwitch
0 comments
Hide comments