[ Team LiB ] |
Recipe 6.3 Creating Your Own Custom Switch ClassProblemThe BooleanSwitch and TraceSwitch classes defined in the FCL may not always have the required flexibility or fine-grained control that you need. You want to create you own switch class that provides the level of control and flexibility that you need. For example, creating a class that allows you to set more precise trace levels than those supported by the TraceSwitch class. The TraceSwitch class provides the following tracing levels:
However, you need a finer-grained set of levels, such as the following:
SolutionYou can create your own switch class that inherits from System.Diagnostics.Switch and provides the level of control that you need. For example, creating a class that allows you to set more precise trace levels than those supported by the TraceSwitch class involves the following steps:
More information on configuration files can be found in Recipe 6.1 and Recipe 6.2. DiscussionThe 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 constructor 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 single public constructor that accepts two string parameters. This is designed so that you must use this constructor when building an object of this type or any type derived from it. Calling the base class's constructor also allows the application configuration file to be searched, if one exists, for any initialization value for this switch object. We can circumvent the configuration file search by writing the constructor as follows: public AppSpecificSwitch(string displayName, string description) : base("", description) { this.Level = (AppSpecificSwitchLevel)base.SwitchSetting; } The other item of interest in this constructor is the one line of code in its body. This line of code grabs the level information acquired from the application configuration file and sets this inherited class's Level property to this value. This line is required because the base class is the one that receives the initialization information from a configuration file, not the inherited class. This class contains several other properties. The first is the Level property, which gets and sets the current level of this object. The levels are defined in the AppSpecificSwitchLevel enumeration. This class also contains a read-only property for each element in the AppSpecificSwitchLevel enumeration. These can be used to query this object to determine whether its various levels are set. See AlsoSee Recipe 6.1 and Recipe 6.2; see the "Switch Class" and "Trace and Debug Settings Schema" topics in the MSDN documentation. |
[ Team LiB ] |