为 ASP.NET 网站设置 Application InsightsSet up Application Insights for your ASP.NET website

此过程将 ASP.NET Web 应用配置为将遥测发送到 Azure Application Insights 服务。This procedure configures your ASP.NET web app to send telemetry to the Azure Application Insights service. 它适用于托管在你自己的本地或云中 IIS 服务器上的 ASP.NET 应用。It works for ASP.NET apps that are hosted either in your own IIS server on-premises or in the Cloud. 你会获得图表和功能强大的查询语言,这有助于你了解应用的性能以及用户使用它的方式。另外,在出现故障或性能问题时还会自动发送警报。You get charts and a powerful query language that help you understand the performance of your app and how people are using it, plus automatic alerts on failures or performance issues. 许多开发人员发现这些功能很有用,不过你也可以根据需要扩展和自定义遥测。Many developers find these features great as they are, but you can also extend and customize the telemetry if you need to.

在 Visual Studio 中单击几下即可完成安装。Setup takes just a few clicks in Visual Studio. 可以选择对遥测量进行限制,从而避免付费。You have the option to avoid charges by limiting the volume of telemetry. 可以通过此功能对用户不多的站点进行试验和调试,或者对其进行监视。This functionality allows you to experiment and debug, or to monitor a site with not many users. 如果认为需要前进一步,对生产站点进行监视,则可轻松地在以后提高该限制。When you decide you want to go ahead and monitor your production site, it's easy to raise the limit later.


若要要将 Application Insights 添加到 ASP.NET 网站,需要:To add Application Insights to your ASP.NET website, you need to:

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

步骤 1:添加 Application Insights SDKStep 1: Add the Application Insights SDK


此示例中的屏幕截图基于 Visual Studio 2017 版本 15.9.9 及更高版本。The screenshots in this example are based on Visual Studio 2017 version 15.9.9 and later. 添加 Application Insights 的体验因 Visual Studio 版本以及 ASP.NET 模板类型而异。The experience to add Application Insights varies across versions of Visual Studio as well as by ASP.NET template type. 较早的版本可能有其他文本,如“配置 Application Insights”。Older versions may have alternate text such as "Configure Application Insights".

在解决方案资源管理器中右键单击 Web 应用名称,并选择“添加” > “Application Insights 遥测”Right-click your web app name in the Solution Explorer, and choose Add > Application Insights Telemetry

解决方案资源管理器的屏幕截图,其中突出显示了“配置 Application Insights”

(根据所用的 Application Insights SDK 版本,系统可能会提示升级到最新的 SDK 版本。(Depending on your Application Insights SDK version you may be prompted to upgrade to the latest SDK release. 如果出现提示,请选择“更新 SDK”。)If prompted, select Update SDK.)

屏幕截图:新版的 Microsoft Application Insights SDK 可用。

Application Insights 配置屏幕:Application Insights Configuration screen:

选择“入门”。Select Get Started.

使用 Application Insights 页注册应用的屏幕截图

如果想要设置用于存储数据的资源组或位置,请单击“配置设置”。If you want to set the resource group or the location where your data is stored, click Configure settings. 资源组用于控制对数据的访问。Resource groups are used to control access to data. 例如,如果有多个应用构成了同一个系统的一部分,可在同一个资源组中放置这些应用的 Application Insights 数据。For example, if you have several apps that form part of the same system, you might put their Application Insights data in the same resource group.

选择“注册”。Select Register.

使用 Application Insights 页注册应用的屏幕截图

选择“项目” > “管理 NuGet 包” > “包源: nuget.org”> 确认你具备 Application Insights SDK 的最新稳定版本 。Select Project > Manage NuGet Packages > Package source: nuget.org > Confirm that you have the latest stable release of the Application Insights SDK.

在调试期间以及发布应用后,遥测数据将发送到 Azure 门户Telemetry will be sent to the Azure portal, both during debugging and after you have published your app.


如果不希望在进行调试时向门户发送遥测,则请直接向应用添加 Application Insights SDK,但不要在门户中配置资源。If you don't want to send telemetry to the portal while you're debugging, just add the Application Insights SDK to your app but don't configure a resource in the portal. 在调试时,可以在 Visual Studio 中查看遥测数据。You are able to see telemetry in Visual Studio while you are debugging. 稍后可以返回此配置页,或者等到部署应用后,启用在运行时打开遥测Later, you can return to this configuration page, or you could wait until after you have deployed your app and switch on telemetry at run time.

步骤 2:运行应用程序Step 2: Run your app

使用 F5 运行应用。Run your app with F5. 打开不同的页以生成一些遥测数据。Open different pages to generate some telemetry.

Visual Studio 中会显示已记录的事件数。In Visual Studio, you will see a count of the events that have been logged.

Visual Studio 的屏幕截图。

步骤 3:查看遥测Step 3: See your telemetry

可在 Visual Studio 或 Application Insights Web 门户中查看遥测数据。You can see your telemetry either in Visual Studio or in the Application Insights web portal. 在 Visual Studio 中搜索遥测,以便对应用进行调试。Search telemetry in Visual Studio to help you debug your app. 当系统运行时,在 Web 门户中监视性能和使用情况。Monitor performance and usage in the web portal when your system is live.

在 Visual Studio 中查看遥测See your telemetry in Visual Studio

在 Visual Studio 中查看 Application Insights 数据。In Visual Studio, to view Application Insights data. 选择“解决方案资源管理器” > “连接的服务”,右键单击“Application Insights”,然后单击“搜索实时遥测”。Select Solution Explorer > Connected Services > right-click Application Insights, and then click Search Live Telemetry.

在 Visual Studio Application Insights 搜索窗口中,可以看到应用程序的服务器端生成的遥测数据。In the Visual Studio Application Insights Search window, you will see the data from your application for telemetry generated in the server side of your app. 使用这些筛选器进行试验,并单击任何事件以查看更多详细信息。Experiment with the filters, and click any event to see more detail.

Application Insights 窗口中“来自调试会话的数据”视图的屏幕截图。


如果看不到任何数据,请确保时间范围正确,并单击“搜索”图标。If you don't see any data, make sure the time range is correct, and click the Search icon.

了解有关 Visual Studio 中的 Application Insights Tools 的详细信息Learn more about Application Insights tools in Visual Studio.

在 Wb 门户中查看遥测See telemetry in web portal

还可以在 Application Insights Web 门户中查看遥测(除非选择仅安装 SDK)。You can also see telemetry in the Application Insights web portal (unless you chose to install only the SDK). 该门户中的图表、分析工具和跨组件视图比 Visual Studio 中的多。The portal has more charts, analytic tools, and cross-component views than Visual Studio. 此门户还提供警报。The portal also provides alerts.

打开 Application Insights 资源。Open your Application Insights resource. 登录到 Azure 门户并在其中找到所需的资源,或者选择“解决方案资源管理器” > “连接的服务”,然后右键单击“Application Insights”并选择“打开 Application Insights 门户”,以转到相应的资源。 > Either sign into the Azure portal and find it there, or select Solution Explorer > Connected Services > right-click Application Insights > Open Application Insights Portal and let it take you there.

门户将从应用打开遥测视图。The portal opens on a view of the telemetry from your app.

Application Insights 概述页的屏幕截图

在门户中,单击任何磁贴或图表以查看更多详细信息。In the portal, click any tile or chart to see more detail.

步骤 4:发布应用Step 4: Publish your app

将应用发布到 IIS 服务器或 Azure。Publish your app to your IIS server or to Azure. 监视 实时指标流 ,确保一切平稳运行。Watch Live Metrics Stream to make sure everything is running smoothly.

遥测数据会在 Application Insights 门户中累积,可在该门户中监视指标、搜索遥测数据。Your telemetry builds up in the Application Insights portal, where you can monitor metrics, search your telemetry. 还可以使用功能强大的 Kusto 查询语言来分析使用情况和性能,或查找特定的事件。You can also use the powerful Kusto query language to analyze usage and performance, or to find specific events.

还可以继续在 Visual Studio 中借助诊断搜索和趋势等工具来分析遥测。You can also continue to analyze your telemetry in Visual Studio, with tools such as diagnostic search and trends.


如果应用发送的遥测足够达到限制,自动采样会打开。If your app sends enough telemetry to approach the throttling limits, automatic sampling switches on. 采样可以减少从应用发送的遥测数量,同时为诊断保留相关数据。Sampling reduces the quantity of telemetry sent from your app, while preserving correlated data for diagnostic purposes.

已经完成全部设置You're all set

祝贺你!Congratulations! 已在应用中安装 Application Insights 包,并将其配置为向 Azure 中的 Application Insights 服务发送遥测。You installed the Application Insights package in your app, and configured it to send telemetry to the Application Insights service on Azure.

接收应用遥测的 Azure 资源通过“检测密钥”进行标识。The Azure resource that receives your app's telemetry is identified by an instrumentation key. 可以在 ApplicationInsights.config 文件中找到该密钥。You'll find this key in the ApplicationInsights.config file.

升级到更高的 SDK 版本Upgrade to future SDK versions

若要升级到 SDK 的新版本,请打开 NuGet 包管理器,并筛选已安装的包。To upgrade to a new release of the SDK, open the NuGet package manager, and filter on installed packages. 选择“Microsoft.ApplicationInsights.Web”,并选择“升级”。Select Microsoft.ApplicationInsights.Web, and choose Upgrade.

如果对 ApplicationInsights.config 执行了任何自定义操作,请在升级前保存相关副本。If you made any customizations to ApplicationInsights.config, save a copy of it before you upgrade. 然后,将更改合并到新版本中。Then, merge your changes into the new version.

后续步骤Next steps

如果对以下主题感兴趣,请查看其他相关文章:There are alternative topics to look at if you are interested in:

其他遥测数据More telemetry



  • 可用性测试:创建测试来确保站点在 Web 上可见。Availability tests: Create tests to make sure your site is visible on the web.
  • 智能诊断:这些测试可自动运行,因此不需要进行任何设置。Smart diagnostics: These tests run automatically, so you don't have to do anything to set them up. 它们会告诉你应用是否具有异常的失败请求速率。They tell you if your app has an unusual rate of failed requests.
  • 指标警报:设置警报以在某个指标超过阈值时发出警告。Metric alerts: Set alerts to warn you if a metric crosses a threshold. 可以在编码到应用中的自定义指标中设置它们。You can set them on custom metrics that you code into your app.