这可能是How to set Steeltoe Dynamic Logging works with 3rd party loggers as Serilog?的重演。我想利用Steeltoe动态日志记录配置(将帮助我动态调整日志级别而无需重新部署),并希望使我的日志语句更加结构化。所以我决定看看Serilog。这是我的代码
public class Program
{
/// <summary>
/// Application entry point.
/// </summary>
/// <param name="args">arguments.</param>
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
/// <summary>
///
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureAppConfiguration((hostContext, config) =>
{
config.AddCloudFoundry();
})
.ConfigureLogging((context, builder) =>
{
// We need to clear providers which are added by CreateDefaultBuilder().
// Please refer https://github.com/aspnet/Logging/issues/648. Otherwise log entries will be duplicated
// since AddDynamicConsole again add console logger
builder.ClearProviders();
if (context.HostingEnvironment.IsDevelopment())
{
builder.AddDebug();
}
builder.AddDynamicConsole();
})
.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(hostingContext.Configuration)
.Enrich.FromLogContext()
.WriteTo.Console(outputTemplate:
"[{Level:u3}] [{Properties}] {Message}{NewLine}{Exception}")
.WriteTo.Trace())
.Build();
}
但这不符合预期。在PCF应用程序管理器中,除了“默认”之外,我无法看到任何日志记录提供程序。如果我取消注释
UseSerilog()
他们也回来了。顺便说一下,我不想把自己限制在Serilog,这可能与NLog(听说它也支持结构化日志记录)有关吗?或者,欢迎任何其他结合结构化日志记录和动态日志记录配置的想法
Steeltoe现在通过Steeltoe.Extensions.Logging.SerilogDynamicLogger
的dev branch MyGet feed初步支持Serilog。源代码和单元测试在this repository,我们欢迎您的反馈!
使用新的NuGet包,您应该能够做到这一点
new WebHostBuilder()
.ConfigureLogging((builderContext, loggingBuilder) =>
{
loggingBuilder.AddConfiguration(builderContext.Configuration.GetSection("Logging"));
// Add Steeltoe Dynamic Serilog provider
loggingBuilder.AddSerilogDynamicConsole();
})
然后就像使用预先存在的动态记录器一样。