Using Switches to Control Debug and Trace:User-Defined Switch : Debug Trace « Development Class « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Development Class » Debug TraceScreenshots 
Using Switches to Control Debug and Trace:User-Defined Switch
 
// compile with: csc /r:system.dll file_1.cs

using System;
using System.Diagnostics;

enum SpecialSwitchLevel
{
    Mute = 0,
    Terse = 1,
    Verbose = 2,
    Chatty = 3
}

class SpecialSwitch: Switch
{
    public SpecialSwitch(string displayName, string description:
    base(displayName, description)
    {
    }
    
    public SpecialSwitchLevel Level
    {
        get
        {
            return((SpecialSwitchLevelbase.SwitchSetting);
        }
        set
        {
            base.SwitchSetting = (intvalue;
        }    
    }
    public bool Mute
    {
        get
        {
            return(base.SwitchSetting == 0);
        }
    }
    
    public bool Terse
    {
        get
        {
            return(base.SwitchSetting >= (int) (SpecialSwitchLevel.Terse));
        }
    }
    public bool Verbose
    {
        get
        {
            return(base.SwitchSetting >= (intSpecialSwitchLevel.Verbose);
        }
    }
    public bool Chatty
    {
        get
        {
            return(base.SwitchSetting >=(intSpecialSwitchLevel.Chatty);
        }
    }
    
    protected new int SwitchSetting
    {
        get
        {
            return((intbase.SwitchSetting);
        }
        set
        {
            if (value < 0)
            value = 0;
            if (value > 4)
            value = 4;
            
            base.SwitchSetting = value;
        }
    }
}

class MyClass
{
    public MyClass(int i)
    {
        this.i = i;
    }
    
    [Conditional("DEBUG")]
    public void VerifyState()
    {
        Console.WriteLine("VerifyState");
        Debug.WriteLineIf(debugOutput.Terse, "VerifyState Start");
        
        Debug.WriteLineIf(debugOutput.Chatty, 
        "Starting field verification");
        
        if (debugOutput.Verbose)
        Debug.WriteLine("VerifyState End");
    }
    
    static SpecialSwitch    debugOutput = 
    new SpecialSwitch("MyClassDebugOutput""application");
    int i = 0;
}

public class TraceUserDefinedSwitch
{
    public static void Main()
    {
        //TraceSwitch ts = new TraceSwitch("MyClassDebugOutput", "application");
        //Console.WriteLine("TraceSwitch: {0}", ts.Level);
        
        Debug.Listeners.Clear();
        Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
        MyClass c = new MyClass(1);
        
        c.VerifyState();
    }
}

           
         
  
Related examples in the same category
1.Using BooleanSwitchUsing BooleanSwitch
2.Debug class
3.Debug and ProfileDebug and Profile
4.Trace to event log
5.Trace to debuger: writeline and flush
6.Trace class: listener and writeline
7.Tracing To A File
8.Tracing Example
9.demonstrates debug outputdemonstrates debug output
10.illustrate the use of the debuggerillustrate the use of the debugger
11.A simple demonstration of the Debug class
12.Demonstrate indenting debug messages
13.Demonstrates routing debug messages to a fileDemonstrates routing debug messages to a file
14.Defensive Programming:Conditional Methods
15.Debug and Trace Output
16.Using Switches to Control Debug and Trace:BooleanSwitch
17.Using Switches to Control Debug and Trace:TraceSwitch
18.Object Dumper
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.