1

This is probably a repeat of How to set Steeltoe Dynamic Logging works with 3rd party loggers as Serilog?. I would like to leverage Steeltoe dynamic logging configuration(Will help me to adjust the log levels dynamically without a redeployment) and wanted to make my log statements more structured. So I decided to take a look at Serilog. Here is my code

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();
    } 

But this is not working as expected. In PCF Apps manager, I am not able to see any logging provider apart from "Default". If I uncomment

UseSerilog()

they are back as well. Btw, I don't want to restrict my self to Serilog and is this possible with NLog(heard that it also supports structured logging)? Or any other thoughts to combine structured logging and dynamic logging configuration are most welcome

Sajan
  • 135
  • 15
  • Possible duplicate of [How to set Steeltoe Dynamic Logging works with 3rd party loggers as Serilog?](https://stackoverflow.com/questions/52891369/how-to-set-steeltoe-dynamic-logging-works-with-3rd-party-loggers-as-serilog) – Tim Feb 25 '19 at 18:58
  • Yes, this is the same as the other question. In order for Steeltoe to dynamically change the log levels, it has to be possible in the given logging framework. Not only is that not currently supported in Serilog, the act of adding Serilog to an ASP.NET Core application breaks other loggers. see https://github.com/SteeltoeOSS/Management/issues/21 – Tim Feb 25 '19 at 19:01
  • I had a look into NLog as well. For NLog, log providers are appearing, but I am not able to control any levels. NLog still filters logs based on the configuration provided in nlog.config – Sajan Feb 26 '19 at 12:51
  • `In order for Steeltoe to dynamically change the log levels, it has to be possible in the given logging framework` Steeltoe provides a wrapper interface that goes around ILoggerProvider (IDynamicLoggerProvider) to manage log levels. The only implementation that exists today is for Microsoft's Console logger,which can be found here: https://github.com/SteeltoeOSS/Logging – Tim Feb 26 '19 at 15:16
  • FWIW, Serilog does provide dynamic level switching via the `LoggingLevelSwitch` class - https://nblumhardt.com/2014/10/dynamically-changing-the-serilog-level/ - though I'm not sure if it fits with what you need, here. – Nicholas Blumhardt Feb 26 '19 at 23:39

1 Answers1

1

Steeltoe now has preliminary support for Serilog via the Steeltoe.Extensions.Logging.SerilogDynamicLogger, available in the dev branch MyGet feed. The source code and unit tests are in this repository, we'd welcome your feedback!

With the new NuGet package, you should be able to do this

new WebHostBuilder()
  .ConfigureLogging((builderContext, loggingBuilder) =>
  {
    loggingBuilder.AddConfiguration(builderContext.Configuration.GetSection("Logging"));

    // Add Steeltoe Dynamic Serilog provider
    loggingBuilder.AddSerilogDynamicConsole();
  })

And then be on your way the same as you would with the pre-existing dynamic logger.

Tim
  • 2,587
  • 13
  • 18
  • Is this targeted for .net core 2.1 as well. Some how I could not get this myget package https://www.myget.org/feed/steeltoemaster/package/nuget/Steeltoe.Extensions.Logging.SerilogDynamicLogger working in my .net core 2.1 application since it is showing version conflicts. Unfortunately I could not use .net core 2.2 at the moment. – Sajan Jun 04 '19 at 15:20
  • This package targets netstandard2.0, which is expected to work on both of those framework targets – Tim Jun 04 '19 at 16:17
  • OK. Somehow I could not get this working. Btw, when this will be available in nuget – Sajan Jun 28 '19 at 07:37
  • A release candidate of this package is available now - https://www.nuget.org/packages/Steeltoe.Extensions.Logging.SerilogDynamicLogger/2.3.0-rc1 – Tim Jun 28 '19 at 12:39