aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Generic Host and Entity Framework Logging Configuration

Open zanate4019 opened this issue 8 years ago • 4 comments

I'm not sure if this is the proper place to log the issue...

I'm using the generic host builder to construct a host for my local service. Logging is configured with a logging level of Error across the board. This is working except in EF Core which is still logging at the info level. I suspect that EF is using a separate logger. Is there something different I need to do to tell EF to use the same log configuration as the rest of the application. Below is the code for my host:

        public static async Task Main(string[] args)
        {
            var host = new HostBuilder()
                .ConfigureHostConfiguration(configHost =>
                {
                    configHost.SetBasePath(Directory.GetCurrentDirectory());
                    configHost.AddJsonFile("hostsettings.json", optional: true);
                    configHost.AddEnvironmentVariables(prefix: "PREFIX_");
                    configHost.AddCommandLine(args);
                })
                .ConfigureAppConfiguration((hostContext, configApp) =>
                {
                    configApp.AddJsonFile("appsettings.json", optional: true);
                    configApp.AddJsonFile(
                        $"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json",
                        optional: true);
                    configApp.AddCommandLine(args);
                })
                .ConfigureServices((hostContext, services) =>
                {
                    // register ABS data context
                    var configuration = hostContext.Configuration;
                    services.AddDbContext<AbsContext>(options =>
                    {
                        options.UseSqlServer(configuration.GetConnectionString("AbsConnection"));
                    });
                   
        <--- omitted for brevity --->

                    // register hosted applications
                    services.AddHostedService<ProviderUpdateService>();
                })
                .ConfigureLogging((hostContext, configLogging) =>
                {
                    configLogging.AddConsole();
                    configLogging.AddDebug();
                    configLogging.AddEventLog();
                })
                .Build();

            await host.StartAsync();
            await host.StopAsync();
            host.Dispose();

        }

Here is the logging level configuration from both of my appsettings.json files:

{
  "Logging": {
    "LogLevel": {
      "ARA":  "Information",
      "Default": "Error",
      "System": "Error",
      "Microsoft": "Error"
    }
  }
}

Finally here is what is getting logged. Notice ARA is logging at info level like it should, but EF is also logging at info when it should be logging at error: image

zanate4019 avatar Dec 05 '18 17:12 zanate4019

Firstly, make sure the appsettings are being picked up.

But if you want to test log levels pass these settings into AddConsole

var settings = new ConsoleLoggerSettings();
settings.Switches.Add("Microsoft", LogLevel.Error);
settings.Switches.Add("Default", LogLevel.Information);

dcarr42 avatar Dec 05 '18 18:12 dcarr42

@dallancarr Thanks. I have been able to verify that my appsettings are being picked up. I'll try your suggesting about configuring within the AddConsole() call to see if the behavior is the same.

A little later... It looks like the AddConsole() method above is an extension of ILoggingBuilder and is not the same implementation of the AddConsole() on LogFactory nor does it expose the LogFactory. I'm thinking there may be an easier solution.

zanate4019 avatar Dec 05 '18 19:12 zanate4019

Found it...

needed to add builder.AddConfiguration(context.Configuration.GetSection("Logging")); to my ConfigureLogging() call.

Looks like the sample project for Generic Host needs it too...

zanate4019 avatar Dec 05 '18 20:12 zanate4019

There is the culprit

https://github.com/aspnet/Logging/blob/master/src/Microsoft.Extensions.Logging/LoggingServiceCollectionExtensions.cs#L45

dcarr42 avatar Dec 05 '18 21:12 dcarr42