.NET 8 ASP.NET Core应用Azure Application Insights无请求数据问题
解决Azure App Service发布后Application Insights请求数据丢失问题
现象总结
- 发布代码更新前Application Insights运行正常
- 发布后异常表现:
- 门户概览图表无请求数据(呈平线)
- Live Metrics面板显示“不可用:无法连接到你的应用程序”
- Transaction Search仅能检索到TRACE级日志,无请求、依赖等核心遥测数据
- 涉及应用:.NET 8 ASP.NET Core
排查与修复步骤
1. 验证SDK配置完整性
- 检查
Program.cs中是否正确启用Application Insights请求跟踪:// 确保已添加基础配置 builder.Services.AddApplicationInsightsTelemetry(); // 确认未手动禁用请求收集 builder.Services.Configure<ApplicationInsightsServiceOptions>(options => { options.EnableRequestTrackingTelemetryModule = true; }); - 核对
appsettings.json或环境变量中的Instrumentation Key/Connection String,确保与Azure门户中目标AI实例一致,避免发布时被覆盖。
2. 检查App Service运行时配置
- 进入Azure门户目标App Service的配置 > 应用程序设置:
- 确认
APPINSIGHTS_INSTRUMENTATIONKEY或APPLICATIONINSIGHTS_CONNECTION_STRING存在且值正确 - 排查是否存在禁用AI的环境变量(如
ApplicationInsights__Enabled设为false)
- 确认
3. 修复Live Metrics连接问题
- 确认项目已安装
Microsoft.ApplicationInsights.PerfCounterCollector包(.NET 8默认包含,若手动移除需重新安装) - 若App Service使用自定义NSG,需允许出站访问
dc.services.visualstudio.com和live.monitor.azure.com
4. 排查遥测过滤规则
- 检查代码中是否存在遥测处理器,错误过滤了请求遥测:
// 错误示例:丢弃所有请求遥测 public class FilterTelemetryProcessor : ITelemetryProcessor { private readonly ITelemetryProcessor _next; public FilterTelemetryProcessor(ITelemetryProcessor next) => _next = next; public void Process(ITelemetry item) { if (item is RequestTelemetry) return; // 此处会导致请求数据丢失 _next.Process(item); } } - 检查AI门户中的数据采样设置,确认未将请求数据的采样率设为0%
5. 分析App Service日志
- 打开App Service的日志流,搜索Application Insights相关报错(如初始化失败、遥测发送异常)
- 查看
stdout日志,确认是否存在SDK依赖缺失或权限问题
内容的提问来源于stack exchange,提问作者prb
相关产品推荐
相关产品推荐

