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 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_MACHINE\SOFTWARE\Microsoft\COMPlus\Switches\MyClassDebugOutput 

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

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish