Search:
Locator+ Code:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Back to VSLive! Show Daily Home


Capture Messages With Trace Listeners
Use the predefined Trace listener objects to capture Debug and Trace messages outside Visual Studio .NET.
by Dave Bost

VSLive! SF, Day 3, February 14, 2002 — When using Debug and Trace messages in your application, the output messages are sent to any Listener object defined in the Listeners collection. When debugging and tracing is enabled, a default listener class called DefaultTraceListener is automatically created and initialized. The DefaultTraceListener object will output any messages received to the Output window if you're running your application in Visual Studio .NET, under debug mode.

If you want to capture your Trace messages outside VS.NET, you can use the other predefined Listener objects: TextWriterTraceListener and EventLogTraceListener. TextWriterTraceListener will redirect any output to an instance of the TextWriter class or to any object that is a Stream class. The EventLogTraceListener will redirect any output to the event log. To take advantage of these objects, create an instance of the listener type and add it to the Listeners collection. Any messages sent through the Debug and Trace objects will be directed through each Listener in the Listeners collection. Debug and Trace share the same Listeners collection, so any Listener defined in the Trace.Listeners collection is also defined in the Debug.Listeners collection. Here's how it works:

' Create a TextWriteTraceListener to 
' capture all Debug and Trace messages

' Define our tracing log file
Dim traceLog As New _
	System.IO.FileStream( _
	"C:\MyAppsTraceLog.log", _
	IO.FileMode.OpenOrCreate)

' Instantiate a new 
' TextWriterTraceListener and
' specify the output location
Dim traceListener As New _
	TextWriterTraceListener(traceLog)

' add Listener to the Listeners 
' collection
Trace.Listeners.Add(traceListener)

' send our tracing messages
Trace.WriteLine("Trace Message 1")
Debug.WriteLine("You are here.")
Trace.WriteLine("Trace Message 2")

' flush the listners buffer and close 
' the output
Trace.Flush()
traceLog.Close()

About the Author
Dave Bost is a senior consultant with Solution Partners Inc., based in Naperville, Ill. He serves as an application architect and mentors his clients in developing applications with the .NET platform. Dave is also a frequent speaker for conferences such as VBITS and Microsoft's DevDays. You can reach Dave at dbost@solpart.com.

Back to top




Java Pro | .NET Magazine | Visual Studio Magazine | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home