Log.Information 不打印日志消息,尽管阈值设置为调试

问题描述 投票:0回答:1

我有一个 Azure 函数,在调试中运行时,我很难将信息记录到控制台。我相信我已经在

local.settings.json
中正确配置了它。您可以在这里查看我的文件内容:

本地.settings.json

{
  "version": "2.0",
  "logging": {
    "logLevel": {
      "default": "Debug",
      "Microsoft": "Error"
    },
    "console": {
      "isEnabled": true
    }
  },
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    "ServiceBusConnectionString": "..."
  }
}

我正在通过在代码中放置这三个日志语句来测试它,我知道这些语句已被执行:

函数内的代码

_logger.LogInformation("+++TESTING INFORMATION LOGGING+++");
_logger.LogWarning("+++TESTING WARNING LOGGING+++");
_logger.LogError("+++TESTING ERROR LOGGING+++");

最后两个正确打印到控制台,但没有

LogInformation
语句的痕迹。

现在,如果我将

host.json
文件中的阈值降低到
Information
(或更低),它就会开始按预期显示。但
local.settings.json
不应该优先于此吗?

我没有手动执行任何操作来添加这两个文件,但该功能正在使用

appsettings.json
和 Azure Key Vault(如果这可以产生任何效果)。设置如下:

程序.cs

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureAppConfiguration((hostContext, configBuilder) =>
    {
        configBuilder
            .AddJsonFile($"appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json",
                optional: false,
                reloadOnChange: true)
            .AddEnvironmentVariables()
            .AddAzureKeyVault(new Uri(configBuilder.Build()["KeyVaultUrl"]), new DefaultAzureCredential());
    })
    .ConfigureServices((hostContext, services) =>
    {
        ConfigureServices(services, hostContext.Configuration);
    })
    .Build();
c# logging azure-functions
1个回答
0
投票

事实证明,

host.json
并不是通过在
local.settings.json
中具有相同的值来简单地覆盖,至少在日志记录方面不是这样。

相反,你可以这样做,来覆盖它,但是如果你需要覆盖太多的值,这会有点麻烦:

{
    (...),
    "Values": {
        "AzureWebJobsStorage": "{storage-account-connection-string}",
        "FUNCTIONS_WORKER_RUNTIME": "{language-runtime}",
        "AzureFunctionsJobHost__logging__logLevel__default":"Debug" // This will override host.json
    }
}

有关更多信息,请参阅文档

© www.soinside.com 2019 - 2024. All rights reserved.