You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core应用日志未显示在Application Insights中的问题排查

问题排查与修复方案

你的配置存在几个可能导致日志无法被Application Insights采集的问题,以下是具体修复步骤:

1. 移除重复的服务注册

你在Program.cs中调用了UseApplicationInsights(),这个方法已经自动完成了Application Insights核心服务(包括日志采集)的注册。而Startup.cs中重复添加的services.AddApplicationInsightsTelemetry();会导致配置冲突,干扰日志采集逻辑。直接删除这一行即可。

2. 完善appsettings.json的日志配置

当前的appsettings.json缺少Logging节点的明确配置,.NET Core默认的日志过滤规则可能会阻止Information级别日志被发送到Application Insights。添加以下配置:

"Logging": {
  "LogLevel": {
    "Default": "Information",
    "Microsoft": "Warning",
    "Microsoft.Hosting.Lifetime": "Information"
  },
  "ApplicationInsights": {
    "LogLevel": {
      "Default": "Information"
    }
  }
}

这个配置确保默认的Information级别日志能被Application Insights采集,同时过滤掉Microsoft类库的冗余Warning以下日志。

3. 确认日志筛选逻辑的一致性

你在Program.cs中设置的AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace)是有效的,但要注意:appsettings.json中的日志级别配置优先级高于代码中的AddFilter设置。如果appsettings里的级别设置为Information,那么Trace和Debug级别的日志不会被采集,但这并不影响你的LogInformation日志。

4. 排查Azure App Service扩展冲突

如果你的应用部署在Azure App Service且开启了Application Insights站点扩展,该扩展可能会与代码中的日志采集配置冲突。可以尝试在App Service的Application Insights设置中,关闭"日志(预览)"选项,仅保留代码层面的配置。

完成以上调整后,重启应用并触发_logger?.LogInformation("important info!")日志,等待5-10分钟后再用traces | sort by timestamp desc查询,应该就能看到对应的日志条目了。

内容的提问来源于stack exchange,提问作者AGuyCalledGerald

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 04:47:05