How to set Steeltoe Dynamic Logging works with 3rd party loggers as Serilog?

我怕爱的太早我们不能终老 提交于 2019-12-10 19:00:49

问题


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?


回答1:


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.




回答2:


I added serilog support for Steeltoe here: https://github.com/SteeltoeOSS/Logging/pull/4

Let me know if it solves your problem.




回答3:


I guess that you could try to use LoggerFactory instead of LoggingProvider in order to have more than one available Logging provider in parallel so that Steeltoe can still wrap ConsoleLoggerProvider while your Serilog keeps working as expected...

Why don't you try a quick POC using this article as an exmaple, (you only have to Add Steeltoe and PCF platform stuff to it and try):

https://www.codeproject.com/Articles/1217036/Console-Logging-and-Reading-Excel-Files-with-NET-C

(This other issue with NLog linked to your issue in Steeltoe Github repo and it gave me the idea, I guess it worth a try)

I hope it helps!



来源:https://stackoverflow.com/questions/52891369/how-to-set-steeltoe-dynamic-logging-works-with-3rd-party-loggers-as-serilog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!