efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Generic Host and Entity Framework Core Logging Configuration

Open zanate4019 opened this issue 8 years ago • 1 comments

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

Found it...

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

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

zanate4019 avatar Dec 05 '18 20:12 zanate4019