使用 Azure Application Insights 查找并诊断运行时异常Find and diagnose run-time exceptions with Azure Application Insights

Azure Application Insights 从应用程序收集遥测数据,帮助确定和诊断运行时异常。Azure Application Insights collects telemetry from your application to help identify and diagnose run-time exceptions. 本教程引导完成对应用程序执行此流程的步骤。This tutorial takes you through this process with your application. 你将学习如何执行以下操作:You learn how to:

  • 修改项目来实现异常跟踪Modify your project to enable exception tracking
  • 确定应用程序不同组件出现的异常Identify exceptions for different components of your application
  • 查看异常的详细信息View details of an exception
  • 使用查询语言分析失败请求的详细信息Analyze details of failed requests using query language
  • 新建工作项来更正故障代码Create a new work item to correct the faulty code

先决条件Prerequisites

完成本教程:To complete this tutorial:

  • 使用以下工作负荷安装 Visual Studio 2017Install Visual Studio 2017 with the following workloads:
    • ASP.NET 和 Web 开发ASP.NET and web development
    • Azure 开发Azure development
  • 将 .NET 应用程序部署到 Azure 并启用 Application Insights SDKDeploy a .NET application to Azure and enable the Application Insights SDK.
  • 本教程跟踪应用程序中异常的标识,以便在开发或测试环境中修改代码来生成异常。The tutorial tracks the identification of an exception in your application, so modify your code in your development or test environment to generate an exception.

登录 AzureLog in to Azure

通过 https://portal.azure.cn 登录到 Azure 门户。Log in to the Azure portal at https://portal.azure.cn.

分析故障Analyze failures

Application Insights 收集应用程序中出现的所有故障,让用户能够查看其在不同操作中出现的频率,帮助将精力集中在具有最大影响的故障上。Application Insights collects any failures in your application and lets you view their frequency across different operations to help you focus your efforts on those with the highest impact. 用户可随后深入探索这些故障的详细信息,从而确定根本原因。You can then drill down on details of these failures to identify root cause.

  1. 选择“Application Insights”,然后选择订阅 。Select Application Insights and then your subscription.

  2. 若要打开“故障” 面板,请选择“调查” 菜单下的“故障” ,或单击“失败的请求” 图表。To open the Failures panel either select Failures under the Investigate menu or click the Failed requests graph.

    失败的请求

  3. “失败的请求” 面板将为应用程序的每个操作显示失败请求计数和受影响用户的数量。The Failed requests panel shows the count of failed requests and the number of users affected for each operation for the application. 通过按用户对此类信息排序,可确定对用户具有最大影响的故障。By sorting this information by user you can identify those failures that most impact users. 在此示例中,GET Employees/CreateGET Customers/Details 是有待调查的可能候选对象,因为其拥有大量失败的请求和受影响的用户。In this example, the GET Employees/Create and GET Customers/Details are likely candidates to investigate because of their large number of failures and impacted users. 选择操作后,可在右侧面板中显示有关此操作的详细信息。Selecting an operation shows further information about this operation in the right panel.

    “失败的请求”面板

  4. 缩短时间范围,详细查看故障率出现峰值的时段。Reduce the time window to zoom in on the period where the failure rate shows a spike.

    “失败的请求”窗口

  5. 单击包含所筛选结果数目的按钮即可查看相关的示例。See the related samples by clicking on the button with the number of filtered results. “建议的”示例有来自所有组件的相关遥测数据,即使采用实际上是在其中随机完成的。The "suggested" samples have related telemetry from all components, even if sampling may have been in effect in any of them. 单击搜索结果即可查看有关失败的详细信息。Click on a search result to see the details of the failure.

    失败请求示例

  6. 失败请求的详细信息显示了甘特图,该图显示此事务中有两个依赖项失败,这两个失败所用时间占事务总持续时间的 50% 以上。The details of the failed request shows the Gantt chart which shows that there were two dependency failures in this transaction, which also attributed to over 50% of the total duration of the transaction. 此体验提供的所有遥测跨分布式应用程序的多个组件,这些组件与此操作 ID 相关。This experience presents all telemetry, across components of a distributed application that are related to this operation ID.

    失败请求的详细信息

  7. 操作详细信息还显示可能是故障原因的 FormatException。The operations detail also shows a FormatException which appears to have caused the failure. 此时,可发现这是无效邮政编码导致的。You can see that it's due to an invalid zip code. 可以打开调试快照,然后即可在 Visual Studio 中查看代码级别的调试信息。You can open the debug snapshot to see code level debug information in Visual Studio.

    异常详细信息

确定故障代码Identify failing code

Snapshot Debugger 收集应用程序中最频繁出现的异常的快照,帮助在生产中诊断其根本原因。The Snapshot Debugger collects snapshots of the most frequent exceptions in your application to assist you in diagnosing its root cause in production. 可在门户中查看调试快照,查看调用堆栈并检查每个调用堆栈帧中的变量。You can view debug snapshots in the portal to see the call stack and inspect variables at each call stack frame. 之后,你可以选择通过下载快照并在 Visual Studio 2017 Enterprise 中打开它来调试源代码。Afterwards, you have the option to debug the source code by downloading the snapshot and opening it in Visual Studio 2017 Enterprise.

  1. 在异常的属性中,单击“打开调试快照” 。In the properties of the exception, click Open debug snapshot.

  2. 此时会打开“调试快照” 面板,并显示请求的调用堆栈。The Debug Snapshot panel opens with the call stack for the request. 单击任意方法来查看提交请求时所有局部变量的值。Click any method to view the values of all local variables at the time of the request. 从此示例的第一个方法开始,可发现局部变量没有值。Starting from the top method in this example, we can see local variables that have no value.

    调试快照

  3. 具有有效值的第一个调用是 ValidZipCode,可发现邮政编码通过无法转换为整数的字母提供。The first call that has valid values is ValidZipCode, and we can see that a zip code was provided with letters that isn't able to be translated into an integer. 这是代码中的错误,需要更正。This appears to be the error in the code that needs to be corrected.

    调试快照

  4. 然后,可以选择将此快照下载到 Visual Studio 中,我们可在其中找到需要更正的实际代码。You then have the option to download this snapshot into Visual Studio where we can locate the actual code that needs to be corrected. 为此,请单击“下载快照” 。To do so, click Download Snapshot.

  5. 快照会加载到 Visual Studio 中。The snapshot is loaded into Visual Studio.

  6. 现在可在 Visual Studio Enterprise 中运行调试会话,以便快速确定导致异常的代码行。You can now run a debug session in Visual Studio Enterprise that quickly identifies the line of code that caused the exception.

    代码中的异常

使用分析数据Use analytics data

Application Insights 收集的所有数据都存储在 Azure Log Analytics 中,Azure Log Analytics 提供一种丰富查询语言,方便使用各种方式分析数据。All data collected by Application Insights is stored in Azure Log Analytics, which provides a rich query language that allows you to analyze the data in a variety of ways. 我们可使用此数据来分析生成了我们正在研究的异常的请求。We can use this data to analyze the requests that generated the exception we're researching.

  1. 单击代码上方的 CodeLens 信息,查看 Application Insights 提供的遥测数据。Click the CodeLens information above the code to view telemetry provided by Application Insights.

    代码

  2. 单击“分析影响” ,打开 Application Insights Analytics。Click Analyze impact to open Application Insights Analytics. 它使用多个提供失败请求详细信息(比如受影响的用户、浏览器和区域)的查询填充。It's populated with several queries that provide details on failed requests such as impacted users, browsers, and regions.

    分析Analytics

添加工作项Add work item

如果将 Application Insights 连接到跟踪系统(比如 Azure DevOps 或 GitHub),可直接通过 Application Insights 创建工作项。If you connect Application Insights to a tracking system such as Azure DevOps or GitHub, you can create a work item directly from Application Insights.

  1. 返回 Application Insights 中的“异常属性” 面板。Return to the Exception Properties panel in Application Insights.

  2. 单击“新建工作项” 。Click New Work Item.

  3. “新建工作项” 面板将打开,其中已填充有关异常的详细信息。The New Work Item panel opens with details about the exception already populated. 可在保存前添加任何其他信息。You can add any additional information before saving it.

    新建工作项

后续步骤Next steps

现在,你已学会如何确定运行时异常,请继续完成下一教程,学习如何确定和诊断性能问题。Now that you've learned how to identify run-time exceptions, advance to the next tutorial to learn how to identify and diagnose performance issues.