serilog-extensions-logging icon indicating copy to clipboard operation
serilog-extensions-logging copied to clipboard

Support `ValueTuple<string, object?>` as state of scope.

Open jimbojim1997 opened this issue 2 years ago • 1 comments
trafficstars

Change for issue #186.

When calling ILogger.BeginScope<TState>(TState state) TState can now be a ValueTuple<string, object?>, Item1 is used as the property name and Item2 is used as the property value.

using (_logger.BeginScope(("TransactionId", (object?)12345)) {
    _logger.LogInformation("Transaction completed in {DurationMs}ms...", 30);
}
// Example JSON output:
// {
//	"@t":"2020-10-29T19:05:56.4176816Z",
//	"@m":"Completed in 30ms...",
//	"@i":"51812baa",
//	"DurationMs":30,
//	"SourceContext":"SomeNamespace.SomeService",
//	"TransactionId": 12345
// }

jimbojim1997 avatar Nov 12 '23 23:11 jimbojim1997

Thanks @jimbojim1997! This looks good, even if the need for the second tuple member to be object? is a bit awkward, it's an improvement on the dictionary approach and we might open it up to arbitrary tuple types later on :+1:

8.0.0 is going out in the next ~24 hours so it's too late for this change to make that release, unfortunately. We'll be able to ship it shortly afterwards, though.

nblumhardt avatar Nov 14 '23 07:11 nblumhardt

I think the checks for OriginalFormatPropertyName can be eliminated on all of the paths except IEnumerable<...>, since only a specifically-typed state object will carry these. Given I dropped the ball on this PR over Christmas though, I'll merge now to avoid any busywork around conflicts etc. :-)

Sorry about the long delay @jimbojim1997, thanks for this!

nblumhardt avatar Mar 14 '24 22:03 nblumhardt