Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read logging configuration from appsettings.json in .net Core 2.2

Tags:

c#

.net

.net-core

In .net Core 2.1 it was done like this

var loggingConfig = configuration.GetSection("Logging");
loggerFactory.AddConsole(loggingConfig);

I have moved it to ConfigureServices and now I get

Error CS1503 Argument 2: cannot convert from 'Microsoft.Extensions.Configuration.IConfigurationSection' to 'System.Action Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions'

like image 832
Mirhat Avatar asked Jan 23 '19 06:01

Mirhat


1 Answers

Base on this you may have to change the way you are configuring your application:

var webHost = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            var env = hostingContext.HostingEnvironment;
            config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                  .AddJsonFile($"appsettings.{env.EnvironmentName}.json", 
                      optional: true, reloadOnChange: true);
            config.AddEnvironmentVariables();
        })
        .ConfigureLogging((hostingContext, logging) =>
        {
            logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
            logging.AddConsole();
            logging.AddDebug();
            logging.AddEventSourceLogger();
        })
        .UseStartup<Startup>()
        .Build();

    webHost.Run();

1 Microsoft ASP.NET Core 2.2 Logging documentation

like image 125
Mohammad Nikravesh Avatar answered Oct 07 '22 09:10

Mohammad Nikravesh