Using Switches to Control Debug and Trace:BooleanSwitch : Debug Trace « Development Class « C# / C Sharp






Using Switches to Control Debug and Trace:BooleanSwitch

 
// 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;
}

public class UsingSwitchestoControlDebugandTraceBooleanSwitch
{
    public static void Main()
    {
        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:TraceSwitch
17.Using Switches to Control Debug and Trace:User-Defined Switch
18.Object Dumper