Skip to Content
C# Cookbook
book

C# Cookbook

by Stephen Teilhet, Jay Hilyard
January 2004
Beginner to intermediate
864 pages
22h 18m
English
O'Reilly Media, Inc.
Content preview from C# Cookbook

6.3. Creating Your Own Custom Switch Class

Problem

The 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:

TraceError
TraceWarning
TraceInfo
TraceVerbose

However, you need a finer-grained set of levels, such as the following:

Disable
MinorError
Note
MediumError
Warning
CriticalError

Solution

You 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:

  1. Define a set of enumerated values that represent the levels to be supported by your switch class:

    public enum AppSpecificSwitchLevel 
    {
        Disable = 0,
        Note = 1,
        Warning = 2,
        MinorError = 3,
        MediumError = 4,
        CriticalError = 5
    }
  2. Define a class, such as AppSpecificSwitch, that inherits from System.Diagnostics.Switch:

    public class AppSpecificSwitch : Switch { protected AppSpecificSwitchLevel level = 0; public AppSpecificSwitch(string displayName, string description) : base(displayName, description) { this.Level = (AppSpecificSwitchLevel)base.SwitchSetting; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# Cookbook

C# Cookbook

Joe Mayo
C# Cookbook, 2nd Edition

C# Cookbook, 2nd Edition

Jay Hilyard, Stephen Teilhet
ASP.NET Cookbook

ASP.NET Cookbook

Michael A Kittel, Geoffrey T. LeBlond

Publisher Resources

ISBN: 0596003390Supplemental ContentCatalog PageErrata