ETW 跟踪ETW Tracing

本示例演示如何通过使用 Windows 事件跟踪 (ETW) 和本示例提供的 ETWTraceListener 来实现端对端 (E2E) 跟踪。This sample demonstrates how to implement End-to-End (E2E) tracing using Event Tracing for Windows (ETW) and the ETWTraceListener that is provided with this sample. 该示例基于 入门 ,并包括 ETW 跟踪。The sample is based on the Getting Started and includes ETW tracing.

备注

本主题的最后介绍了此示例的设置过程和生成说明。The set-up procedure and build instructions for this sample are located at the end of this topic.

本示例假定您熟悉 跟踪和消息日志记录This sample assumes that you are familiar with Tracing and Message Logging.

System.Diagnostics 跟踪模型中的每个跟踪源都可以具有多个跟踪侦听器,这些侦听器确定跟踪数据的位置和方式。Each trace source in the System.Diagnostics tracing model can have multiple trace listeners that determine where and how the data is traced. 侦听器的类型定义记录跟踪数据的格式。The type of listener defines the format in which trace data is logged. 下面的代码示例演示如何向配置中添加侦听器。The following code sample shows how to add the listener to configuration.

<system.diagnostics>  
    <sources>  
        <source name="System.ServiceModel"
             switchValue="Verbose,ActivityTracing"  
             propagateActivity="true">  
            <listeners>  
                <add type=  
                   "System.Diagnostics.DefaultTraceListener"  
                   name="Default">  
                   <filter type="" />  
                </add>  
                <add name="ETW">  
                    <filter type="" />  
                </add>  
            </listeners>  
        </source>  
    </sources>  
    <sharedListeners>  
        <add type=  
            "Microsoft.ServiceModel.Samples.EtwTraceListener, ETWTraceListener"  
            name="ETW" traceOutputOptions="Timestamp">  
            <filter type="" />  
       </add>  
    </sharedListeners>  
</system.diagnostics>  

在使用此侦听器之前,必须启动 ETW 跟踪会话。Before using this listener, an ETW Trace Session must be started. 可以通过使用 Logman.exe 或 Tracelog.exe 来启动此会话。This session can be started by using Logman.exe or Tracelog.exe. 本示例随附一个 SetupETW.bat 文件,您可以和 CleanupETW.bat 文件一起使用此文件来设置 ETW 跟踪会话,以便关闭会话并完成日志文件。A SetupETW.bat file is included with this sample so that you can set up the ETW Trace Session along with a CleanupETW.bat file for closing the session and completing the log file.

备注

本主题的最后介绍了此示例的设置过程和生成说明。The setup procedure and build instructions for this sample are located at the end of this topic. 有关这些工具的详细信息,请参阅 https://go.microsoft.com/fwlink/?LinkId=56580For more information about these tools, see https://go.microsoft.com/fwlink/?LinkId=56580

使用 ETWTraceListener 时,将在二进制 .etl 文件中记录跟踪。When using the ETWTraceListener, traces are logged in binary .etl files. 在打开 ServiceModel 跟踪的情况下,所有生成的跟踪都显示在同一个文件中。With ServiceModel tracing turned on, all generated traces appear in the same file. 使用 服务跟踪查看器工具 ( # A0) 查看 .etl 和 .svclog 日志文件。Use Service Trace Viewer Tool (SvcTraceViewer.exe) for viewing .etl and .svclog log files. 该查看器可创建系统的端对端视图,可以从消息源到消息目标和使用点来跟踪消息。The viewer creates an end-to-end view of the system that makes it possible to trace a message from its source to its destination and point of consumption.

ETW 跟踪侦听器支持循环记录。The ETW Trace Listener supports circular logging. 若要启用此功能,请参阅 开始运行 和键入 cmd 以启动命令控制台。To enable this feature, go to Start, Run and type cmd to start a command console. 在下面的命令中,用日志文件的名称替换 <logfilename> 参数。In the following command, replace the <logfilename> parameter with the name of your log file.

logman create trace Wcf -o <logfilename> -p "{411a0819-c24b-428c-83e2-26b41091702e}" -f bincirc -max 1000  

-f-max 开关是可选的。The -f and -max switches are optional. 它们分别指定二进制循环格式和 1000 MB 的最大日志大小。They specify the binary circular format and max log size of 1000MB respectively. -p 开关用于指定跟踪提供程序。The -p switch is used to specify the trace provider. 在我们的示例中,"{411a0819-c24b-428c-83e2-26b41091702e}" 是“XML ETW 示例提供程序”的 GUID。In our example, "{411a0819-c24b-428c-83e2-26b41091702e}" is the GUID for "XML ETW Sample Provider".

若要启动会话,请键入以下命令。To start the session, type in the following command.

logman start Wcf  

在完成日志记录后,可以用下面的命令停止会话。After you have finished logging, you can stop the session with the following command.

logman stop Wcf  

此过程将生成二进制循环日志,您可以用所选的工具处理此日志,包括 服务跟踪查看器工具 ( # A0) 或 Tracerpt。This process generates binary circular logs that you can process with your tool of choice, including Service Trace Viewer Tool (SvcTraceViewer.exe) or Tracerpt.

若要详细了解如何执行循环日志记录,还可以查看 循环跟踪 示例。You can also review the Circular Tracing sample for more information on an alternative listener to perform circular logging.

设置、生成和运行示例To set up, build, and run the sample

  1. 请确保已 为 Windows Communication Foundation 示例执行了一次性安装过程Be sure you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.

  2. 若要生成解决方案,请按照 生成 Windows Communication Foundation 示例中的说明进行操作。To build the solution, follow the instructions in Building the Windows Communication Foundation Samples.

    备注

    若要使用 RegisterProvider.bat、SetupETW.bat 和 CleanupETW.bat 命令,必须在本地管理员帐户下运行。To use the RegisterProvider.bat, SetupETW.bat and CleanupETW.bat commands, you must run under a local administrator account. 如果你使用的是 Windows Vista 或更高版本,则还必须使用提升的权限运行命令提示符。If you are using Windows Vista or later, you must also run the command prompt with elevated privileges. 为此,请右键单击 "命令提示符" 图标,然后单击 "以 管理员身份运行"。To do so, right-click the command prompt icon, then click Run as administrator.

  3. 运行示例之前,在客户端和服务器上运行 RegisterProvider.bat。Before running the sample, run RegisterProvider.bat on the client and server. 这会设置生成的 ETWTracingSampleLog.etl 文件以生成可由服务跟踪查看器读取的跟踪。This sets up the resulting ETWTracingSampleLog.etl file to generate traces that can be read by the Service Trace Viewer. 此文件可以在 C:\logs 文件夹中找到。This file can be found in the C:\logs folder. 如果此文件夹不存在,则必须创建此文件夹;否则将不会生成跟踪。If this folder does not exist, it must be created or no traces are generated. 然后,在客户端和服务器计算机上运行 SetupETW.bat 以开始 ETW 跟踪会话。Then, run SetupETW.bat on the client and server computers to begin the ETW Trace Session. SetupETW.bat 文件可以在 CS\Client 文件夹下找到。The SetupETW.bat file can be found under the CS\Client folder.

  4. 若要以单机配置或跨计算机配置来运行示例,请按照 运行 Windows Communication Foundation 示例中的说明进行操作。To run the sample in a single- or cross-computer configuration, follow the instructions in Running the Windows Communication Foundation Samples.

  5. 示例完成后,运行 CleanupETW.bat 以完成 ETWTracingSampleLog.etl 文件的创建。When the sample is completed, run CleanupETW.bat to complete the creation of the ETWTracingSampleLog.etl file.

  6. 在服务跟踪查看器中打开 ETWTracingSampleLog.etl 文件。Open the ETWTracingSampleLog.etl file from within the Service Trace Viewer. 系统会提示您将二进制格式的文件另存为 .svclog 文件。You will be prompted to save the binary formatted file as a .svclog file.

  7. 从服务跟踪查看器中打开新创建的 .svclog 文件以查看 ETW 和 ServiceModel 跟踪。Open the newly created .svclog file from within the Service Trace Viewer to view the ETW and ServiceModel traces.

重要

您的计算机上可能已安装这些示例。The samples may already be installed on your computer. 在继续操作之前,请先检查以下(默认)目录:Check for the following (default) directory before continuing.

<InstallDrive>:\WF_WCF_Samples

如果此目录不存在,请参阅Windows Communication Foundation (wcf) ,并 Windows Workflow Foundation (的 WF) .NET Framework Windows Communication Foundation (WFWFIf this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WFWF samples. 此示例位于以下目录:This sample is located in the following directory.

<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Management\AnalyticTrace

请参阅See also