ApplicationInsights 未考虑 json 配置的日志级别

ApplicationInsights is not considering json configured log level

鉴于此 appsettings.json

{
  "ApplicationInsights": {
    "InstrumentationKey": "foobar",
    "LogLevel": {
      "Default": "Debug"
    }
  },
  "Logging": {
    "LogLevel": {
      "Default": "Debug"
    }
  },
  "AllowedHosts": "*"
}

这来自 startup.cs

public void ConfigureServices(IServiceCollection services)
{
    var appSettingsSection = Configuration.GetSection("TD");
    var appSettings = new AppSettings();
    new ConfigureFromConfigurationOptions<AppSettings>(appSettingsSection).Configure(appSettings);
    services.Configure<AppSettings>(appSettingsSection);

    services.AddApplicationInsightsTelemetry();

    services.AddTransient<IMyService, MyService>();
    services.AddControllersWithViews();
    services.AddSpaStaticFiles(configuration =>
    {
        configuration.RootPath = "ClientApp";
    });
}

为什么我的 AI 输出中仍然只看到 Critical、Error 和 Warning?

例如来自控制器的任意日志

public async Task<IActionResult> MyAction(string foobar)
{
    Logger.LogDebug($"Ilogger debug {foobar}");
    Logger.LogInformation($"Ilogger info {foobar}");
    Logger.LogWarning($"Ilogger warning {foobar}");
    Logger.LogError($"Ilogger error {foobar}");
    Logger.LogCritical($"Ilogger critical {foobar}");
}

根据微软文档 here

ApplicationInsightsLoggerProvider is enabled by default in Microsoft.ApplicationInsights.AspNet SDK version 2.7.1 (and later) when you turn on regular Application Insights monitoring through either of the methods:

  • By calling the UseApplicationInsights extension method onIWebHostBuilder (Now obsolete)
  • By calling the AddApplicationInsightsTelemetry extension method on IServiceCollection

所以我有点迷惑为什么 Debug/Info 跟踪没有被输出。

appsettings.development.json 中没有覆盖或日志记录设置。

我正在使用 AppInsights SDK 2.13.1。

Asp.NET 核心 3.1.

appsettings.json 有问题(如果您确定没有其他地方覆盖日志记录级别)。应用程序洞察的日志级别应放在 .json 文件的 Logging 元素中(参见官方文档 here)。

正确的应该是这样的:

{
  "ApplicationInsights": {
    "InstrumentationKey": "xxxxx"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Debug"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug"
      }
    }
  },
  "AllowedHosts": "*"
}

而测试结果: