为什么 ASP.NET Core 3.1 应用程序中的 appsettings 中的 App Insight LogLevel 会被忽略?

Pau*_*ski 13 c# .net-core asp.net-core appinsights

我们在 ASP.NET Core 3.1 网站中使用 ILogger,使用 Microsoft.ApplicationInsights.AspNetCore 包,版本 2.14。我们正在尝试启用将信息性消息记录到 App Insight 中,例如。_logger.LogInformation("这里的信息')

在 ConfigureServices 中启动时,我们启用 App Insights:

services.AddApplicationInsightsTelemetry();
Run Code Online (Sandbox Code Playgroud)

我们的 appsettings.json 文件中有以下内容:

{
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Information"
        }
    },
    "ApplicationInsights": {
        "InstrumentationKey": "12345678-1234-5678-1234-1234567890ab",
        "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Information"
        }
    },
Run Code Online (Sandbox Code Playgroud)

它正确获取应用程序洞察键(我们确实获得了正常指标、日志条目,例如异常),但是我们对 logger.LogInformation("Info here") 的调用都没有被发送/记录在 App Insight 仪表板中。

我发现了这个类似的问题:

ILogger 不尊重 Application Insights 的日志级别

但是答案仍然没有真正解决如何能够从 appsettings.json 文件更改日志级别与将日志级别硬编码到代码中。

从文档来看,这似乎应该“正常工作”,但似乎没有。

? https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger#control-logging-level

我们哪里错了?

Kir*_*kin 17

你在错误的地方得到了LogLevel财产ApplicationInsights。它应该是这样的:

"Logging": {
    "LogLevel": {
        "Default": "Information",
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information"
    },
    "ApplicationInsights": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Information"
        }
    }
},
"ApplicationInsights": {
    "InstrumentationKey": "12345678-1234-5678-1234-1234567890ab"    
}
Run Code Online (Sandbox Code Playgroud)

日志级配置位于Logging父级之下,但InstrumentationKey位于该层次结构之外。

有关特定于提供程序的配置的更多信息,请参阅官方 ASP.NET Core 文档中的配置日志记录