/* 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 (); } } }
Development Class
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(); } } }
Trace class: listener and writeline
/* * 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 Class1Chapter_16___Debugging1 { [STAThread] static void Main(string[] args) { Trace.Listeners.Add(new TextWriterTraceListener(Console.Out)); Trace.WriteLine("My Trace to the console"); } } }
Trace to debuger: writeline and flush
/* * 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 TracingToDebugger { [STAThread] static void Main(string[] args) { Trace.WriteLine("My Trace String"); Trace.Flush(); } } }
Trace to event log
/* * 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 TracingToEventLog { [STAThread] static void Main(string[] args) { //You can change the listener with the following code EventLogTraceListener EventListener = new EventLogTraceListener("MyApp"); Trace.Listeners.Add(EventListener); Trace.WriteLine("My Trace String To Console"); } } }