Reputation: 13
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.
Upvotes: 1
Views: 637
Reputation: 11
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:
install-package Serilog.AspNetCore
install-package Steeltoe.Extensions.Logging.DynamicLogger
install-package Steeltoe.Extensions.Logging.DynamicSerilogCore
install-package Steeltoe.Management.TracingCore
// Add services to the container.
builder.AddDistributedTracincAspNetCore();
builder.AddDynamicSerilog((cfg, log) =>
log.ReadFrom.Configuration(cfg.Configuration));
builder.AddDynamicLogging();
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.
Upvotes: 1
Reputation: 456
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. 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.
Upvotes: 1