// 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(); } }
Author: coder
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(); } }
Defensive Programming:Conditional Methods
using System; using System.Diagnostics; class MyClass { public MyClass(int i) { this.i = i; } [Conditional("DEBUG")] public void VerifyState() { if (i != 0) Console.WriteLine("Bad State"); } int i = 0; } public class DefensiveProgrammingConditionalMethods { public static void Main() { MyClass c = new MyClass(1); c.VerifyState(); } }
Demonstrate indenting debug messages
/* 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
/*
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
/* * 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> */
Tracing To A File
/* * 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.IO; using System.Diagnostics; namespace Client.Chapter_16___Debugging { public class TracingToAFile { [STAThread] static void Main(string[] args) { FileStream Log = new FileStream("Log.txt", FileMode.OpenOrCreate); Trace.Listeners.Add(new TextWriterTraceListener(Log)); Trace.WriteLine("My Trace String To Log File"); Trace.Flush(); Log.Close(); } } }