Using Switches to Control Debug and Trace: BooleanSwitch : Debug « Development « C# / CSharp Tutorial






// compile with: csc /D:DEBUG /r:system.dll boolean.cs
using System;
using System.Diagnostics;

class MyClass
{
    public MyClass(int i)
    {
        this.i = i;
    }
    
    [Conditional("DEBUG")]
    public void VerifyState()
    {
        Debug.WriteLineIf(debugOutput.Enabled, "VerifyState Start");
        
        if (debugOutput.Enabled)
            Debug.WriteLine("VerifyState End");
    }
    
    BooleanSwitch debugOutput =  new BooleanSwitch("MyClassDebugOutput", "Control debug output");
    int i = 0;
}

class MainClass
{
    public static void Main()
    {
        Debug.Listeners.Clear();
        Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
        MyClass c = new MyClass(1);
        
        c.VerifyState();
    }
}








14.24.Debug
14.24.1.Debug Assert
14.24.2.Add Console.Out to TextWriterTraceListener
14.24.3.Set up a TraceListener to a file
14.24.4.Add EventLogTraceListener to Debug.Listener
14.24.5.Debug and Trace Output
14.24.6.Using Switches to Control Debug and Trace: BooleanSwitch
14.24.7.Using Switches to Control Debug and Trace: TraceSwitch