选择正确的 SDK 版本 📅 2026/6/26 2:37:35 选择正确的 SDK 版本ASP.NET Framework 与 ASP.NET Core 使用不同的 NuGet 包请勿混淆框架NuGet 包当前推荐版本ASP.NET Framework 4.6.2Microsoft.ApplicationInsights.Web2.22.xASP.NET CoreMicrosoft.ApplicationInsights.AspNetCore2.22.xWorker Service / ConsoleMicrosoft.ApplicationInsights.WorkerService2.22.x对于 .NET Framework 4.8 项目我们使用Microsoft.ApplicationInsights.Web。注意请使用Microsoft.ApplicationInsights.Web2.23.0安装该包后会自动引入以下组件Microsoft.ApplicationInsights— 核心 SDK提供 TelemetryClientMicrosoft.AI.Web— 自动追踪 HTTP 请求和响应Microsoft.AI.DependencyCollector— 自动追踪 SQL 查询、HTTP 外部调用等依赖项Microsoft.AI.PerfCounterCollector— 收集 CPU、内存等性能计数器Microsoft.AspNet.TelemetryCorrelation— 分布式追踪关联集成步骤第一步安装 NuGet 包在 Visual Studio 中打开 Package Manager Console执行Install-Package Microsoft.ApplicationInsights.Web -Version 2.23.0或者通过 NuGet Package Manager UI 搜索 Microsoft.ApplicationInsights.Web 并安装2.23.0版。第二步配置 Connection String安装完成后项目根目录会自动生成 ApplicationInsights.config 文件。打开该文件配置 Connection String?xml version1.0 encodingutf-8? ApplicationInsights xmlnshttp://schemas.microsoft.com/ApplicationInsights/2013/Settings ConnectionStringInstrumentationKeyxx-x-x-x-xxx;EndpointSuffixapplicationinsights.azure.cn;IngestionEndpointhttps://chinanorth3-0.in.applicationinsights.azure.cn/;LiveEndpointhttps://chinanorth3.livediagnostics.monitor.azure.cn/;AADAudiencehttps://monitor.azure.cn/;ApplicationId xx-x-x-x-xxx /ConnectionString TelemetryInitializers !-- 自动生成的初始化器配置 -- /TelemetryInitializers TelemetryModules !-- 自动生成的模块配置 -- /TelemetryModules /ApplicationInsightsPS示例使用的是中国区的Azure Application Insights 服务所以Connection String指向中国区 endpointIngestionEndpointhttps://dc.applicationinsights.azure.cn/;LiveEndpointhttps://live.applicationinsights.azure.cn/也可以使用环境变量 APPLICATIONINSIGHTS_CONNECTION_STRING来保存连接字符串。第三步验证 web.config 配置NuGet 包安装时会自动修改 web.config添加两个关键的 HTTP Module。请确认以下配置存在configuration system.webServer modules remove nameTelemetryCorrelationHttpModule / add nameTelemetryCorrelationHttpModule typeMicrosoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation preConditionmanagedHandler / remove nameApplicationInsightsWebTracking / add nameApplicationInsightsWebTracking typeMicrosoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web preConditionmanagedHandler / /modules /system.webServer /configuration提示这两个 Module 的顺序很重要 — TelemetryCorrelationHttpModule 必须在前负责分布式追踪的上下文传播。第四步发送自定义遥测数据可选SDK 安装后请求、依赖、异常等已自动收集。如需发送自定义事件或指标using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights.DataContracts; public class OrderController : Controller { private readonly TelemetryClient _telemetryClient new TelemetryClient(); … // 追踪自定义事件 _telemetryClient.TrackEvent(test event, new Dictionarystring, string { { event ID, Guid.NewGuid().ToString() }, { event Title, this is custome events title 20260514 } }); // 追踪自定义指标 _telemetryClient.TrackMetric(customer metrics, (new Random()).NextDouble()); … }第五步验证数据上报1. 本地运行项目触发几个页面请求2. 打开 Azure Portal → Application Insights 资源3. 进入Search确认能看到请求遥测数据4. 检查 Live Metrics确认实时数据流正常