在本地计算机开发安装过程中监视和诊断服务Monitor and diagnose services in a local machine development setup

监视、检测、诊断和故障排除允许服务继续运行,同时对用户体验造成最小中断。Monitoring, detecting, diagnosing, and troubleshooting allow for services to continue with minimal disruption to the user experience. 虽然监视和诊断在实际部署的生产环境中相当重要,但是效率将取决于在服务开发过程中采用类似的模型,以便在移到实际安装时能确保其正常工作。While monitoring and diagnostics are critical in an actual deployed production environment, the efficiency will depend on adopting a similar model during development of services to ensure they work when you move to a real-world setup. Service Fabric 使服务开发人员能够轻松实现可以跨单个计算机本地开发安装和实际生产群集安装无缝工作的诊断。Service Fabric makes it easy for service developers to implement diagnostics that can seamlessly work across both single-machine local development setups and real-world production cluster setups.

Windows 事件跟踪Event Tracing for Windows

Windows 事件跟踪 (ETW) 是推荐的用于跟踪 Service Fabric 中的消息的技术。Event Tracing for Windows (ETW) is the recommended technology for tracing messages in Service Fabric. 使用 ETW 的一些好处包括:Some benefits of using ETW are:

  • ETW 速度很快。ETW is fast. 它是作为一种对代码执行时间影响最小的跟踪技术而构建的。It was built as a tracing technology that has minimal impact on code execution times.
  • ETW 跟踪可以跨本地开发环境和实际群集安装无缝工作。ETW tracing works seamlessly across local development environments and also real-world cluster setups. 这意味着准备好将代码部署到实际群集时,无需重新编写跟踪代码。This means you don't have to rewrite your tracing code when you are ready to deploy your code to a real cluster.
  • Service Fabric 系统代码也使用 ETW 进行内部跟踪。Service Fabric system code also uses ETW for internal tracing. 这可让你查看与 Service Fabric 系统跟踪交错的应用程序跟踪。This allows you to view your application traces interleaved with Service Fabric system traces. 同时帮助你更轻松地了解基础系统中应用程序代码与事件之间的序列和相互关系。It also helps you to more easily understand the sequences and interrelationships between your application code and events in the underlying system.
  • Service Fabric Visual Studio 工具中有内置支持,可以查看 ETW 事件。There is built-in support in Service Fabric Visual Studio tools to view ETW events. 使用 Service Fabric 正确配置 Visual Studio 后,ETW 事件将出现在 Visual Studio 的“诊断事件”视图中。ETW events appear in the Diagnostic Events view of Visual Studio once Visual Studio is correctly configured with Service Fabric.

在 Visual Studio 中查看 Service Fabric 系统事件View Service Fabric system events in Visual Studio

Service Fabric 发出 ETW 事件以帮助应用程序开发人员了解平台中发生的情况。Service Fabric emits ETW events to help application developers understand what's happening in the platform. 如果还没有这么做,请继续遵循在 Visual Studio 中创建第一个应用程序中的步骤。If you haven't already done so, go ahead and follow the steps in Creating your first application in Visual Studio. 此信息将帮助你启动应用程序,并运行可显示跟踪消息的诊断事件查看器。This information will help you get an application up and running with the Diagnostics Events Viewer showing the trace messages.

  1. 如果诊断事件窗口未自动显示,请在 Visual Studio 中转到“视图”选项卡上,选择“其他窗口”,并选择“诊断事件查看器”。If the diagnostics events window does not automatically show, Go to the View tab in Visual Studio, choose Other Windows and then Diagnostic Events Viewer.

  2. 每个事件都有标准元数据信息,它将告诉你事件来自的节点、应用程序和服务。Each event has standard metadata information that tells you the node, application and service the event is coming from. 也可以使用事件窗口顶端的“筛选事件”框来筛选事件列表。You can also filter the list of events by using the Filter events box at the top of the events window. 例如,可以根据“节点名称”或“服务名称”进行筛选。For example, you can filter on Node Name or Service Name. 而查看事件详细信息时,也可以使用事件窗口顶端的“暂停”按钮来暂停,稍后再继续,而不会丢失任何事件。And when you're looking at event details, you can also pause by using the Pause button at the top of the events window and resume later without any loss of events.

    Visual Studio 诊断事件查看器

将自己的自定义跟踪添加到应用程序代码Add your own custom traces to the application code

Service Fabric Visual Studio 项目模板包含示例代码。The Service Fabric Visual Studio project templates contain sample code. 该代码演示如何添加自定义应用程序代码 ETW 跟踪(与来自 Service Fabric 的系统跟踪一起显示在 Visual Studio ETW 查看器中)。The code shows how to add custom application code ETW traces that show up in the Visual Studio ETW viewer alongside system traces from Service Fabric. 此方法的优点是元数据会自动添加到跟踪中,并已配置了 Visual Studio 诊断事件查看器来显示它们。The advantage of this method is that metadata is automatically added to traces, and the Visual Studio Diagnostic Events Viewer is already configured to display them.

对于从服务模板(无状态或有状态)创建的项目,只要搜索 RunAsync 实现即可:For projects created from the service templates (stateless or stateful) just search for the RunAsync implementation:

  1. RunAsync 方法中 ServiceEventSource.Current.ServiceMessage 的调用显示了应用程序代码中的自定义 ETW 跟踪的一个示例。The call to ServiceEventSource.Current.ServiceMessage in the RunAsync method shows an example of a custom ETW trace from the application code.
  2. 在 ServiceEventSource.cs 文件中,可找到 ServiceEventSource.ServiceMessage 方法的重载,由于性能方面的原因,应该将其用于高频率事件。In the ServiceEventSource.cs file, you will find an overload for the ServiceEventSource.ServiceMessage method that should be used for high-frequency events due to performance reasons.

对于从 Actor 模板(无状态或有状态)创建的项目:For projects created from the actor templates (stateless or stateful):

  1. 打开 "ProjectName".cs 文件,其中,ProjectName 是为 Visual Studio 项目选择的名称。Open the "ProjectName".cs file where ProjectName is the name you chose for your Visual Studio project.
  2. DoWorkAsync 方法中查找代码 ActorEventSource.Current.ActorMessage(this, "Doing Work");Find the code ActorEventSource.Current.ActorMessage(this, "Doing Work"); in the DoWorkAsync method. 这是根据应用程序代码编写的自定义 ETW 跟踪的一个示例。This is an example of a custom ETW trace written from application code.
  3. 在文件 ActorEventSource.cs 中,可找到 ActorEventSource.ActorMessage 方法的重载,由于性能方面的原因,应该将其用于高频率事件。In file ActorEventSource.cs, you will find an overload for the ActorEventSource.ActorMessage method that should be used for high-frequency events due to performance reasons.

将自定义 ETW 跟踪添加到服务代码之后,可以再次生成、部署和运行应用程序以在诊断事件查看器中查看事件。After adding custom ETW tracing to your service code, you can build, deploy, and run the application again to see your event(s) in the Diagnostic Events Viewer. 如果使用 F5 调试应用程序,则会自动打开诊断事件查看器。If you debug the application with F5, the Diagnostic Events Viewer will open automatically.

后续步骤Next steps

添加到上述应用程序用于进行本地诊断的相同跟踪代码将与工具一起工作,在 Azure 群集上运行应用程序时,可以使用这些工具查看这些事件。The same tracing code that you added to your application above for local diagnostics will work with tools that you can use to view these events when running your application on an Azure cluster. 请查看以下文章,其中介绍了不同的工具选项,以及如何设置这些选项。Check out these articles that discuss the different options for the tools and describe how you can set them up.