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 • 2 comments

Describe the bug

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"));
                    });
                    services.AddAutoMapper();

                    // register Safesforce configuration
                    var sfconfig = new SalesForceConfiguration();
                    configuration.Bind(nameof(SalesForceConfiguration), sfconfig);
                    services.AddSingleton(sfconfig);

                    // register Salesforce Data Context
                    services.AddSingleton<DataManager>();

                    // register repositories
                    services.AddSingleton<IAbsRepository, EfAbsRepository>();
                    services.AddSingleton<ISalesForceRepository, RestApiSalesForceRepository>();

                    // register engines
                    services.AddSingleton<ISalesForceEngine, DefaultSalesForceEngine>();
                    services.AddSingleton<IAbsEngine, DefaultAbsEngine>();

                    // register manager
                    services.AddSingleton<IProviderManager, DefaultProviderManager>();

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

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

        }`

zanate4019 avatar Dec 04 '18 22:12 zanate4019

Here is the loglevel currently configured on both my appsettings files:

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

Here is what gets logged. ARA object get logged at info like they should, but the EF logs should be at Error, but are also logging at info:

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