From 2d1b099b0276cecea5b5ddd2cc2a0c62d64d34a2 Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 6 Sep 2024 11:25:59 +0100 Subject: [PATCH 1/2] Add ToStirng override to CorrelatedLogScope Output to the default Console logger uses `ToString` on scope items. --- src/Correlate.Core/Extensions/LoggerExtensions.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Correlate.Core/Extensions/LoggerExtensions.cs b/src/Correlate.Core/Extensions/LoggerExtensions.cs index 66e2443..16c165f 100644 --- a/src/Correlate.Core/Extensions/LoggerExtensions.cs +++ b/src/Correlate.Core/Extensions/LoggerExtensions.cs @@ -49,5 +49,14 @@ public KeyValuePair this[int index] throw new ArgumentOutOfRangeException(nameof(index)); } } + + /// + /// Returns a representation of the scope items as a list of comma-separated items, + /// matching the default representation provided by the standard Console logger. + /// + public override string ToString() + { + return string.Join(", ", this.Select(kv => $"{kv.Key}:{kv.Value}")); + } } } From 439fddffc0ce2667ab1685ea63c232a730493d9e Mon Sep 17 00:00:00 2001 From: skwasjer <11424653+skwasjer@users.noreply.github.com> Date: Sun, 8 Sep 2024 16:49:12 +0200 Subject: [PATCH 2/2] test: add coverage and simplify with concatenation --- .../Extensions/LoggerExtensions.cs | 2 +- .../Extensions/LoggerExtensionsTests.cs | 64 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 test/Correlate.Core.Tests/Extensions/LoggerExtensionsTests.cs diff --git a/src/Correlate.Core/Extensions/LoggerExtensions.cs b/src/Correlate.Core/Extensions/LoggerExtensions.cs index 16c165f..df1fd90 100644 --- a/src/Correlate.Core/Extensions/LoggerExtensions.cs +++ b/src/Correlate.Core/Extensions/LoggerExtensions.cs @@ -56,7 +56,7 @@ public KeyValuePair this[int index] /// public override string ToString() { - return string.Join(", ", this.Select(kv => $"{kv.Key}:{kv.Value}")); + return _scopeKey + ':' + _correlationId; } } } diff --git a/test/Correlate.Core.Tests/Extensions/LoggerExtensionsTests.cs b/test/Correlate.Core.Tests/Extensions/LoggerExtensionsTests.cs new file mode 100644 index 0000000..488b888 --- /dev/null +++ b/test/Correlate.Core.Tests/Extensions/LoggerExtensionsTests.cs @@ -0,0 +1,64 @@ +using Microsoft.Extensions.Logging; + +namespace Correlate.Extensions; + +public sealed class LoggerExtensionsTests +{ + private readonly ILogger _logger; + + public LoggerExtensionsTests() + { + _logger = Substitute.For(); + } + + [Theory] + [InlineData(CorrelateConstants.CorrelationIdKey, "12345")] + [InlineData("CustomKey", "abcdef")] + public void When_beginning_scope_it_should_return_disposable_scope_implementing_kvp_list_containing_the_correlation_id + ( + string scopeKey, + string correlationId + ) + { + var expectedKvp = new KeyValuePair(scopeKey, correlationId); + + Func>, bool> assertScope = kvps => + { + kvps.Should() + .ContainSingle() + .Which.Should() + .Be(expectedKvp); + return true; + }; + + // Act + _logger.BeginCorrelatedScope(scopeKey, correlationId); + + // Assert + _logger.Received(1).BeginScope(Arg.Is>>(e => assertScope(e))); + } + + [Theory] + [InlineData(CorrelateConstants.CorrelationIdKey, "12345")] + [InlineData("CustomKey", "abcdef")] + public void When_formatting_scope_it_should_return_expected + ( + string scopeKey, + string correlationId + ) + { + string expectedStr = $"{scopeKey}:{correlationId}"; + + Func assertScope = formattable => + { + formattable.ToString().Should().Be(expectedStr); + return true; + }; + + // Act + _logger.BeginCorrelatedScope(scopeKey, correlationId); + + // Assert + _logger.Received(1).BeginScope(Arg.Is(e => assertScope(e))); + } +}