-
Notifications
You must be signed in to change notification settings - Fork 105
Support ActivityTrackingOptions to enrich logs with Activity tags and baggage #245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @david-obee! I think this would be really interesting to explore. Is there a chance you can whip up a proof-of-concept pull request we can dig into further? |
Hi @nblumhardt, thanks for the quick response! I've created the PR #246 which demonstrates the original issue, then tests and implements this. As mentioned I've not contributed to Serilog before, so let me know if there's anything I'm missing and I can fix it up! |
Merged; currently in the 8.0.1-dev-* series. |
Hi @nblumhardt, a significant caveat I've only just realized (apologies!). This 'fix' only works if you use the Serilog What I didn't realize sooner is that typically Serilog users are not using the The issue is that in my experiment (the new sample service) where I tested this change, I was testing it using the So, this change could still be useful for people just using Serilog as a |
Hi David, thanks for looping back. Yes, sorry, I should have mentioned this caveat, too. It would be great if, at some stage, we can figure out how to get external scope providers into |
Hi, I'm raising this initially just as an issue, to check my thinking and see if there would be interest in this feature. If so, then I can potentially have a go at fully implementing this and raising a PR.
Is your feature request related to a problem? Please describe.
The
Microsoft.Extensions.Logging
library supports automatically enriching your logs with tags and/or baggage from the associatedSystem.Diagnostics
Activity
at the time of logging. This is enabled by setting theActivityTrackingOptions
setting to eitherTags
orBaggage
(or both). This is as introduced in this PR dotnet/runtime#48722 which comes off the back of this issue dotnet/runtime#46571.I've found that setting this property doesn't seem to work when I'm using Serilog behind the
Microsoft.Extensions.Logging
API. Looking into it, I believe the reason is this line here in theMicrosoft.Extensions.Logging
library:https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.Logging/src/LoggerFactory.cs#L204
Here, the
LoggerFactoryScopeProvider
is what does the work of pulling out the tags and baggage defined on theActivity
and making available for logging scopes. If theILoggerProvider
supports external scopes, by implementingISupportExternalScope
, then it can receive thisLoggerFactoryScopeProvider
to use later to enrich its logs.Unfortunately, it seems that the
SerilogLoggerProvider
doesn't implement thatISupportExternalScope
interface (https://github.com/serilog/serilog-extensions-logging/blob/main/src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerProvider.cs), and hence misses out on this feature.Describe the solution you'd like
Ideally, Serilog could support these
Tags
andBaggage
switches on theActivityTrackingOptions
setting, so that logs can be enriched with tags and baggage from the ongoingActivity
.I don't have much deep experience with Serilog, so I might be a bit naïve here, but I think this might be a relatively straightforward addition to implement the
ISupportExternalScope
interface.I think we can modify the
Enrich
method inSerilogLoggerProvider
, with something like (this is a quick sketch of a solution):where we implement the
ISupportExternalScope.SetScopeProvider(IExternalScopeProvider scopeProvider)
method by just storing theIExternalScopeProvider
as the field_externalScopeProvider
like:Describe alternatives you've considered
An alternative is to manually define a Serilog enricher, to do the same thing, pulling out tags and baggage and enriching the logs. This is the approach taken by in this blog post: https://www.jimmybogard.com/increasing-trace-cardinality-with-tags-and-baggage/.
However, it would be great if Serilog could support all the available options on the
ActivityTrackingOptions
setting, and then allow us to rely on Microsoft's implementation for doing this.The text was updated successfully, but these errors were encountered: