ApplicationInsights没有考虑JSON配置的日志级别。

3

假设有这个 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输出中仅显示关键、错误和警告信息?
例如,从控制器的任意日志中。
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}");
}

根据 Microsoft 文档 这里,在启用常规应用程序性能监视的情况下,Microsoft.ApplicationInsights.AspNet SDK 版本 2.7.1(及更高版本)默认启用 ApplicationInsightsLoggerProvider。可以通过以下两种方法之一来开启应用程序性能监视:

  • 通过对 IWebHostBuilder 调用 UseApplicationInsights 扩展方法(现已过时)。
  • 通过对 IServiceCollection 调用 AddApplicationInsightsTelemetry 扩展方法。
因此,我有点不明白为什么 Debug/Info 跟踪信息没有输出。
在 appsettings.development.json 中没有覆盖或记录设置。
我正在使用 AppInsights SDK 2.13.1。
Asp NET Core 3.1。
1个回答

3
如果确定没有其他地方覆盖了日志级别,那么appsettings.json文件可能有问题。应用程序洞察的日志级别应该放在.json文件中的Logging元素中(请参见官方文档此处)。正确的格式如下:
{
  "ApplicationInsights": {
    "InstrumentationKey": "xxxxx"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Debug"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug"
      }
    }
  },
  "AllowedHosts": "*"
}

测试结果如下图所示:

enter image description here


成功了 - 将ApplicationInsights/LogLevel移动到appsettings.json中的Logging节点中。谢谢! - glosrob

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接