6

We are trying to configure logging for our ASP.NET Core application via a configuration file (the default appsettings.json). The docu says it should be possible (https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1), but we can't get it working like expected.

What we have tried so far:

  • leave the code as it is in the project template for ASP.NET Core and just change the appsettings.json => no effect
  • explicitly add appsettings.json as config file and configure logging in Program.cs => no logging at all
    public static IHostBuilder CreateHostBuilder (string[] args) =>
        Host.CreateDefaultBuilder (args)
            .ConfigureAppConfiguration ((hostingContext, config) =>
            {
              config.Sources.Clear ();

              var env = hostingContext.HostingEnvironment;

              config.AddJsonFile ("appsettings.json", optional: false, reloadOnChange: true)
                    .AddJsonFile ($"appsettings.{env.EnvironmentName}.json",
                        optional: true, reloadOnChange: true);

              config.AddEnvironmentVariables ();
            })
            .ConfigureLogging ((hostingContext, logging) =>
            {
              logging.ClearProviders ();

              logging.AddConfiguration (hostingContext.Configuration.GetSection ("Logging"));
            })
            .ConfigureWebHostDefaults (webBuilder =>
             {
               webBuilder.UseStartup<Startup> ();
             });
  • ... configure logging in Startup.ConfigureServices => no effect
    public void ConfigureServices (IServiceCollection services)
    {
      services.AddLogging (config => config.AddConfiguration (Configuration.GetSection("Logging")));
      services.AddControllers ();
    }

1 Answer 1

9

You misunderstood. You can configure logging, i.e. log levels for each provider, filtering out messages, etc., but the actual providers must be assigned in code. In other words, you can configure what messages go to the console via config, but you must still call logging.AddConsole() within ConfigureLogging to add that provider.

The code you have clears all the default providers and adds nothing, so there's no providers at all and hence no logs are generated. You cannot actually add the providers via config, just configure providers that have been added.

Sign up to request clarification or add additional context in comments.

2 Comments

This is seems to be correct, but I find it really counter-intuitive that you can't add a log provider through configuration. The way it is, you have to hard-code all the providers you MAY want to use, but then disable (set log-level to none) those you are currently not using. Also, the documentation makes it sound like it should work to add a provider either through code OR config. @Chris: Do you have a source where its mentioned that providers MUST be added through code?
@Rev No source, no, but it's dictated by the way Microsoft.Extensions.Logging works. Providers implement an interface, and dependency injection is use to bring in the implementation. The necessitates that they be registered with the service collection, which means you have to declare them in code. You can use config values to determine whether they're actually registered or not, if you wanted, using a conditional and your own custom config values, but you still have to mention them in the code, even if they aren't being utilized at runtime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.