1

I am trying to setup Serilog Dynamic Logging with Steeltoe. I am using .NET 7. I am using the Host Builder to add the functionality. Code example below using Host builder:

.AddDynamicSerilog((cfg, log) => log.ReadFrom.Configuration(cfg.Configuration))

I have set a breakpoint in the extension here but my code is not hitting this breakpoint:

public static IHostBuilder AddDynamicSerilog(
    this IHostBuilder hostBuilder,
    Action<HostBuilderContext, LoggerConfiguration> configureLogger = null,
    bool preserveStaticLogger = false,
    bool preserveDefaultConsole = false)

Steeltoe Configuration:

"Serilog": {
    "MinimumLevel": "Information",
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
    "WriteTo": [
      {
        "Name": "Console"
      },
      {
        "Name": "File",
        "Args": { "path": "Logs/log.txt" }
      }
    ],
    "Properties": {
      "Application": "Test"
    }
  }

My application is logging when i use the logger but it is logging in just the regular format no application name or tracing extensions etc. so I am assuming my configuration is not being read somehow? Is the Serilog Dynamic Logging package compatible with .NET 7? Or is there something else going on?

Tried given examples. Logging does not look like Serilog configuration is being read or applied from settings file. Also extension code isn't executed.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Ben Pierce
  • 11
  • 2

2 Answers2

1

You can try this particular sample here: https://github.com/SteeltoeOSS/Samples/tree/main/Management/src/CloudFoundry

Change the target to <TargetFramework>net7.0</TargetFramework>

I just tried this and you can see in the picture the Serilog configuration being read. Serilog Debug view Note the versions of Steeltoe etc in the sample (to help narrow down the issue).

As far as debugging - we enable sourcelink on our nuggets so you should be able to view and debug Steeltoe code by enabling it in your debug options. debug options

Hananiel
  • 421
  • 2
  • 9
  • I copied the example code exactly and made sure I have Source Link option turned on. However I am still not hitting my break point in the extension. Shouldn't that at least happen? – Ben Pierce Jan 19 '23 at 20:10
  • I also verified that I am using Steeltoe.Extensions.Logging.DynamicSerilog.Core version 3.2.2 – Ben Pierce Jan 19 '23 at 20:12
  • using var host = Host.CreateDefaultBuilder() .ConfigureAppConfiguration((hostingContext, configuration) => { configuration.Sources.Clear(); var configurationRoot = configuration.Build(); }) .AddDynamicSerilog((cfg, log) => log.ReadFrom.Configuration(cfg.Configuration)) .AddDistributedTracingAspNetCore() .Build(); – Ben Pierce Jan 19 '23 at 20:12
  • Code from my Host Builder. I noticed that the difference is I am using the new Host not WebHost like the sample? Could that be causing the issue? Isn't it recommended to use Host now and no longer use WebHost? – Ben Pierce Jan 19 '23 at 20:14
  • @BenPierce you may also need to disable "Just My Code" and/or enable the NuGet.org symbol server. It might be worth a quick read through [this post](https://devblogs.microsoft.com/dotnet/improving-debug-time-productivity-with-source-link/#enabling-source-link) to check if anything is off with your setup – Tim Jan 24 '23 at 13:22
  • The extensions for [`HostBuilder`](https://github.com/SteeltoeOSS/Steeltoe/blob/release/3.2/src/Logging/src/DynamicSerilogBase/SerilogHostBuilderExtensions.cs#L13) and [`WebHostBuilder`](https://github.com/SteeltoeOSS/Steeltoe/blob/release/3.2/src/Logging/src/DynamicSerilogCore/SerilogWebHostBuilderExtensions.cs#L13) should be effectively identical, but there do appear to be some differences, so it may be worth trying `Host.ConfigureWebHost(builder => builder.AddDynamicSerilog((cfg, log) => log.ReadFrom.Configuration(cfg.Configuration)))` for a quick answer as to whether the results change – Tim Jan 24 '23 at 13:35
1

I suspect the problem is that outputTemplate is missing in appsettings. It needs to include "{Properties}", so that scoped values are included in the message.

  "Serilog": {
    "MinimumLevel": "Information",
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {SourceContext}: {Properties} {NewLine} {EventId} {Message:lj}{NewLine}{Exception}"
        }
      },
      {
        "Name": "File",
        "Args": { "path": "Logs/log.txt" }
      }
    ],
    "Properties": {
      "Application": "Test"
    }
  }

I took the following steps to reproduce the issue and make it work:

  1. In Visual Studio: File > New > Project > ASP.NET Core Web API
  2. From the Package Manager Console:
    install-package Serilog.AspNetCore
    install-package Steeltoe.Extensions.Logging.DynamicLogger
    install-package Steeltoe.Extensions.Logging.DynamicSerilogCore
    install-package Steeltoe.Management.TracingCore
    
  3. Add to Program.cs:
    // Add services to the container.
    
    builder.AddDistributedTracincAspNetCore();
    builder.AddDynamicSerilog((cfg, log) =>
        log.ReadFrom.Configuration(cfg.Configuration));
    builder.AddDynamicLogging();
    
  4. Add the section from above to appsettings.Development.json
  5. Run the application

This prints the following line on my console:

[14:13:08 INF] Microsoft.AspNetCore.Hosting.Diagnostics: {Protocol="HTTP/2", Method="GET", ContentType=null, ContentLength=null, Scheme="https", Host="localhost:7142", PathBase="", Path="/weatherforecast", QueryString="", RequestId="0HMNU1ISSN7T6:00000001", RequestPath="/weatherforecast", ConnectionId="0HMNU1ISSN7T6", Scope=[" [DynamicLoggingWebApi,52a92b64eb0209466ca872311bf309cc,e425a4b06ed50908,0000000000000000,true] "], Application="Test"}
 { Id: 1 } Request starting HTTP/2 GET https://localhost:7142/weatherforecast - -

From the line above, the tracing info is:

[DynamicLoggingWebApi,52a92b64eb0209466ca872311bf309cc,e425a4b06ed50908,0000000000000000,true]

Hope that helps!

By the way, I didn't have any problems stepping into the sources. To set up Visual Studio, follow the instructions at https://devblogs.microsoft.com/dotnet/improving-debug-time-productivity-with-source-link/#enabling-source-link.

  • Thank you for responding. I got a little further with your suggestions. I did all of the steps. The only exception is my build code looks like this – Ben Pierce Jan 25 '23 at 17:58
  • var host = BuildConfiguration(); var builder = WebApplication.CreateBuilder() .AddDistributedTracincAspNetCore() .AddDynamicSerilog((cfg, log) => log.ReadFrom.Configuration(cfg.Configuration)) .AddDynamicLogging() .AddAllActuators(); – Ben Pierce Jan 25 '23 at 18:05
  • I am now getting file logs with no additional tracing information added so I know that my configuration files are now being read? Do I need to add any management tracing settings to get this to work? I do not have any right now in my config file. – Ben Pierce Jan 25 '23 at 18:06
  • Also do I still need to add a Logging section in my config? I wouldn't think so now that Serilog is there but I see lots of examples where they are using both. Yours did not have it and I think that is correct. – Ben Pierce Jan 25 '23 at 18:14
  • I just found the problem. outputTemplate goes under the args section. I had it underneath that. Minor issue but what pain it caused! Thank you everyone for the help. – Ben Pierce Jan 25 '23 at 20:36
  • Does any one know if there is online documentation of the Serilog config file settings. Lots of examples show how to do it in code but very little on format when using configuration files. – Ben Pierce Jan 26 '23 at 16:17
  • Also change writeto value Console to Debug when using latest version of Webapi there is no more console as far as I can tell? – Ben Pierce Jan 26 '23 at 16:56