我在Pivotal Cloud Foundry中有ASP.NET Core 2.1应用程序,我们希望能够在飞行中配置日志记录级别。作为logger提供商,我们正在使用Serilog。 Steeltoe Dynamic Logging是否有可能与第三方记录器一起正常工作?
这是我尝试过的:
在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>();
在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" ]
}
在appsettings.Development.json中:
"Logging": {
"LogLevel": {
"Default": "Debug"
}
}
我只在配置记录级别中得到这个:Configure logging levels screenshot
我究竟做错了什么?
Steeltoe.Extensions.Logging.DynamicLogger是围绕Microsoft.Extensions.Logging.Console.ConsoleLoggerProvider
的包装纸。如今,它适用于允许多个ILoggerProviders的Microsoft Logging系统。
Serilog将自己插入日志管道并且不允许其他ILoggerProviders,因此使用Steeltoe Management和DynamicLogger将其添加到应用程序将破坏Steeltoe在运行时更改日志级别的能力。
Andrew Lock用一些details on how Serilog plugs itself in写了一篇不错的帖子。
我在这里为Steeltoe添加了serilog支持:https://github.com/SteeltoeOSS/Logging/pull/4
如果它能解决您的问题,请告诉我。
我猜你可以尝试使用LoggerFactory而不是LoggingProvider,以便并行拥有多个可用的Logging提供程序,这样当你的Serilog按预期工作时,Steeltoe仍然可以包装ConsoleLoggerProvider ......
为什么不尝试使用本文作为示例的快速POC,(您只需添加Steel Toe和PCF平台的东西并尝试):
https://www.codeproject.com/Articles/1217036/Console-Logging-and-Reading-Excel-Files-with-NET-C
(This other issue with NLog与您在Steeltoe Github回购中的问题有关,它给了我这个想法,我想值得一试)
我希望它有所帮助!