Sajan
Sajan

Reputation: 155

Steeltoe dynamic logging configuration with Serilog(or any structured logging library)

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

Upvotes: 1

Views: 1382

Answers (1)

Tim
Tim

Reputation: 2707

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.

Upvotes: 1

Related Questions