Debug and Trace Output : Debug Trace « Development Class « C# / C Sharp






Debug and Trace Output

 
using System;
using System.Diagnostics;

class MyClass
{
    public MyClass(int i)
    {
        this.i = i;
    }
    
    [Conditional("DEBUG")]
    public void VerifyState()
    {
        Debug.WriteLineIf(debugOutput, "In VerifyState");
        Debug.Assert(i == 0, "Bad State");
    }
    
    static public bool DebugOutput
    {
        get
        {
            return(debugOutput);
        }
        set
        {
            debugOutput = value;
        }
    }
    
    int i = 0;
    static bool debugOutput = false;
}

public class DebugandTraceOutput
{
    public static void Main()
    {
        Debug.Listeners.Clear();
        Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
        MyClass c = new MyClass(1);
        
        c.VerifyState();
        MyClass.DebugOutput = true;
        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.Using Switches to Control Debug and Trace:BooleanSwitch
16.Using Switches to Control Debug and Trace:TraceSwitch
17.Using Switches to Control Debug and Trace:User-Defined Switch
18.Object Dumper