开始监视 ASP.NET Web 应用程序Start monitoring your ASP.NET 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 收集的有关应用的性能和效率的信息,可在维护和改进应用程序时做出明智的选择。With the information that you collect from Application Insights about the performance and effectiveness of your app, you can make informed choices to maintain and improve your application.

本快速入门演示如何将 Application Insights 添加到现有 ASP.NET Web 应用程序并开始分析实时统计信息,这只是可用来分析应用程序的各种方法之一。This quickstart shows how to add Application Insights to an existing ASP.NET web application and start analyzing live statistics, which is just one of the various methods you can use to analyze your application. 如果没有 ASP.NET Web 应用程序,则可以按照创建 ASP.NET Web 应用快速入门创建一个。If you do not have an ASP.NET web application, you can create one following the Create an ASP.NET Web App quickstart.

先决条件Prerequisites

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

  • 使用以下工作负荷安装 Visual Studio 2019Install Visual Studio 2019 with the following workloads:
    • ASP.NET 和 Web 开发ASP.NET and web development
    • Azure 开发Azure development

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

启用 Application InsightsEnable Application Insights

  1. 在 Visual Studio 2019 中打开你的项目。Open your project in Visual Studio 2019.

  2. 从“项目”菜单中选择“配置 Application Insights”。Select Configure Application Insights from the Project menu. Visual Studio 会将 Application Insights SDK 添加到应用程序。Visual Studio adds the Application Insights SDK to your application.

    重要

    添加 Application Insights 的过程因 ASP.NET 模板类型而异。The process to add Application Insights varies by ASP.NET template type. 若要使用“空”或“Azure 移动应用”模板, 请选择“项目” > “添加 Application Insights 遥测”。If you are using the Empty or Azure Mobile App template select Project > Add Application Insights Telemetry. 有关所有其他 ASP.NET 模板,请参阅上述步骤中的说明。For all other ASP.NET templates consult the instructions in the step above.

  3. 单击“开始使用”(早期版本的 Visual Studio 会改为使用“免费开始”按钮)。Click Get Started (earlier versions of Visual Studio have a Start Free button instead).

    将 Application Insights 添加到 Visual Studio

  4. 选择订阅并单击“注册”。Select your subscription and click Register.

  5. 选择“项目” > “管理 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.

  6. 通过从“调试”菜单中选择“启动调试”或通过按 F5 键来运行应用程序。Run your application by either selecting Start Debugging from the Debug menu or by pressing the F5 key.

确认应用配置Confirm app configuration

Application Insights 收集应用程序的遥测数据,而不考虑应用程序在何处运行。Application Insights gathers telemetry data for your application regardless of where it's running. 按照以下步骤开始查看此数据。Use the following steps to start viewing this data.

  1. 通过单击“查看” -> “其他 Windows” -> “Application Insights 搜索”打开 Application Insights。Open Application Insights by clicking View -> Other Windows -> Application Insights Search. 从当前会话查看遥测数据。You see the telemetry from your current session.

    Visual Studio 中的遥测Telemetry in Visual Studio

  2. 单击列表中的第一个请求(在此示例中为“获取主页/索引”)查看请求详细信息。Click on the first request in the list (GET Home/Index in this example) to see the request details. 请注意,包含状态代码和响应时间以及有关请求的其他重要信息。Notice that the status code and response time are both included along with other valuable information about the request.

    Visual Studio 中的响应详细信息

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

现在可以在 Azure 门户中打开 Application Insights,查看有关正在运行的应用程序的各种详细信息。You can now open Application Insights in the Azure portal to view various details about your running application.

  1. 在解决方案资源管理器中展开“连接的服务”文件夹(云和插头图标),然后右键单击 Application Insights 文件夹,单击“打开 Application Insights 门户”。Expand the Connected Services folder (cloud and plug icon) in the Solution Explorer then right-click on the Application Insights folder and click Open Application Insights Portal. 可以看到有关应用程序的一些信息和各种选项。You see some information about your application and a variety of options.

    应用程序地图

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

    应用程序地图

  3. 单击其中一个应用程序组件上的“应用分析”图标 应用程序地图“在日志(分析)中查看”。Click on the App Analytics icon Application Map View in Logs (Analytics) on one of the application components. 这将打开“日志(分析)”,该软件提供丰富的查询语言,可用于分析 Application Insights 收集的所有数据。This opens Logs (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. 在“调查”下,单击左侧的“实时指标流”。Click on Live Metrics Stream on the left under investigate. 这将显示有关正在运行的应用程序的实时统计信息。This shows live statistics about your application as it's running. 这包括传入请求数、这些请求的持续时间和发生的任何故障等信息。This includes such information as the number of incoming requests, the duration of those requests, and any failures that occur. 还可以检查处理器和内存等关键性能指标。You can also inspect critical performance metrics such as processor and memory.

    实时流

    如果已准备好在 Azure 中托管应用程序,则可以立即发布它。If you are ready to host your application in Azure, you can publish it now. 按照创建 ASP.NET Web 应用快速入门中所述的步骤进行操作。Follow the steps described in Create an ASP.NET Web App Quickstart.

  5. 如果使用 Visual Studio 来添加 Application Insights 监视,则可自动添加客户端监视。If you use Visual Studio to add Application Insights monitoring, you can automatically add client-side monitoring. 若要手动向应用程序添加客户端监视,请将以下 JavaScript 添加到应用程序中:To add client-side monitoring manually to an application add the following JavaScript to your application:

<!-- 
To collect user behavior analytics about your application, 
insert the following script into each page you want to track.
Place this code immediately before the closing </head> tag,
and before any other scripts. Your first data will appear 
automatically in just a few seconds.
-->
<script type="text/javascript">
var appInsights=window.appInsights||function(a){
  function b(a){c[a]=function(){var b=arguments;c.queue.push(function(){c[a].apply(c,b)})}}var c={config:a},d=document,e=window;setTimeout(function(){var b=d.createElement("script");b.src=a.url||"https://az416426.vo.msecnd.net/scripts/a/ai.0.js",d.getElementsByTagName("script")[0].parentNode.appendChild(b)});try{c.cookie=d.cookie}catch(a){}c.queue=[];for(var f=["Event","Exception","Metric","PageView","Trace","Dependency"];f.length;)b("track"+f.pop());if(b("setAuthenticatedUserContext"),b("clearAuthenticatedUserContext"),b("startTrackEvent"),b("stopTrackEvent"),b("startTrackPage"),b("stopTrackPage"),b("flush"),!a.disableExceptionTracking){f="onerror",b("_"+f);var g=e[f];e[f]=function(a,b,d,e,h){var i=g&&g(a,b,d,e,h);return!0!==i&&c["_"+f](a,b,d,e,h),i}}return c
  }({
      instrumentationKey:"<your instrumentation key>"
  });

window.appInsights=appInsights,appInsights.queue&&0===appInsights.queue.length&&appInsights.trackPageView();
</script>

若要了解详细信息,请访问适用于开源 JavaScript SDK 的 GitHub 存储库。To learn more, visit the GitHub repository for our open-source JavaScript SDK.

清理资源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.

  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

在本快速入门中,已通过 Azure Application Insights 为应用程序启用了监视。In this quickstart, you’ve enabled your application for monitoring by Azure Application Insights. 继续学习教程,了解如何使用它来监视统计信息和检测应用程序中的问题。Continue to the tutorials to learn how to use it to monitor statistics and detect issues in your application.