How to: Trace Code in an Application
The Trace class allows you to instrument your application. You can receive informative messages from your running application that can help diagnose problems or analyze performance. The following is an overall view of the major steps typically involved in using tracing to analyze and correct potential problems in deployed applications. For more information on how to perform these steps, see the appropriate link.
To use tracing in an application
Consider which tracing output you will want to receive onsite after you have deployed the application.
Create a set of switches. For more information, see Creating and Initializing Trace Switches.
Add the trace statements to the application code.
Determine where you want the tracing output to appear and add the appropriate listeners. For more information, see Creating and Initializing Trace Listeners.
Test and debug your application and the tracing code it contains.
Compile the application into executable code using one of the following procedures:
Use the Build menu along with the Debug page of the Property Pages dialog box in Solution Explorer. Use this when compiling in Visual Studio.
- or -
Use the Trace and Debug compiler directives for the command-line method of compiling. For more information, see Compiling Conditionally with Trace and Debug. Use this when compiling from the command line.
If a problem occurs during run time, turn on the appropriate trace switch. For more information, see Configuring Trace Switches.
The tracing code writes tracing messages to a specified target, for example, a screen, a text file, or an event log. The type of listener you included in the Trace.Listeners collection determines the target.
Analyze the tracing messages to identify and understand the problem in the application.