配置 Azure WebJob SDK 记录器过滤器

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

背景

使用

.net core 7
Webjob SDK
构建 WebJob,发现来自
Azure.Core
的打印太多,并尝试对其进行过滤,它可以使用代码,但配置失败,希望通过配置文件/环境配置日志记录变量

有什么作用

builder.ConfigureLogging((context, b) =>
{
    b.AddConsole();
    b.AddFilter("Azure.Core", LogLevel.None); // this work
});

什么是不行的

从某些网络应用程序模板复制了以下应用程序设置,但它不起作用

    "Logging": {
        "LogLevel": {
          "Default": "None",
          "Azure.Core": "None"
        }
    },

尝试使用

AddConfiguration
但不确定我是否做对了。

builder.ConfigureLogging((context, b) =>
{
    b.AddConfiguration(context.Configuration); // this line
    b.AddConsole();
});

完整代码

class Program
{
    static async Task Main()
    {
        var builder = new HostBuilder();
        builder.ConfigureWebJobs(b =>
        {
            b.AddAzureStorageCoreServices();
            b.AddAzureStorageQueues();
        });
        builder.ConfigureAppConfiguration((context, configurationBuilder) =>
        {
            configurationBuilder
                .AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
        });
        builder.ConfigureLogging((context, b) =>
        {
            b.AddConfiguration(context.Configuration);
            b.AddConsole();
        });

        var host = builder.Build();
        using (host)
        {
            await host.RunAsync();
        }
    }
}
azure azure-webjobs azure-webjobssdk
1个回答
0
投票

在您当前的配置中,

AddConfiguration
方法用于加载配置设置,但它可能不会直接应用于日志记录配置。

  • 要使用所需的过滤器配置日志记录,您可以从配置中显式访问日志记录部分并应用过滤器。

appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Azure.Core": "None"
    }
  }
}

程序.cs:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        var builder = new HostBuilder();
        builder.ConfigureWebJobs(b =>
        {
            b.AddAzureStorageCoreServices();
            b.AddAzureStorageQueues();
        });
        builder.ConfigureAppConfiguration((context, configurationBuilder) =>
        {
            configurationBuilder
                .AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
        });
        builder.ConfigureLogging((context, b) =>
        {
            var loggingConfiguration = context.Configuration.GetSection("Logging");
            b.AddConfiguration(loggingConfiguration);
            b.AddConsole();
            b.AddFilter("Azure.Core", LogLevel.None); // Apply the filter after loading the configuration
        });

        var host = builder.Build();
        using (host)
        {
            await host.RunAsync();
        }
    }
}
  • AddConfiguration
    方法从appsettings.json文件中读取日志配置,随后的
    AddFilter
    方法将“Azure.Core”命名空间的过滤器设置为
    LogLevel.None
© www.soinside.com 2019 - 2024. All rights reserved.