3

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?

Daniel Mikusa
  • 13,716
  • 1
  • 22
  • 28
TCO
  • 33
  • 5

4 Answers4

2

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

Let me know if it solves your problem.

Hananiel
  • 421
  • 2
  • 9
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.

Tim
  • 2,587
  • 13
  • 18
  • Thank you for your answer, Tim – TCO Oct 25 '18 at 12:19
  • We don't have active plans to work on this type of compatibility, but I did add an issue to the Management repo to track this item (https://github.com/SteeltoeOSS/Management/issues/21) if you wish to further discuss, follow progress or make suggestions there – Tim Oct 25 '18 at 14:29
  • 4
    Tim, Is there some other 3rd party logging provider that can works properly with Steeltoe Dynamic Logging? We need semantic logging and steeltoe dynamic levels at the same time. – TCO Nov 22 '18 at 09:30
  • Probably not, though I don't think we've spent significant time on the effort. If there are other tools you've looked at, I could give some thought to what it would take to make Steeltoe work with them – Tim Nov 26 '18 at 13:51
1

I've been working for hours on the same problem now: Register Serilog as well as other Log Providers (Nlog in my case).

Beside the default behaviour of Serilog (replacing all other Log Providers) Serilog provides a way to register other LogProviders via it's Api.

Code to create a Serilog logger and register NLog beside Serilog as Log Provider via Serilog Api looks like this in my case:

// Configure nlog.
NLogBuilder.ConfigureNLog("nlog.config");

// Create an ILoggerProvider instance (needed to pass it to Serilog Api).
var nlogLoggerProvider = new NLogLoggerProvider();

// Create LoggerProviderCollection because this data type has to be passed to Serilog Api-Call.
var loggerProviderCollection = new LoggerProviderCollection();
loggerProviderCollection.Add(nlogLoggerProvider);

// Create Serilog Logger and call WriteTo.Providers() in order to register other Log Providers.
var logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .MinimumLevel.Debug()
    .Enrich.FromLogContext()

    // This way you can register other LogProviders beside Serilog.
    .WriteTo.Providers(loggerProviderCollection)

    .CreateLogger();

What you need to register other Log Providers when using Serilog is an object of your Log Provider that implements ILoggerProvider interface. You can pass a collection of objects that implement ILoggerProvider to your Serilog Logger and they should work (it works at least when I mix NLog and Serilog).

tpeter1985
  • 11
  • 1
0

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!

Juan
  • 2,156
  • 18
  • 26