Skip to content

Default LogLevel from appsettings doesn't do anything #128949

@rwb196884

Description

@rwb196884

Description

Example program:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace ConsoleAppLogSpam
{
    internal class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("-- ConsoleAppLogSpam --");

            using (IHost host = Host.CreateDefaultBuilder(args)
                .ConfigureLogging((HostBuilderContext context, ILoggingBuilder cfg) =>
                {
                    cfg.ClearProviders();
                    cfg.AddConfiguration(context.Configuration.GetSection("Logging"));
                    cfg.AddDebug();
                }).
                ConfigureServices((HostBuilderContext context, IServiceCollection services) =>
                {
                    services.AddSingleton<Job>();
                })
                .Build()
                )
            {
                using (IServiceScope scope = host.Services.CreateScope())
                {
                    Job j = scope.ServiceProvider.GetRequiredService<Job>();
                    j.Do();

                    System.Console.WriteLine("That's all folks. Press <enter> to quit.");
                    System.Console.ReadLine();
                }
            }
            System.Console.WriteLine("Done.");
        }
    }

    class Job
    {
        private readonly ILogger<Job> _Logger;

        public Job(ILogger<Job> logger)
        {
            _Logger = logger;
        }

        public void Do()
        {
            _Logger.LogTrace("LogTrace");
            _Logger.LogDebug("LogDebug");
            _Logger.LogInformation("LogInformation");
            _Logger.LogWarning("LogWarning");
            _Logger.LogError("LogError");
            _Logger.LogCritical("LogCritical");
        }
    }
}

With appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning",
      "ConsoleAppLogSpam": "Warning"
    }
  }
}

Output is

ConsoleAppLogSpam.Job: Information: LogInformation
ConsoleAppLogSpam.Job: Warning: LogWarning
ConsoleAppLogSpam.Job: Error: LogError
ConsoleAppLogSpam.Job: Critical: LogCritical

The appsettings.json has no effect.

Reproduction Steps

As above.

Expected behavior

That logging output should change according to the settings.

Actual behavior

Logging output is always at Information level.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions