4.9.09

"Debugging" and "Tracing"

Tracing in .NET

So how has tracing been implemented in .NET? 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, a 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.

The System.Diagnostics namespace provides two classes named Trace and Debug that are used for writing errors and application execution information in logs. These classes are helpful during the 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.



The System.Diagnostics namespace provides some Trace Listeners. The default Trace Listener is System.Diagnostics.DefaultTraceListener. Write and WriteLine methods of this class route the tracing information to the OutputDebugString and to the Log method of the attached debugger.

.NET framework also introduced the concept of Trace Switches. As the name implies, these are simply switches whose values can be controlled from outside of the application. What I meant by outside of the application is that once an application is compiled and converted to .exe or .DLL files, we don't have to change our code in order to change the values of these switches; we can just update the corresponding entries in .config files and the corresponding switches in our applications will have the updated values. To be more precise,

Trace Switches are simple objects that can be controlled externally through the .config files.

We will learn more about Trace Listeners and Trace Switches as we progress through the article. Following is a simple example of WriteLine method of the Trace and Debug classes.


Trace.WriteLine ("Hello 15Seconds Reader - Trace!") ;
Debug.WriteLine ("Hello 15Seconds Reader - Debug!") ;

When used in debug mode, both of these methods direct the tracing and debugging output to the output windows of the debugger; as shown in the following figure



NEED TO GET MORE ON "http://www.15seconds.com/Issue/020910.htm"

No comments: