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

ASP.NET Core Web API中Application Insights无法采集Information级别日志

解决方案

核心原因

你当前配置不生效是因为控制Application Insights日志采集级别的配置位置错误,根节点下ApplicationInsights块中的LogLevel配置不负责过滤ILogger输出的日志,正确的规则需要配置在Logging节点下。同时Application Insights日志Provider的默认最低采集级别为Warning,未显式配置的情况下只会采集Warning及以上级别的日志。

具体修改步骤

第一步:调整appsettings.json配置

修改配置文件的Logging节点,新增Application Insights专属的日志级别规则:

{
  "ApplicationInsights": {
    "InstrumentationKey": "my-instrumentation-key",
    "EnableAdaptiveSampling": false,
    "EnablePerformanceCounterCollectionModule": false
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    },
    // 新增Application Insights日志过滤规则
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Information",
        // 可单独指定命名空间的日志级别,过滤系统类库的冗余日志
        "Microsoft": "Warning",
        "System": "Warning"
      }
    }
  },
  "AllowedHosts": "*"
}

第二步(可选,配置文件不生效时使用):显式配置日志Provider

打开Program.cs,在CreateHostBuilder方法中添加Application Insights日志Provider的显式配置:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureLogging(logging =>
        {
            // 注册Application Insights日志Provider
            logging.AddApplicationInsights();
            // 显式设置全局采集最低级别为Information
            logging.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("", LogLevel.Information);
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

验证方式

修改完成后重新发布应用,触发接口请求后等待3-5分钟(Application Insights数据存在天然传输延迟),再查询traces表即可看到Information级别的日志。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 21:36:03