Team LiB
Previous Section Next Section

Chapter 16: Debugging

Tracing

There are two types of tracing that you can do with the System.Diagnostics namespace. You can use the Trace class to place Trace statements in both the retail and debug builds, without requiring a recompilation. The Debug class exists only in the debug build of an application, but the overall goal and results are the same as using the Trace class.

Tip 

A healthy dose of tracing and debug logging in your application will save you many hours of support work and debugging. The Microsoft Exchange team included extended amounts of tracing in the transport components of Exchange 2000. This saved thousands of hours in troubleshooting and debugging.

By default, tracing information is sent to the debugger, but you have the following choices for listeners: debugger ( DefaultTraceListener ), file or console ( TextWriterTraceListener ), or event log ( EventLogTraceListener ).

You can enable tracing using one of these methods:

The following examples demonstrate tracing methods.

Note 

When tracing to a file, event log, the debugger, or console, you can use the Debug class instead of the Trace class if you want to do the tracing only in debug mode.

Code Example: Tracing to a File
Start example
using System;
using System.IO;
using System.Diagnostics;

namespace Client.Chapter_16___Debugging
{
          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();
               }
          }
}
End example
Code Example: Tracing to the Event Log
Start example

using System;
using System.IO;
using System.Diagnostics;

namespace Client.Chapter_16___Debugging
{
          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");
                }
          }
}
End example
Code Example: Tracing to the Debugger
Start example

using System;
using System.IO;
using System.Diagnostics;

namespace Client.Chapter_16___Debugging
{
          class TracingToDebugger
          {
                [STAThread]
                static void Main(string[] args)
                {
                      Trace.WriteLine("My Trace String");
                      Trace.Flush();
                }
          }
}
End example
Code Example: Tracing to the Console
Start example
using System;
using System.IO;
using System.Diagnostics;

namespace Client.Chapter_16___Debugging
{
          class Class1
          {
                [STAThread]
                static void Main(string[] args)
                {
                       Trace.Listeners.Add(
                          new TextWriterTraceListener(Console.Out));
                       Trace.WriteLine("My Trace to the console");
                }
          }
}
End example
Code Example: Using BooleanSwitch
Start example

using System;
using System.IO;
using System.Diagnostics;

namespace Client.Chapter_16___Debugging
{
          class UsingBooleanSwitch
          {
                 static BooleanSwitch MySwitch =
                    new BooleanSwitch("MyData", "MyModule");
                 [STAThread]
                 static void Main(string[] args)
                 {
                        MySwitch.Enabled = true;
                        if (MySwitch.Enabled)
                               Console.WriteLine("Error happened!");
                 }
          }
}
End example
Code Example: Using TraceSwitch and Config Files
Start example

using System;
using System.Diagnostics;

namespace Client.Chapter_16___Debugging
{
          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");
                }
          }
}
End example

The configuration file settings are as follows:

<?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>

The values settings in the configuration file are as follows:

Off

0

Error

1

Warnings & Error

2

Info, Warning & Error

3

Verbose

4


Team LiB
Previous Section Next Section