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

如何配置Application Insights以记录Debug级别的日志?

问题:Application Insights无法记录Debug级别日志

我在配置Application Insights时遇到了无法记录Debug级别日志的问题,日志代码如下:

_logger.LogTrace("Trace: I was here");
_logger.LogDebug("Debug: I was here");
_logger.LogInformation("Information: I was here");
_logger.LogWarning("Warning: I was here");
_logger.LogError("Error: I was here");
_logger.LogCritical("Critical: I was here");

配置文件设置:

"ApplicationInsights": {
    "ConnectionString": "NotPostingMyConnectionStringOnStackOverflow" 
  },

  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "Microsoft": "Debug"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug"
      }
    }
  },

Program.cs配置:

builder.Services.AddApplicationInsightsTelemetry();

运行后,Application Insights仅显示Warning及以上级别的日志,请问如何设置才能让其记录Debug级别的日志?

解决方案

要让Application Insights捕获Debug及更高级别的日志,需要调整以下关键配置:

  • 修正配置文件的日志处理器设置
    默认情况下,Application Insights的日志采集器会过滤掉Warning以下级别的日志,仅靠Logging.ApplicationInsights.LogLevel无法覆盖这个默认规则。需要在appsettings.json中明确配置Application Insights的日志提供器级别:
"Logging": {
  "LogLevel": {
    "Default": "Debug",
    "Microsoft": "Debug"
  },
  "Providers": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug"
      },
      "Enabled": true
    }
  }
}

或者直接在Application Insights节点下配置日志级别:

"ApplicationInsights": {
  "ConnectionString": "你的连接字符串",
  "Logging": {
    "LogLevel": {
      "Default": "Debug"
    }
  }
}
  • 优化Program.cs中的服务配置
    在.NET 6+中,添加Application Insights服务时,可以显式启用Debug日志采集,确保配置生效:
builder.Services.AddApplicationInsightsTelemetry(options =>
{
    options.EnableDebugLogger = true;
});

也可以直接绑定配置文件,让系统自动读取所有Application Insights相关设置:

builder.Services.AddApplicationInsightsTelemetry(builder.Configuration);
  • 排查配置优先级冲突
    确认没有环境变量、其他环境配置文件(如appsettings.Development.json)中的设置覆盖了当前的日志级别,确保所有配置中Default日志级别都设置为Debug或更低。

完成以上设置后,重启应用,Debug及以上级别的日志就能正常出现在Application Insights的日志查询界面中。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 21:08:13