Partilhar via


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

  1. Consider which tracing output you will want to receive onsite after you have deployed the application.

  2. Create a set of switches. For more information, see Creating and Initializing Trace Switches.

  3. Add the trace statements to the application code.

  4. Determine where you want the tracing output to appear and add the appropriate listeners. For more information, see Creating and Initializing Trace Listeners.

  5. Test and debug your application and the tracing code it contains.

  6. 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.

  7. 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.

  8. Analyze the tracing messages to identify and understand the problem in the application.

See Also

Tasks

How to: Add Trace Statements to Application Code
How to: Compile Conditionally with Trace and Debug

Concepts

Introduction to Instrumentation and Tracing
Trace Switches
Trace Listeners

Other Resources

Tracing and Instrumenting Applications