TraceListener Classes in VB.NET

In this article I will explain you about the TraceListener Classes in VB.NET
  • 4620

The Listener property of the Trace and Debug classes retrieves the active Listeners collection. The TraceListener-derived classes control the tracing output. The members of this class are shown in Table 21.6.

Table 21.6: TraceListener Class Members

table21.6.gif

The TextWriterTraceListener sends output to a text file, while the EventLogTraceListener sends output to the event log. In other words, we can say that the listeners monitor trace and debug output and generate formatted output of the trace. By default, the output of all methods is directed to the DefaultTraceListener. Therefore, the environment that executes your code sets where the DefaultTraceListener output is directed. The TraceListener class (the thread-safe parent class for other listeners) has the following subclasses:

  • DefaultTraceListener. Sends your debugging or tracing write output through the OutputDebugString API function of the operating system. If you have no active debugger, OutputDebugString will not do anything. If you are inside the Visual Studio .NET integrated development environment, it will output the trace output to the debug window. If you are in console mode, you should use the Debug Monitor utility (DbMon.exe) of the Platform SDK. The Debug Monitor has its own console window and displays debugging and tracing messages sent by your application using the OutputDebugString-the DefaultListener-function.
  • EventLogTraceListener. Sends tracing or debugging output to the event log.
  • TextWriterTraceListener. Sends tracing or debugging output to Console.Out, FileStream, or other Stream and TextWriter classes.

Listing 21.14 shows a typical application configuration file with various diagnostic settings.

    <configuration>
        <
system.diagnostics>
            <
trace autoflush="true" indentsize="1">
                <
listeners>
                    <
add name="myTextFileListener"
                    type="System.Diagnostics.TextWriterTraceListener,System"
                    initializeData="c:\mylog.txt" />
                    <
remove type="System.Diagnostics.DefaultTraceListener,System"/>
                </
listeners>
            </
trace>
        </
system.diagnostics>
    </
configuration>

If you want to remove a listener that you created, use the Remove method in the Listeners collection. For example, to remove the DefaultListener use the following code:

Debug.Listeners.Remove("Default")

The example in Listing 21.15 shows how you can use TraceListeners. The application outputs trace code to the system console, to a file, and to the event log (a new event source named Demo) separately to illustrate all of the possible TraceListener styles. Note that you can replace the Debug class with the Trace class if you wish in the example.

Listing 21.15: Using TraceListeners

    Imports System.IO
        Public Class Test
            Public Shared Sub Main()
                Dim myWriter As New TextWriterTraceListener(Console.Out)
                Debug.Listeners.Add(myWriter)
                Debug.WriteLine("Test output 1 ")\
                Dim myFile As Stream = File.Create("output.txt")
                Dim myTextListener As New TextWriterTraceListener(myFile)
                Debug.Listeners.Add(myTextListener)
                Debug.WriteLine("Test output 2 ")
                If Not EventLog.SourceExists("Demo") Then
                    EventLog.CreateEventSource("Demo", "Demo")
                End If
                Debug.Listeners.Add(New EventLogTraceListener("Demo"))
                Debug.WriteLine("Test output 3 ")
                myWriter.Flush()
                myWriter.Close()
                myFile.Flush()
                myFile.Close()
            End Sub
        End
Class

Conclusion

Hope this article would have helped you in understanding the TraceListener Classes in VB.NET

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.