This article illustrates how to enable your .NET application to publish its trace/debug messages on the network so that the desired clients can subscribe to it and view them in real-time as they occur. The core idea is to design a custom trace listener which would listen on a port for incoming client connections and broadcast the trace messages to the connected clients. You can also find this ongoing project here.
In .NET, we have objects called Trace Listeners. A listener is an object that receives the trace output and outputs it somewhere; that somewhere could be a window in your development environment, a file on your hard drive, a Windows Event log, an SQL Server or Oracle database, or any other customized data store.
You can think of a Trace Listener as a conduit for passing tracing information from your application to the output store. We write tracing information on Trace Listeners and Trace Listeners in turn output the tracing information to the target media.
Storing tracing information in a data store of some sort provides the flexibility of analyzing the data later on and helps us find the error and performance related patterns in our applications. The
System.Diagnostics namespace provides the interfaces, classes, enumerations and structures that are used for tracing, debugging, and other application and system diagnostic services. In this article, we will focus only on Tracing.
System.Diagnostics namespace provides two classes named
Debug that are used for writing errors and application execution information in logs. These classes are helpful during development (to output debug messages, etc.) and after deployment (to output performance related patterns, etc.). These two classes are the same, except that the methods of the
Trace class become part of the release build code whereas methods of the
Debug class don't become part of the release build executable.
When you create a new project, define the
DEBUG symbol to enable output with the
Debug class and the
TRACE symbol to enable output with the
Trace class. If you create a project in Visual Studio .NET, its Debug version will have these symbols defined.
Using the Code
Trace monitor is a diagnostics tool which facilitates you to see the trace output of your .NET application in real-time. This contains two components:
TcpTraceListener.dll is the plug-in library which will route your trace out to the specified port. TraceMonitor.exe is a simple TCP client which facilitates to see the trace output. Alternatively, you can always look at the trace output by making a telnet to the specified port.
Steps to Install
- Copy TcpTraceListener.dll and TraceMonitor.exe to a directory, say C:\Temp
- Register TcpTraceListener.dll to GAC if you need to use it in system wide, issuing gacutil command at Visual Studio command prompt. [
gacutil –I C:\Temp\TcpTraceListener.dll]
- Add reference to TcpTraceListener.dll for the project you desire. [I've done it for viewing the request and response XML in real-time]
- Add this to the list of trace listeners. [
Param.TraceListener.TcpTraceListener objTcpTraceListener = new Param.TraceListener.TcpTraceListener(9090); System.Diagnostics.Trace.Listeners.Add(objTcpTraceListener);]
- Write trace/debug statements as usual. [
- Run TraceMonitor.exe
- Use “Settings” menu to set the IP address and Port to which it should listen for trace output. [By default, its 127.0.0.1 and 9090]
- Say “Connect”. And, here you go…
- Alternatively, you can always use telnet.
- You can see the trace output in real-time as it occurs.
- Makes your life easy in debugging.
- Multiple users can see the trace output of a single application and of course vice-versa.
- 25th Feb, 2008 - Baseline (v1.0)
Paramesh Gunasekaran is currently working as a Software Engineer in HCL Technologies, India. He obtained his Bachelor's degree in Information Technology from Anna University, India. His research areas include Computational Biology, Artificial Neural Networks and Network Engineering. He has also received international acclaim for authoring industry papers in these areas. He is a Microsoft Certified Professional in ASP.NET/C# and has also been working in .NET technologies for more than 8 years.