使用 Visual Studio 调试 Service Fabric 应用程序Debug your Service Fabric application by using Visual Studio

调试本地 Service Fabric 应用程序Debug a local Service Fabric application

可以通过在本地计算机开发群集中部署和调试 Azure Service Fabric 应用程序来节省时间和资金。You can save time and money by deploying and debugging your Azure Service Fabric application in a local computer development cluster. Visual Studio 2019 或 2015 可以将应用程序部署到本地群集,并将调试器自动连接到应用程序的所有实例。Visual Studio 2019 or 2015 can deploy the application to the local cluster and automatically connect the debugger to all instances of your application. 必须以管理员身份运行 Visual Studio 才能连接调试器。Visual Studio must be run as Administrator to connect the debugger.

  1. 按照设置 Service Fabric 开发环境中的步骤启动本地开发群集。Start a local development cluster by following the steps in Setting up your Service Fabric development environment.

  2. F5 或单击“调试” > 开始调试Press F5 or click Debug > Start Debugging.

    开始调试应用程序

  3. 通过单击“调试” 菜单中的命令来设置代码中的断点并单步执行应用程序。Set breakpoints in your code and step through the application by clicking commands in the Debug menu.

    Note

    Visual Studio 将附加到应用程序的所有实例。Visual Studio attaches to all instances of your application. 单步执行代码时,断点可能被多个进程命中,从而产生并发会话。While you're stepping through code, breakpoints may get hit by multiple processes resulting in concurrent sessions. 尝试通过在线程 ID 上设置每个断点条件,或使用诊断事件,在命中后禁用断点。Try disabling the breakpoints after they're hit, by making each breakpoint conditional on the thread ID or by using diagnostic events.

  4. “诊断事件” 窗口会自动打开,以便用户可实时查看诊断事件。The Diagnostic Events window will automatically open so you can view diagnostic events in real time.

    查看实时诊断事件

  5. 也可以在 Cloud Explorer 中打开“诊断事件”窗口。You can also open the Diagnostic Events window in Cloud Explorer. 在“Service Fabric”下,右键单击任何节点,并选择“查看流式跟踪”Under Service Fabric, right-click any node and choose View Streaming Traces.

    打开“诊断事件”窗口

    如果要筛选对特定服务或应用程序的跟踪,请在该特定服务或应用程序上启用流式跟踪。If you want to filter your traces to a specific service or application, enable streaming traces on that specific service or application.

  6. 诊断事件可以在自动生成的 ServiceEventSource.cs 文件中查看并从应用程序代码中进行调用。The diagnostic events can be seen in the automatically generated ServiceEventSource.cs file and are called from application code.

    ServiceEventSource.Current.ServiceMessage(this, "My ServiceMessage with a parameter {0}", result.Value.ToString());
    
  7. “诊断事件” 窗口支持实时筛选、暂停和检查事件。The Diagnostic Events window supports filtering, pausing, and inspecting events in real time. 筛选是对事件消息及其内容进行的简单字符串搜索。The filter is a simple string search of the event message, including its contents.

    实时筛选、暂停和恢复或检查事件

  8. 调试服务与调试其他任何应用程序类似。Debugging services is like debugging any other application. 通常,将通过 Visual Studio 设置断点,以便轻松调试。You'll normally set Breakpoints through Visual Studio for easy debugging. 即使可靠集合在多个节点间进行复制,它们仍会实现 IEnumerable。Even though Reliable Collections replicate across multiple nodes, they still implement IEnumerable. 此实现意味着,可以在调试时使用 Visual Studio 中的结果视图来查看其中存储的内容。This implementation means that you can use the Results View in Visual Studio while you debug to see what you've stored inside. 为此,可以在代码中的任意位置设置断点。To do so, set a breakpoint anywhere in your code.

    开始调试应用程序

在调试过程中运行脚本Running a script as part of debugging

某些情况下,可能需要在启动调试会话的过程中运行脚本(例如,在不使用默认服务时)。In certain scenarios you might need to run a script as part of starting a debugging session (e.g. when not using Default Services).

在 Visual Studio 中,可以将名为 Start-Service.ps1 的文件添加到 Service Fabric 应用程序项目 (.sfproj) 的 Scripts 文件夹中。In Visual Studio, you can add a file called Start-Service.ps1 in the Scripts folder of the Service Fabric Application project (.sfproj). 在本地群集中创建完应用程序以后,就会调用该脚本。This script will be invoked after the application has been created in the local cluster.

调试远程 Service Fabric 应用程序Debug a remote Service Fabric application

如果 Service Fabric 应用程序正在 Azure 中的 Service Fabric 群集上运行,你可以直接从 Visual Studio 远程调试这些应用程序。If your Service Fabric applications are running on a Service Fabric cluster in Azure, you can remotely debug these applications, directly from Visual Studio.

Warning

远程调试适用于开发/测试方案,而非用于生产环境中,因为它会对运行中的应用程序造成影响。Remote debugging is meant for dev/test scenarios and not to be used in production environments, because of the impact on the running applications.

  1. 在“云资源管理器”中导航到群集 。Navigate to your cluster in Cloud Explorer. 右键单击并选择“启用调试” Right-click and choose Enable Debugging

    启用远程调试

    此操作将启动在群集节点和所需的网络配置上启用远程调试扩展的过程。This action will kick off the process of enabling the remote debugging extension on your cluster nodes and required network configurations.

  2. 在“Cloud Explorer”中右键单击群集节点,并选择“附加调试器”。Right-click the cluster node in Cloud Explorer, and choose Attach Debugger

    附加调试程序

  3. 在“附加到进程”对话框中,选择想要调试的进程,并单击“附加”。In the Attach to process dialog, choose the process you want to debug, and click Attach

    选择进程

    想要附加到的进程名称等于服务项目程序集名称。The name of the process you want to attach to, equals the name of your service project assembly name.

    调试器将附加到运行该进程的所有节点。The debugger will attach to all nodes running the process.

    • 如果调试的是无状态服务,所有节点上的该服务的所有实例都是调试会话的一部分。In the case where you're debugging a stateless service, all instances of the service on all nodes are part of the debug session.

    • 如果调试的是有状态服务,则只有任何分区的主副本才将处于活动状态并因此会被调试器捕获。If you're debugging a stateful service, only the primary replica of any partition will be active and therefore caught by the debugger. 如果在调试会话期间移动主副本,仍在调试会话中处理该副本。If the primary replica moves during the debug session, the processing of that replica will still be part of the debug session.

    • 若要仅捕获给定服务的相关分区或实例,可以使用条件断点,以仅中断特定的分区或实例。To only catch relevant partitions or instances of a given service, you can use conditional breakpoints to only break a specific partition or instance.

      条件断点

      Note

      目前我们不支持使用具有相同服务可执行文件名的多个实例调试 Service Fabric 群集。Currently we do not support debugging a Service Fabric cluster with multiple instances of the same service executable name.

  4. 应用程序调试完成后,即可在“Cloud Explorer”中右键单击群集并选择“禁用调试”来禁用远程调试扩展Once you finish debugging your application, you can disable the remote debugging extension by right-clicking the cluster in Cloud Explorer and choose Disable Debugging

    禁用远程调试

从远程群集节点流式传输跟踪Streaming traces from a remote cluster node

也可直接从远程群集节点将跟踪流式传输到 Visual studio。You're also able to stream traces directly from a remote cluster node to Visual Studio. 借助此功能,可以流式传输在 Service Fabric 群集节点上生成的 ETW 跟踪事件。This feature allows you to stream ETW trace events, produced on a Service Fabric cluster node.

Note

此功能需要 Service Fabric SDK 2.0Azure SDK for .NET 2.9This feature requires Service Fabric SDK 2.0 and Azure SDK for .NET 2.9. 此功能仅支持在 Azure 中运行的群集。This feature only supports clusters running in Azure.

Warning

流跟踪适用于开发/测试方案,而非用于生产环境中,因为它会对运行中的应用程序造成影响。Streaming traces is meant for dev/test scenarios and not to be used in production environments, because of the impact on the running applications. 在生产方案中,应依赖于使用 Azure 诊断转发事件。In a production scenario, you should rely on forwarding events using Azure Diagnostics.

  1. 在“云资源管理器”中导航到群集 。Navigate to your cluster in Cloud Explorer. 右键单击并选择“启用流式跟踪” Right-click and choose Enable Streaming Traces

    启用远程流跟踪

    此操作将启动在群集节点以及所需的网络配置上启用流式跟踪扩展的过程。This action will kick off the process of enabling the streaming traces extension on your cluster nodes, as well as required network configurations.

  2. 在“Cloud Explorer”中展开“节点”元素,右键单击想要进行流式跟踪的节点,并选择“查看流式跟踪Expand the Nodes element in Cloud Explorer, right-click the node you want to stream traces from and choose View Streaming Traces

    查看远程流跟踪

    针对想要从其中查看跟踪的任意数目节点重复步骤 2。Repeat step 2 for as many nodes as you want to see traces from. 每个节点流显示在专用窗口中。Each nodes stream will show up in a dedicated window.

    现在即可查看 Service Fabric 以及服务发出的跟踪。You are now able to see the traces emitted by Service Fabric, and your services. 如果想要筛选事件以便只显示特定的应用程序,只需在筛选器中键入应用程序的名称即可。If you want to filter the events to only show a specific application, simply type in the name of the application in the filter.

    查看流跟踪

  3. 在群集中完成流式跟踪后,即可在“Cloud Explorer”中右键单击群集,并选择“禁用流式跟踪”来禁用远程流式跟踪。Once you are done streaming traces from your cluster, you can disable remote streaming traces, by right-clicking the cluster in Cloud Explorer and choose Disable Streaming Traces

    禁用远程流跟踪

后续步骤Next steps