Instrumenting Applications with .NET Tracing Application instrumentation gives you the ability to perform runtime diagnosis of enterprise application state, which is critical to mission success. To help with instrumentation and logging, .NET ships with tracing types in the System.Diagnostics namespace. Using these types, you have the ability to log information to multiple output streams for diagnosis of application runtime behavior. Information produced by instrumentation and tracing types enable you to examine the runtime state of an application and fix problems that would be otherwise expensive and painful to solve. For many small and medium sized programs, it isn't too difficult to find and fix bugs based on reproducible information from users. As applications increase in size and complexity, the ability to figure out what is causing a bug becomes more difficult. On larger enterprise systems you need a way to track what is happening to find out what is causing problems. You must instrument your application so you can turn on tracing that will reveal pertinent information about your program's behavior. | " | The Trace class allows you to perform logging in a production system, giving you the ability to analyze application behavior during run time.
| " |
Two of the primary types in the System.Diagnostics namespace for application instrumentation are the Debug and Trace classes. Both classes have the same functionality but different use cases. Use the Debug class during development and use the Trace class in production applications. The types for debugging and tracing, which come with the .NET Framework library, are convenient because they provide reuse and extendable functionality. You don't have to create your own logging library and you can build custom instrumentation types. When tracing, you can control output with switches. A BooleanSwitch turns tracing on and off. TraceSwitch lets you trace at different levels. Alternatively, you can create a custom switch to define identifiers, granularity, and logic that meets the requirements of a given application. Trace output is sent to another type called a TraceListener. .NET ships with trace listeners for writing to the console, event log, or text files. You can also define your own trace listener for output to the stream of your choice. Instrumentation is a critical component of enterprise application development, enabling you to build maintainable, reliable, and robust systems. Through Debug and Trace classes, switches, and trace listeners, you have the ability to instrument your application in an easy and flexible manner. Configuration The Debug and Trace classes have the same functionality, but Microsoft designed them for different purposes. You use the Debug class for development purposes and it relies on DEBUG being defined. Use the Trace class for production purposes. It relies on TRACE being defined. By default, Visual Studio .NET (VS.NET) defines both DEBUG and TRACE for Debug (development) configurations and only TRACE for Release (production) configurations. To view your project configurations in VS.NET, right-click on the project, select Properties, select Configuration Properties, select the Build option, and view the Conditional Compilation Constants property. When compiling C# applications from the command-line, compilation configuration options are specified with the /define: or /d: option, as follows: csc.exe /d:TRACE MyApp.cs
| & | | 
By: Joe Mayo Joe Mayo is an author, independent consultant, and trainer specializing in .NET technologies. He operates the C# Station Web site (www.csharp-station.com) and is a Microsoft Most Valuable Professional (MVP). Joe is author of C# Unleashed (Sams) and C#Builder Kick Start (Sams). For more information about Joe, please visit www.mayosoftware.com.
jmayo@MayoSoftware.com | Fast Facts | | If you really want to know how a production application is running, try turning on .NET's tracing capabilities. You can use .NET's built-in tracing or build your own. | |
|