
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
444
|
Chapter 8: Diagnostics
3. In code, you can instantiate this custom class by invoking its constructor:
AppSpecificSwitch appSwitch = new AppSpecificSwitch("MyApplication",
"My Application Specific Switch");
4. Set the switch in the application configuration file. For example, the following
configuration file sets the level of your custom switch to
AppSpecificSwitchLevel.
CriticalLevel
:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<switches>
<add name="MyApplication" value="5" />
</switches>
</system.diagnostics>
</configuration>
More information on configuration files can be found in Recipes 8.1 and 8.2.
Discussion
The BooleanSwitch and TraceSwitch classes defined in the FCL might not always have
the flexibility that you need. In these cases, you can create a class that inherits from
the
Switch class—the abstract base class of all switch type classes.
The critical part of creating a custom switch class is the constructor. The construc-
tor must call its base class constructor using the
:base() syntax. If this syntax is
omitted, a compiler error will appear, indicating that there is no default constructor
to call on the base class
Switch. You might notice that the Switch class contains a sin-
gle public constructor that accepts two string parameters. This ...