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://docs.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 ();
}