azure - ApplicationInsights 不考虑 json 配置的日志级别

标签 azure asp.net-core azure-application-insights asp.net-core-3.1 ilogger

鉴于此 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 输出中仍然只看到严重、错误和警告?

例如来自 Controller 的任意日志

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

所以我有点不明白为什么没有输出调试/信息跟踪。

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

我使用的是 AppInsights SDK 2.13.1。

Asp NET Core 3.1。

最佳答案

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

正确的应该是这样的:

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

测试结果:

enter image description here

关于azure - ApplicationInsights 不考虑 json 配置的日志级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60928341/

相关文章:

git - "fatal: Authentication failed"与 git-credential-manager

powershell - 无法连接到服务器: dial tcp [::1]:8080: connectex: 由于目标机器主动拒绝而无法建立连接

c# - Razor Pages - 使用多个提交按钮或多个表单处理分页

c# - 微服务架构数据库回滚

Azure错误: Client side authentication flow with Google is not supported

c# - azure : data storage with append

uwp - Application Insights 发送事件需要时间

azure - 如何在 Azure 应用程序见解中执行明确的声明?

asp.net-core - MVC 6 安装为 Windows 服务 (ASP.NET Core 1.0.0)

azure - 使用来自多个区域的 Application Insights