使用 Azure 诊断跟踪云服务应用程序的流Trace the flow of a Cloud Services application with Azure Diagnostics

跟踪是在应用程序运行时监视其执行情况的一种方式。Tracing is a way for you to monitor the execution of your application while it is running. 可以使用 System.Diagnostics.TraceSystem.Diagnostics.DebugSystem.Diagnostics.TraceSource 类在日志、文本文件或其他设备中记录与错误及应用程序执行情况相关的信息,供以后进行分析。You can use the System.Diagnostics.Trace, System.Diagnostics.Debug, and System.Diagnostics.TraceSource classes to record information about errors and application execution in logs, text files, or other devices for later analysis. 有关跟踪的详细信息,请参阅 跟踪和检测应用程序For more information about tracing, see Tracing and Instrumenting Applications.

使用 Trace 语句和 Trace 开关Use trace statements and trace switches

通过将 DiagnosticMonitorTraceListener 添加到应用程序配置,并在应用程序代码中调用 System.Diagnostics.Trace 或 System.Diagnostics.Debug,在云服务应用程序中实施跟踪。Implement tracing in your Cloud Services application by adding the DiagnosticMonitorTraceListener to the application configuration and making calls to System.Diagnostics.Trace or System.Diagnostics.Debug in your application code. 对辅助角色使用配置文件 app.config,对 Web 角色使用配置文件 web.config。Use the configuration file app.config for worker roles and the web.config for web roles. 使用 Visual Studio 模板创建新的托管服务时,系统会针对添加的角色将 Azure 诊断自动添加到项目,并将 DiagnosticMonitorTraceListener 添加到相应的配置文件。When you create a new hosted service using a Visual Studio template, Azure Diagnostics is automatically added to the project and the DiagnosticMonitorTraceListener is added to the appropriate configuration file for the roles that you add.

有关如何放置 Trace 语句的信息,请参阅 如何:向应用程序代码添加 Trace 语句For information on placing trace statements, see How to: Add Trace Statements to Application Code.

通过在代码中放置 Trace 开关 ,可以控制是否进行跟踪以及跟踪的范围。By placing Trace Switches in your code, you can control whether tracing occurs and how extensive it is. 这样可以在生产环境中监视应用程序的状态。This lets you monitor the status of your application in a production environment. 这在使用多台计算机上运行的多个组件的业务应用程序中尤其重要。This is especially important in a business application that uses multiple components running on multiple computers. 有关详细信息,请参阅 如何:配置 Trace 开关For more information, see How to: Configure Trace Switches.

在 Azure 应用程序中配置跟踪侦听器Configure the trace listener in an Azure application

Trace、Debug 和 TraceSource 都需要设置“侦听器”来收集和记录发送的消息。Trace, Debug and TraceSource, require you set up "listeners" to collect and record the messages that are sent. 侦听器可收集、存储和路由跟踪消息。Listeners collect, store, and route tracing messages. 它们将跟踪输出传输到适当的目标,如日志、窗口或文本文件。They direct the tracing output to an appropriate target, such as a log, window, or text file. Azure 诊断使用 DiagnosticMonitorTraceListener 类。Azure Diagnostics uses the DiagnosticMonitorTraceListener class.

完成以下过程之前,必须初始化 Azure 诊断监视器。Before you complete the following procedure, you must initialize the Azure diagnostic monitor. 若要执行此操作,请参阅在 Azure 中启用诊断To do this, see Enabling Diagnostics in Azure.

请注意,如果使用 Visual Studio 提供的模板,自动添加侦听器的配置。Note that if you use the templates that are provided by Visual Studio, the configuration of the listener is added automatically for you.

添加跟踪侦听器Add a trace listener

  1. 打开角色的 web.config 或 app.config 文件。Open the web.config or app.config file for your role.

  2. 将以下代码添加到文件。Add the following code to the file. 更改 Version 属性,以使用引用的程序集的版本号。Change the Version attribute to use the version number of the assembly you are referencing. 除非有更新,否则程序集版本不一定随着每个 Azure SDK 发行版而改变。The assembly version does not necessarily change with each Azure SDK release unless there are updates to it.

    <system.diagnostics>
        <trace>
            <listeners>
                <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener,
                  Microsoft.WindowsAzure.Diagnostics,
                  Version=2.8.0.0,
                  Culture=neutral,
                  PublicKeyToken=31bf3856ad364e35"
                  name="AzureDiagnostics">
                    <filter type="" />
                </add>
            </listeners>
        </trace>
    </system.diagnostics>
    

    Important

    确保与 Microsoft.WindowsAzure.Diagnostics 程序集建立项目引用。Make sure you have a project reference to the Microsoft.WindowsAzure.Diagnostics assembly. 更新上述 xml 中的版本号,以便与引用的 Microsoft.WindowsAzure.Diagnostics 程序集的版本匹配。Update the version number in the xml above to match the version of the referenced Microsoft.WindowsAzure.Diagnostics assembly.

  3. 保存 config 文件。Save the config file.

有关侦听器的详细信息,请参阅 跟踪侦听器For more information about listeners, see Trace Listeners.

完成添加侦听器步骤后,可以将 Trace 语句添加到代码。After you complete the steps to add the listener, you can add trace statements to your code.

将 Trace 语句添加到代码To add trace statement to your code

  1. 打开应用程序的源文件。Open a source file for your application. 例如,用于辅助角色或 Web 角色的 .cs 文件。For example, the .cs file for the worker role or web role.
  2. 添加以下 using 语句(如果尚未添加):Add the following using statement if it has not already been added:
        using System.Diagnostics;
    
  3. 添加 Trace 语句,以便捕获有关应用程序状态的信息。Add Trace statements where you want to capture information about the state of your application. 可以使用多种方法格式化 Trace 语句的输出。You can use a variety of methods to format the output of the Trace statement. 有关详细信息,请参阅 如何:向应用程序代码添加 Trace 语句For more information, see How to: Add Trace Statements to Application Code.
  4. 保存源文件。Save the source file.