I have ASP.NET Core 2.1 app in Pivotal Cloud Foundry where we want to be able to configure logging levels on fly. As logger provider we are using Serilog. Is it possible that Steeltoe Dynamic Logging works properly with 3rd party loggers and how?
Here is what I tried:
In Program.cs:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseCloudFoundryHosting()
.ConfigureLogging((builderContext, loggingBuilder) =>
{
loggingBuilder.AddDynamicConsole();
})
.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(hostingContext.Configuration))
.UseStartup<Startup>();
In appsettings.json
"Serilog": {
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {SourceContext}: {Properties} {NewLine} {EventId} {Message:lj}{NewLine}{Exception}"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
}
In appsettings.Development.json:
"Logging": {
"LogLevel": {
"Default": "Debug"
}
}
And I get only this in Configure Logging Levels: Configure logging levels screenshot
What am I doing wrong?
Steeltoe.Extensions.Logging.DynamicLogger is a wrapper around the Microsoft.Extensions.Logging.Console.ConsoleLoggerProvider
. As it exists today, it is intended for use with the Microsoft Logging system where multiple ILoggerProviders are allowed.
Serilog inserts itself in the logging pipeline and does not allow other ILoggerProviders, so adding it to an app with Steeltoe Management and DynamicLogger will break Steeltoe's ability to change log levels at runtime.
Andrew Lock wrote a nice post with some details on how Serilog plugs itself in.