# 跟踪应用程序和在应用程序中插入检测点Tracing and Instrumenting Applications

• 代码跟踪 - 接收运行时有关应用程序执行的信息性消息。Code tracing - Receiving informative messages about the execution of an application at run time.

• 调试 - 跟踪并修复开发中的应用程序的编程错误。Debugging - Tracking down and fixing programming errors in an application under development. 有关详细信息，请参阅调试For more information, see Debugging.

• 性能计数器 - 可用于跟踪应用程序性能的组件。Performance counters - Components that allow you to track the performance of your application. 有关更多信息，请参阅性能计数器For more information, see Performance Counters.

• 事件日志 - 可用于接收和跟踪应用程序执行过程中重要事件的组件。Event logs - Components that allow you receive and track major events in the execution of your application. 有关更多信息，请参见 EventLog 类。For more information, see the EventLog class.

TraceSource 类提供了增强的跟踪功能，可用来代替较旧的 TraceDebug 跟踪类的静态方法。The TraceSource class provides enhanced tracing features and can be used in place of the static methods of the older Trace and Debug tracing classes. 常见的 TraceDebug 类仍然使用广泛，但建议对新的跟踪命令（如 TraceEventTraceData）使用 TraceSource 类。The familiar Trace and Debug classes are still widely used, but the TraceSource class is recommended for new tracing commands, such as TraceEvent and TraceData.

TraceDebug 类相同，只不过 Trace 类的过程和函数默认编译为发布版本，Debug 类的并非如此。The Trace and Debug classes are identical, except that procedures and functions of the Trace class are compiled by default into release builds, but those of the Debug class are not.

TraceDebug 类提供了在开发期间或部署之后监视和检查应用程序性能的方法。The Trace and Debug classes provide the means to monitor and examine application performance either during development or after deployment. 例如，您可以使用 Trace 类跟踪在已部署的应用程序中发生的特定类型的操作（例如新建数据库连接），因而可以监视该应用程序的效率。For example, you can use the Trace class to track particular types of actions in a deployed application as they occur (for example, creation of new database connections), and can therefore monitor the application's efficiency.

## 代码跟踪和调试Code Tracing and Debugging

Trace.WriteLine("Hello World!")
Debug.WriteLine("Hello World!")

System.Diagnostics.Trace.WriteLine("Hello World!");
System.Diagnostics.Debug.WriteLine("Hello World!");


### 代码跟踪的阶段Phases of Code Tracing

1. 检测 - 向应用程序添加跟踪代码。Instrumentation — you add trace code to your application.

2. 跟踪 - 跟踪代码向指定目标写入相应信息。Tracing — the tracing code writes information to the specified target.

3. 分析 - 评估跟踪信息以确定和了解应用程序中的问题。Analysis — you evaluate the tracing information to identify and understand problems in the application.

##### 若要在应用程序中使用跟踪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 How to: Configure 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. 在 Visual Studio 中编译时，请使用此选项。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.

## 跟踪的输出Output from Tracing

AssertAssert 指定的文本；若未指定，则为调用堆栈。The specified text; or, if none is specified, the Call Stack. 只有指定为“Assert”语句中的自变量的条件为“false”时，才写入输出。The output is written only if the condition specified as an argument in the Assert statement is false.

WriteWrite 指定的文本。The specified text.
WriteIfWriteIf 如果满足指定为“WriteIf”语句中的自变量的条件，则为指定的文本。The specified text, if the condition specified as an argument in the WriteIf statement is satisfied.
WriteLineWriteLine 指定的文本和一个回车符。The specified text and a carriage return.
WriteLineIfWriteLineIf 如果满足指定为“WriteIf”语句中的自变量的条件，则为指定的文本和一个回车符。The specified text and a carriage return, if the condition specified as an argument in the WriteLineIf statement is satisfied.

Listeners 集合中所有的侦听器都会接收到上表中介绍的消息，但种类不同的侦听器在接到消息后所执行的操作可能也不同。All listeners in the Listeners collection receive the messages described in the above table, but the actions taken may vary depending on what kind of listener receives the message. 例如，DefaultTraceListener 将在接收到“Fail”或失败的“Assert”通知时显示一个断言对话框，而 TextWriterTraceListener 仅将输出写入它的流中。For example, the DefaultTraceListener displays an assertion dialog box when it receives a Fail or failed Assert notification, but a TextWriterTraceListener simply writes the output to its stream.

“Write”和“WriteLine”方法始终写入指定的文本。The Write and WriteLine methods always write the text that you specify. “Assert”、“WriteIf”和“WriteLineIf”需要布尔型自变量，该自变量控制它们是否写入指定的文本；只有在表达式为“true”（对于“WriteIf”和“WriteLineIf”），或“false”（对于“Assert”）时，它们才会写入指定的文本。Assert, WriteIf, and WriteLineIf require a Boolean argument that controls whether or not they write the specified text; they write the specified text only if the expression is true (for WriteIf and WriteLineIf), or false (for Assert). “Fail”方法始终写入指定的文本。The Fail method always writes the specified text. 有关详细信息，请参阅如何：向应用程序代码添加跟踪语句 和 .NET Framework 引用。For more information, see How to: Add Trace Statements to Application Code and the .NET Framework reference.