Use the Trace class with EventLog & TraceLog listeners to debug runtime code

Debug and Trace in the .NET Framework's System.Diagnostics namespace help you find bugs in applications at both design and run time. Both have the same methods and properties. Debug lets you display a variable's content and debug messages at design time. Trace traces application information at run time.

Method

Description

Write

Write a message to the listener application.

WriteIf

Same as Write if the specified condition is True.

WriteLine

Same as Write adding a carriage return

WriteLineIf

Same as WriteLine if the specified condition is True.

Assert

Display a messagebox if the specified condition is False.

Fail

Same as Assert but unconditional.

Debugging code can bog down your application. The .NET compiler can strip debug and trace calls from your release code by unchecking Define DEBUG Constant and Define TRACE Constant on the dialog presented when you select Project | Properties | Configuration Properties | Build.

However, .NET lets you conditionally enable this functionality via switches in your application's configuration file. To debug your release executable just set a value in its configuration file.

The BooleanSwitch can be used as the condition in the WriteIf, WriteLineIf, and Assert statements. For example, use this configuration file:

    <configuration>
        <system.diagnostics>
            <switches>
                <add name="myBooleanSwitch" value="1"/>
            <switches/>
        <system.diagnostics/>
    <configuration/>

and this code to write a message to the listener application (more about this later):

    Imports System.Diagnostics

    Dim bs As New BooleanSwitch("myBooleanSwitch", "Description for myBooleanSwitch")

    Trace.WriteLineIf(bs.Enabled, "About to run code in method abc at line 123")

To turn off tracing, set myBooleanSwitch equal to zero.

The TraceSwitch is used to control the severity of messages that are output as show in this table:

Value

Description

0

Tracing is disabled.

1

TraceError: Only error messages are output.

2

TraceWarning: Error and warning messages are output.

3

TraceInformation: Error, warning and informational messages are output.

4

TraceVerbose: All messages are output.

The following causes an error message to be written if the Catch block is executed. The message in the Try block will not be output because it's an Information message and the config file says only output error messages.

    <configuration>
        <system.diagnostics>
            <switches>
                <add name="myTraceSwitch" value="1"/>
            <switches/>
        <system.diagnostics/>
    <configuration/>

    Imports System.Diagnostics

    Dim ts As New TraceSwitch("myTraceSwitch", "Description for myTraceSwitch")

    Try
       Trace.WriteLineIf(ts.TraceInformation, "About to run code in method abc at line 123")
        ...
    Catch Ex As Exception
       Trace.WriteLineIf(ts.Error, "Code in method abc at line 123 failed")
    End Try

By default trace output goes to VS.NET's Output window. .NET's EventLogListener and TextWriterListener classes can redirect it to the Windows Event Log and a text file respectively. This snippet creates a new Windows Event Log Listener and adds it to the Listeners collection:

    Imports System.Diagnostics

    '
    ' Remove the default listener and add the new one.
    '
    Dim ev As New EventLogTraceListener("TraceLog")
    Trace.Listeners.RemoveAt(0)
    Trace.Listeners.Add(ev)

The constructor of the TextWriterListener class takes a Stream object such as Console.Out or the FileStream class. This code writes output to a text file.

    Imports System.Diagnostics
    
    Dim ts As New TraceSwitch("myTraceSwitch", "Description for myTraceSwitch")
    Dim tw As New TextWriterListener("C:\myLogFile.log")
    Trace.Listeners.RemoveAt(0)
    Trace.Listeners.Add(tw)
    . . . 
    Trace.WriteLineIf(ts.Error, "Code in method abc at line 123 failed")
    . . . 
    Trace.WriteLineIf(ts.Error, "Code in method xyx at line 57 failed")
    . . . 
    Trace.Close()

To learn how to use the EventLog class to read to and write from the Windows Event Logs, click here.




About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page