-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add ToString override to CorrelatedLogScope (#110)
* Add ToStirng override to CorrelatedLogScope Output to the default Console logger uses `ToString` on scope items. * test: add coverage and simplify with concatenation --------- Co-authored-by: skwasjer <[email protected]>
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
test/Correlate.Core.Tests/Extensions/LoggerExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Correlate.Extensions; | ||
|
||
public sealed class LoggerExtensionsTests | ||
{ | ||
private readonly ILogger _logger; | ||
|
||
public LoggerExtensionsTests() | ||
{ | ||
_logger = Substitute.For<ILogger>(); | ||
} | ||
|
||
[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<string, object>(scopeKey, correlationId); | ||
|
||
Func<IReadOnlyList<KeyValuePair<string, object>>, bool> assertScope = kvps => | ||
{ | ||
kvps.Should() | ||
.ContainSingle() | ||
.Which.Should() | ||
.Be(expectedKvp); | ||
return true; | ||
}; | ||
|
||
// Act | ||
_logger.BeginCorrelatedScope(scopeKey, correlationId); | ||
|
||
// Assert | ||
_logger.Received(1).BeginScope(Arg.Is<IReadOnlyList<KeyValuePair<string, object>>>(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<object, bool> assertScope = formattable => | ||
{ | ||
formattable.ToString().Should().Be(expectedStr); | ||
return true; | ||
}; | ||
|
||
// Act | ||
_logger.BeginCorrelatedScope(scopeKey, correlationId); | ||
|
||
// Assert | ||
_logger.Received(1).BeginScope(Arg.Is<object>(e => assertScope(e))); | ||
} | ||
} |