Using Switches to Control Debug and Trace:TraceSwitch

image_pdfimage_print
   

using System;
using System.Diagnostics;

class MyClass
{
    public MyClass(int i)
    {
        this.i = i;
    }
    
    [Conditional("DEBUG")]
    public void VerifyState()
    {
        Debug.WriteLineIf(debugOutput.TraceInfo, "VerifyState Start");
        
        Debug.WriteLineIf(debugOutput.TraceVerbose, 
        "Starting field verification");
        
        if (debugOutput.TraceInfo)
        Debug.WriteLine("VerifyState End");
    }
    
    static TraceSwitch    debugOutput = 
    new TraceSwitch("MyClassDebugOutput", "Control debug output");
    int i = 0;
}

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

           
          


Using Switches to Control Debug and Trace:BooleanSwitch

image_pdfimage_print
   

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

           
          


Debug and Trace Output

image_pdfimage_print
   

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();
    }
}

           
          


Demonstrate indenting debug messages

image_pdfimage_print
   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

// Indent.cs -- Demonstrate indenting debug messages.
//
//              Compile this program with the following command line:
//                  C:>csc /debug:full /d:DEBUG Indent.cs
using System;
using System.Diagnostics;

namespace nsDebugging
{
    public class Indent
    {
        static public void Main ()
        {
            Debug.Listeners.Clear ();
            Debug.Listeners.Add (new TextWriterTraceListener(Console.Out));
            Debug.AutoFlush = true;
            Debug.IndentSize = 5;
            Debug.WriteLine ("First level debug message.");
            Debug.Fail ("It failed!");
            FirstMethod ();
            Debug.WriteLine ("Return to first level debug message.");
        }
        static private void FirstMethod ()
        {
 //           Debug.Indent ();
            ++Debug.IndentLevel;
            Debug.WriteLine ("Second level debug message");
            SecondMethod ();
            Debug.WriteLine ("Return to second level debug message");
//            Debug.Unindent ();
            --Debug.IndentLevel;
        }
        static private void SecondMethod ()
        {
            Debug.Indent ();
            Debug.WriteLine ("Third level debug message.");
            Debug.Unindent  ();
        }
    }
}

           
          


A simple demonstration of the Debug class

image_pdfimage_print

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

// DebugTst.cs — A simple demonstration of the Debug class.
//
// Compile this program with the following command line:
// C:>csc /debug:full /d:DEBUG DebugTst.cs
using System;
using System.Diagnostics;
using System.IO;

namespace nsDebugTest
{
public class DebugTst
{
static void Main()
{
// Debug.Listeners.Clear();
// Debug.Listeners.Add (new TextWriterTraceListener(Console.Out));
// Debug.AutoFlush = true;
Debug.WriteLine (“Debug is on”);
clsTest test = new clsTest(42);
test.ShowValue();
}
}
class clsTest
{
public clsTest (int num)
{
m_Num = num;
}
int m_Num;

public void ShowValue()
{
try
{
DoSomething ();
}
catch (Exception e)
{
Console.WriteLine (e.StackTrace);
}
if (m_Num < 50) { Debug.WriteLine (m_Num + " is less than 50"); } } void DoSomething () { Debug.WriteLine (Environment.StackTrace); } } } [/csharp]

Tracing Example

image_pdfimage_print
   

/*
 * C# Programmers Pocket Consultant
 * Author: Gregory S. MacBeth
 * Email: gmacbeth@comporium.net
 * Create Date: June 27, 2003
 * Last Modified Date:
 * Version: 1
 */
using System;
using System.Diagnostics;

namespace Client.Chapter_16___Debugging
{
  public class TracingExample
  {
    static void Main(string[] args)
    {
      TraceSwitch General = new TraceSwitch("General", "Application Switch");

      Trace.WriteLineIf(General.TraceError, "General - Error Tracing Enabled");
      Trace.WriteLineIf(General.TraceWarning, "General - Warning Tracing Enabled");
      Trace.WriteLineIf(General.TraceInfo, "General - Info Tracing Enabled");
      Trace.WriteLineIf(General.TraceVerbose, "General - Verbose Tracing Enabled");

      TraceSwitch MyComponent = new TraceSwitch("MyComponent", "Application Switch");

      Trace.WriteLineIf(MyComponent.TraceError, "MyComponent - Error Tracing Enabled");
      Trace.WriteLineIf(MyComponent.TraceWarning, "MyComponent - Warning Tracing Enabled");
      Trace.WriteLineIf(MyComponent.TraceInfo, "MyComponent - Info Tracing Enabled");
      Trace.WriteLineIf(MyComponent.TraceVerbose, "MyComponent - Verbose Tracing Enabled");
    }
  }
}

/*
<?xml version="1.0" encoding="utf-8" ?> 
<configuration>
    <system.diagnostics>
        <switches>
            <add name="General" value="1" />
            <add name="MyComponent" value="3" />
        </switches>
    </system.diagnostics>
</configuration>
*/