Azure 函数 iLogger isEnabled 未启动 host.json

Azure function iLogger isEnabled is not picking up host.json

我试图避免在 Azure 函数中未启用调试级别时执行 log.debug。当我执行下面的代码时,控件进入 IF 块。请解释为什么无论 host.json 文件中的配置如何,都会执行此操作。

我尝试使用 ILogger logger2 创建另一个对象,但还是一样。

[FunctionName("Function3")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        log.LogCritical("*****From Function3******");


        log.LogTrace("LogTrace");
        if (log.IsEnabled(LogLevel.Debug))
        {
            //Control should not come inside this as the debug level is not enabled 

            log.LogDebug("Debug message inside debug level");//Not printed as expected.
            log.LogInformation("Inside Debug level"); //Not printed as expected
            log.LogError("LogError Inside Debug level"); //Printed because of the setting in host.json
        }
        log.LogDebug("LogDebug");
        log.LogInformation("LogInformation Blue");
        log.LogWarning("LogWarning Yellow");
        log.LogError("LogError Red");
        log.LogCritical("LogCritical White");

        return new OkObjectResult("Okay!");
    }

Host.JSON

{
  "version": "2.0",
  "logging": {
    "LogLevel": {
      "Default": "Warning",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Warning",
      "Function.Function3.User": "Error",
      "Function.PaymentFeeder.User": "Warning",
      "Function": "Warning"

    },
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  }
}

更新: 使用 Visual Studio 2022 和 targeting.Net 6 包括截图

引用的包:

<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
  <PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.30" />
  <PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="4.5.0" />
  <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />

谢谢

我们检查您在 host.json 函数 .

中使用的相同内容

我们遵循相同的方法,这里是解决方法:

如果您想在未启用调试级别时避免执行 log.debug,请执行以下操作:

如果我们想避免针对特定功能,您可以这样提及 "Function.Function1.User": "Error" 用于特定功能使用特定日志.这里的示例 对于 Funciton1,我们启用了错误日志,所以这里 不会将 进入 if 块 if (log.IsEnabled(LogLevel.Debug))

这是使用调试级别登录Function1

的解决方法

这里提到Function1应该useDebug log so 会进入if块 if (log.IsEnabled(LogLevel.Debug))

参考此 MS Doc 了解有关 LogLevel 配置的更多信息

更新:

对函数 1

使用 错误 loglevel

使用 调试 logLevel 函数 1