使用 Snapshot Debugger 调试实时 ASP.NET Azure 应用Debug live ASP.NET Azure apps using the Snapshot Debugger

Snapshot Debugger 会在你感兴趣的代码执行时为生产中的应用拍摄快照。The Snapshot Debugger takes a snapshot of your in-production apps when code that you're interested in executes. 若要指示该调试器拍摄快照,可以在代码中设置快照点和记录点。To instruct the debugger to take a snapshot, you set snappoints and logpoints in your code. 通过该调试器,可精确查看出错的内容,而不会影响生产应用程序的流量。The debugger lets you see exactly what went wrong, without impacting traffic of your production application. Snapshot Debugger 有助于大幅减少解决生产环境中出现的问题所需的时间。The Snapshot Debugger can help you dramatically reduce the time it takes to resolve issues that occur in production environments.

快照点和记录点类似于断点,但与断点不同的是,快照点不会在命中时停止应用程序。Snappoints and logpoints are similar to breakpoints, but unlike breakpoints, snappoints don't halt the application when hit. 通常,在快照点捕获快照需要 10-20 毫秒。Typically, capturing a snapshot at a snappoint takes 10-20 milliseconds.

在本教程中,你将:In this tutorial, you will:

  • 启动 Snapshot DebuggerStart the Snapshot Debugger
  • 设置快照点,以及查看快照Set a snappoint and view a snapshot
  • 设置记录点Set a logpoint

先决条件Prerequisites

  • Snapshot Debugger 仅适用于具有 Azure 开发工作负载的 Visual Studio 2017 Enterprise 版本 15.5 或更高版本。Snapshot Debugger is only available starting in Visual Studio 2017 Enterprise version 15.5 or higher with the Azure development workload. (可在“各个组件”选项卡的“调试和测试” > “Snapshot Debugger”下找到它 。)(Under the Individual components tab, you find it under Debugging and testing > Snapshot debugger.)

    如果尚未安装,请安装 Visual Studio 2019If it's not already installed, install Visual Studio 2019. 如果从旧版 Visual Studio 安装更新,请运行 Visual Studio 安装程序,并选中 ASP.NET 和 Web 开发工作负载中的 Snapshot Debugger 组件。If you're updating from a previous Visual Studio installation, run the Visual Studio Installer and check the Snapshot Debugger component in the ASP.NET and web development workload.

    如果尚未安装,请安装 Visual Studio 2017 Enterprise 版本 15.5 或更高版本。If it's not already installed, install Visual Studio 2017 Enterprise version 15.5 or later. 如果从旧版 Visual Studio 2017 安装更新,请运行 Visual Studio 安装程序,并选中 ASP.NET 和 Web 开发工作负载中的 Snapshot Debugger 组件。If you're updating from a previous Visual Studio 2017 installation, run the Visual Studio Installer and check the Snapshot Debugger component in the ASP.NET and web development workload.

  • 基本或更高版本的 Azure 应用服务计划。Basic or higher Azure App Service plan.

  • 快照集合适用于在 Azure App Service 中运行的以下 Web 应用:Snapshot collection is available for the following web apps running in Azure App Service:

    • 在 .NET Framework 4.6.1 或更高版本上运行的 ASP.NET 应用程序。ASP.NET applications running on .NET Framework 4.6.1 or later.
    • 在 Windows 中的 .Net Core 2.0 或更高版本上运行的 ASP.NET Core 应用程序。ASP.NET Core applications running on .NET Core 2.0 or later on Windows.

打开项目并启动 Snapshot DebuggerOpen your project and start the Snapshot Debugger

  1. 打开要进行快照调试的项目。Open the project you would like to snapshot debug.

    重要

    对于快照调试,需要打开与已发布到 Azure 应用服务的“相同版本的源代码”。To snapshot debug, you need to open the same version of source code that is published to your Azure App Service.

  1. 在云资源管理器(“视图”>“云资源管理器”)中,右键单击项目部署到的 Azure 应用服务,然后选择“附加快照调试器” 。In the Cloud Explorer (View > Cloud Explorer), right-click the Azure App Service your project is deployed to and select Attach Snapshot Debugger.

    启动 Snapshot Debugger

  1. 选择“调试”>“附加 Snapshot Debugger...”。选择部署项目的 Azure 应用服务和一个 Azure 存储帐户,然后单击“附加”。Choose Debug > Attach Snapshot Debugger.... Select the Azure App Service your project is deployed to and an Azure storage account, and then click Attach. Snapshot Debugger 还支持 Azure Kubernetes 服务Azure 虚拟机 (VM) 和虚拟机规模集Snapshot Debugger also supports Azure Kubernetes Service and Azure Virtual Machines (VM) & Virtual Machine Scale Sets.

    从“调试”菜单启动 Snapshot Debugger

    选择 Azure 资源

重要

第一次选择“附加快照调试器”时,系统会提示在 Azure 应用服务上安装快照调试器站点扩展。The first time you select Attach Snapshot Debugger, you're prompted to install the Snapshot Debugger site extension on your Azure App Service. 此安装需要重启 Azure 应用服务。This installation requires a restart of your Azure App Service.

备注

Application Insights 站点扩展还支持快照调试。The Application Insights site extension also supports Snapshot Debugging. 如果遇到“站点扩展过期”错误消息,请参阅快照调试的疑难解答提示和已知问题以获取升级详细信息。If you come across a "site extension out of date" error message, see troubleshooting tips and known issues for snapshot debugging for upgrading details.

备注

(Visual Studio 2019 版本 16.2 及更高版本)Snapshot Debugger 已启用 Azure 云支持。(Visual Studio 2019 version 16.2 and above) Snapshot Debugger has enabled Azure cloud support. 请确保所选的 Azure 资源和 Azure 存储帐户都来自同一个云。Make sure that both the Azure resource and Azure Storage account you select are from the same cloud. 如果对企业 Azure 合规性配置有任何疑问,请联系 Azure 管理员。Please contact your Azure administrator if you have questions about your enterprise's Azure compliance configurations.

Visual Studio 现在处于快照调试模式下。Visual Studio is now in snapshot debugging mode. 快照调试模式Snapshot debugging mode

“模块”窗口显示何时 Azure 应用服务的所有模块都已加载(依次选择“调试”>“窗口”>“模块”可打开此窗口)。The Modules window shows you when all the modules have loaded for the Azure App Service (choose Debug > Windows > Modules to open this window).

选中“模块”窗口

设置快照点Set a snappoint

  1. 在代码编辑器中,单击你感兴趣的代码行旁边的左滚动条槽以设置快照点。In the code editor, click the left gutter next to a line of code you're interested in to set a snappoint. 请确保它是你已知将执行的代码。Make sure it's code that you know will execute.

    设置快照点

  2. 单击“开始收集”以打开快照点。Click Start Collection to turn on the snappoint.

    开启快照点

    提示

    查看快照时,不能执行步骤,但可以在代码中放置多个快照点以在不同代码行处跟进执行。You can't step when viewing a snapshot, but you can place multiple snappoints in your code to follow execution at different lines of code. 如果代码中有多个快照点,快照调试器可以确保对应的快照来自相同的最终用户会话。If you have multiple snappoints in your code, the Snapshot Debugger makes sure that the corresponding snapshots are from the same end-user session. 即使有多个用户点击应用,快照调试器仍会执行此操作。The Snapshot Debugger does this even if there are many users hitting your app.

获取快照Take a snapshot

设置快照点后,可以通过转到网站的浏览器视图并运行标记的代码行来手动生成快照,也可以等待用户根据其网站的使用情况生成快照。Once a snappoint is set, you can either manually generate a snapshot by going to the browser view of your web site and running the line of code marked or wait for your users to generate one from their usage of the site.

检查快照数据Inspect snapshot data

  1. 命中快照点时,快照将出现在诊断工具窗口。When the snappoint is hit, a snapshot appears in the Diagnostic Tools window. 若要打开此窗口,请依次选择“调试”>“窗口”>“显示诊断工具”。To open this window, choose Debug > Windows > Show Diagnostic Tools.

    打开快照点

  2. 双击快照点,以在代码编辑器中打开快照。Double-click the snappoint to open the snapshot in the code editor.

    检查快照数据

    在此视图中,可通过将鼠标悬停在变量上来查看数据提示;使用“局部变量”、“监视”和“调用堆栈”窗口;以及计算表达式 。From this view, you can hover over variables to view DataTips, use the Locals, Watches, and Call Stack windows, and also evaluate expressions.

    网站本身仍然是实时的,最终用户不会受到影响。The website itself is still live and end users aren't affected. 默认情况下,每个快照点只捕获一个快照:快照点在捕获快照后即关闭。Only one snapshot is captured per snappoint by default: after a snapshot is captured the snappoint turns off. 如果要在此快照点再捕获一个快照,可以通过单击“更新集合”来重新打开快照点。If you want to capture another snapshot at the snappoint, you can turn the snappoint back on by clicking Update Collection.

还可以向应用添加更多快照点,并使用“更新集合”按钮将其启动。You can also add more snappoints to your app and turn them on with the Update Collection button.

需要帮助?Need help? 请参阅疑难解答和已知问题快照调试常见问题解答页。See the Troubleshooting and known issues and FAQ for snapshot debugging pages.

设置条件性快照点Set a conditional snappoint

如果很难在应用中重新创建特定状态,请考虑使用条件性快照点。If it's difficult to recreate a particular state in your app, consider using a conditional snappoint. 条件性快照点帮助你控制何时拍摄快照(例如,当变量包含要检查的特定值时)。Conditional snappoints help you control when to take a snapshot such as when a variable contains a particular value that you want to inspect. 可以使用表达式、筛选器或命中次数设置表达式。You can set conditions using expressions, filters, or hit counts.

创建条件性快照点To create a conditional snappoint

  1. 右键单击快照点图标(空心球)并选择“设置”。Right-click a snappoint icon (the hollow ball) and choose Settings.

    选择“设置”

  2. 在“快照点设置”窗口中,键入一个表达式。In the snappoint settings window, type an expression.

    键入表达式

    在上图中,仅在 visitor.FirstName == "Dan" 时为快照点拍摄快照。In the preceding illustration, the snapshot is only taken for the snappoint when visitor.FirstName == "Dan".

设置记录点Set a logpoint

除了在点击快照点时拍摄快照,还可以配置快照点以记录消息(即创建记录点)。In addition to taking a snapshot when a snappoint is hit, you can also configure a snappoint to log a message (that is, create a logpoint). 无需重新部署应用即可设置记录点。You can set logpoints without having to redeploy your app. 记录点通过虚拟方式执行,不会对正在运行的应用程序产生任何影响或副作用。Logpoints are executed virtually and cause no impact or side effects to your running application.

创建记录点To create a logpoint

  1. 右键单击快照点图标(蓝色六边形),然后选择“设置”。Right-click a snappoint icon (the blue hexagon) and choose Settings.

  2. 在“快照点设置”窗口中,选择“操作”。In the snappoint settings window, select Actions.

    创建记录点

  3. 在“消息”字段中,可以输入想要记录的新日志消息。In the Message field, you can enter the new log message you want to log. 还可以将日志消息中的变量放在大括号中,从而计算它们的值。You can also evaluate variables in your log message by placing them inside curly braces.

    如果选择“发送到输出窗口”,则在点击记录点时,消息将出现在“诊断工具”窗口中。If you choose Send to Output Window, when the logpoint is hit, the message appears in the Diagnostic Tools window.

    “诊断工具”窗口中的记录点数据

    如果选择“发送到应用程序日志”,则在点击记录点时,消息会出现在可以看到来自 System.Diagnostics.Trace(或 .NET Core 中的 ILogger)的消息的任何位置,例如 App InsightsIf you choose Send to application log, when the logpoint is hit, the message appears anywhere that you can see messages from System.Diagnostics.Trace (or ILogger in .NET Core), such as App Insights.

后续步骤Next steps

在本教程中,你已了解如何使用适用于应用服务的 Snapshot Debugger。In this tutorial, you've learned how to use the Snapshot Debugger for App Services. 你可能想要阅读有关此功能的更多详细信息。You may want to read more details about this feature.