开始监视 ASP.NET Core Web 应用程序Start Monitoring Your ASP.NET Core Web Application

使用 Azure Application Insights,可轻松监视 Web 应用程序的可用性、性能和使用情况。With Azure Application Insights, you can easily monitor your web application for availability, performance, and usage. 还可以快速确定并诊断应用程序中的错误,而无需等待用户报告这些错误。You can also quickly identify and diagnose errors in your application without waiting for a user to report them.

本快速入门介绍如何将 Application Insights SDK 添加到现有 ASP.NET Core Web 应用程序。This quickstart guides you through adding the Application Insights SDK to an existing ASP.NET Core web application. 若要了解如何在没有 Visual Studio 的情况下配置 Application Insights,请查看此文章To learn about configuring Application Insights without Visual Studio checkout this article.

先决条件Prerequisites

若要完成本快速入门教程,需先执行以下操作:To complete this quickstart:

如果没有 ASP.NET Core Web 应用程序,可以使用分步指南来创建 ASP.NET Core 应用并添加 Application InsightsIf you don't have an ASP.NET Core web application, you can use our step-by-step guide to create an ASP.NET Core app and add Application Insights.

如果没有 Azure 订阅,请在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

登录到 Azure 门户Sign in to the Azure portal

登录到 Azure 门户Sign in to the Azure portal.

启用 Application InsightsEnable Application Insights

Application Insights 可以从任何连接 Internet 的应用程序收集遥测数据,而不考虑它是在本地运行还是在云中运行。Application Insights can gather telemetry data from any internet-connected application, regardless of whether it's running on-premises or in the cloud. 按照以下步骤开始查看此数据。Use the following steps to start viewing this data.

  1. 选择“创建资源” > “开发人员工具” > “Application Insights”。Select Create a resource > Developer tools > Application Insights.

    Note

    如果这是你首次创建 Application Insights 资源,可以通过访问创建 Application Insights 资源文档来了解更多信息。If this is your first time creating an Application Insights resource you can learn more by visiting the Create an Application Insights Resource doc.

    此时会显示配置对话框,请使用下表填写输入字段。A configuration box appears; use the following table to fill out the input fields.

    设置Settings ValueValue 说明Description
    名称Name 全局唯一值Globally Unique Value 标识所监视的应用的名称Name that identifies the app you are monitoring
    资源组Resource Group MyResourceGroupmyResourceGroup 用于托管 App Insights 数据的新资源组的名称。Name for the new resource group to host App Insights data. 可创建新的资源组或使用现有资源组。You can create a new resource group or use an existing one.
    位置Location 中国北部China North 选择离你近的位置或离托管应用的位置近的位置Choose a location near you, or near where your app is hosted
  2. 单击创建Click Create.

配置 App Insights SDKConfigure App Insights SDK

  1. 在 Visual Studio 中打开 ASP.NET Core Web 应用项目 > 在解决方案资源管理器中右键单击“应用名称”> 选择“添加” > “Application Insights 遥测”。Open your ASP.NET Core Web App project in Visual Studio > Right-click on the AppName in the Solution Explorer > Select Add > Application Insights Telemetry.

    添加 Application Insights 遥测

  2. 单击“入门”按钮Click the Get Started button

  3. 选择帐户和订阅 > 选择在 Azure 门户中创建的现有资源 > 单击“注册”。Select your account and subscription > Select the Existing resource you created in the Azure portal > Click Register.

  4. 选择“项目” > “管理 NuGet 包” > “包源: nuget.org” > “更新”,将 Application Insights SDK 包更新到最新稳定版本 。Select Project > Manage NuGet Packages > Package source: nuget.org > Update the Application Insights SDK packages to the latest stable release.

  5. 选择“调试” > “开始执行(不调试)”(Ctrl+F5) 启动应用Select Debug > Start without Debugging (Ctrl+F5) to Launch your app

    Application Insights 概述菜单

Note

这需要 3-5 分钟,然后数据才开始显示在门户中。It takes 3-5 minutes before data begins appearing in the portal. 如果此应用是一个低流量测试应用,请记住,仅当存在活动请求或操作时,才会捕获大多数指标。If this app is a low-traffic test app, keep in mind that most metrics are only captured when there are active requests or operations.

开始在 Azure 门户中监视Start monitoring in the Azure portal

  1. 在 Azure 门户中重新打开 Application Insights 的“概述”页(选择“主页”,然后在最新资源下选择此前创建的资源),以便查看有关当前正在运行的应用程序的详细信息。Reopen the Application Insights Overview page in the Azure portal by selecting Home and under recent resources select the resource you created earlier, to view details about your currently running application.

    Application Insights 概述菜单

  2. 单击“应用程序映射”以获取应用程序组件之间依赖关系的可视布局。Click Application map for a visual layout of the dependency relationships between your application components. 每个组件均显示 KPI,如负载、性能、失败和警报。Each component shows KPIs such as load, performance, failures, and alerts.

    应用程序地图

  3. 单击“应用分析”图标 “应用程序映射”图标 “在 Analytics 中查看”。Click on the App Analytics icon Application Map icon View in Analytics. 这将打开“Application Insights Analytics”,该软件提供丰富的查询语言,可用于分析 Application Insights 收集的所有数据。This opens Application Insights Analytics, which provides a rich query language for analyzing all data collected by Application Insights. 在本示例中,将生成以图表形式呈现请求计数的查询。In this case, a query is generated for you that renders the request count as a chart. 可以编写自己的查询来分析其他数据。You can write your own queries to analyze other data.

    一段时间内用户请求的分析图

  4. 返回到“概述”页并检查 KPI 仪表板。Return to the Overview page and examine the KPI Dashboards. 此仪表板提供有关应用程序运行状况的统计信息,包括传入请求数、这些请求的持续时间,以及发生的任何故障。This dashboard provides statistics about your application health, including the number of incoming requests, the duration of those requests, and any failures that occur.

    “运行状况概述时间线”图

  5. 在左侧单击“指标”。On the left click on Metrics. 使用指标资源管理器调查资源的运行状况和利用率。Use the metrics explorer to investigate the health and utilization of your resource. 可以单击“添加新图表”创建其他自定义视图,或者选择“编辑”修改现有的图表类型、高度、调色板、分组和指标。You can click Add new chart to create additional custom views or select Edit to modify the existing chart types, height, color palette, groupings, and metrics. 例如,可以制作一个图表来显示浏览器页面的平均加载时间,只需从指标下拉列表中选取“浏览器页面加载时间”并从聚合中选择“平均”即可。For example, you can make a chart that displays the average browser page load time by picking "Browser page load time" from the metrics drop down and "Avg" from aggregation. 若要详细了解 Azure 指标资源管理器,请访问 Azure 指标资源管理器入门To learn more about Azure Metrics Explorer visit Getting started with Azure Metrics Explorer.

    “指标”选项卡:浏览器网页平均加载时间图表

清理资源Clean up resources

完成测试后,即可删除资源组和所有相关的资源。When you are done testing, you can delete the resource group and all related resources. 为此,请执行以下步骤。To do so follow the steps below.

Note

如果使用了现有资源组,以下说明将不起作用,只需删除单个 Application Insights 资源即可。If you used an existing resource group the instructions below will not work and you will need to just delete the individual Application Insights resource. 请记住,无论何时删除资源组,都将删除作为该组成员的所有底层资源。Keep in mind anytime you delete a resource group all underyling resources that are members of that group will be deleted.

  1. 在 Azure 门户的左侧菜单中,单击“资源组”,然后单击“myResourceGroup”。From the left-hand menu in the Azure portal, click Resource groups and then click myResourceGroup.
  2. 在资源组页上单击“删除”,在文本框中键入 myResourceGroup,并单击“删除”。On your resource group page, click Delete, type myResourceGroup in the text box, and then click Delete.

后续步骤Next steps