Configure Azure WebJob SDK logger filter

97 Views Asked by At

Background

Building a WebJob with .net core 7 and Webjob SDK, found there are too many prints from Azure.Core and tried to filter it, it work with code but failed with configure, would like to make the logging configurable via configure files/environment variable

What's worked

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

What's not work

Copied the appsettings below from some web app template and it is not working

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

Tried to use AddConfiguration but not sure am I doing the right thing.

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

Full code

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();
        }
    }
}
1

There are 1 best solutions below

2
Suresh Chikkam On BEST ANSWER

In your current configuration, the AddConfiguration method is used to load configuration settings, but it may not directly apply to the logging configuration.

  • To configure the logging with the desired filters, you can explicitly access the logging section from the configuration and apply filters.

appsettings.json:

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

Program.cs:

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, loggingBuilder) =>
        {
            // Load logging configuration from appsettings.json
            var loggingConfiguration = context.Configuration.GetSection("Logging");
            loggingBuilder.AddConfiguration(loggingConfiguration);

            // Add console logging
            loggingBuilder.AddConsole();
        });

        var host = builder.Build();
        using (host)
        {
            await host.RunAsync();
        }
    }
}
  • I've removed the explicit use of AddFilter. Instead, we rely on the configuration loaded via AddConfiguration to apply the filtering for "Azure.Core" based on the configuration present in the appsettings.json.