-
Notifications
You must be signed in to change notification settings - Fork 104
Support ValueTuple<string, object?> as state of scope.
#232
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
322c5f2
SerilogLoggerScope add support for ValueTuple<string, object?> state.…
jimbojim1997 d8637c2
Update readme with exaple of BeginScope with a ValueTuple.
jimbojim1997 85172ad
Changed copyright header from .NET Foundation to Serilog Contributors.
jimbojim1997 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
100 changes: 100 additions & 0 deletions
100
test/Serilog.Extensions.Logging.Tests/SerilogLoggerScopeTests.cs
This file contains hidden or 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,100 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using Serilog.Events; | ||
| using Serilog.Extensions.Logging.Tests.Support; | ||
|
|
||
| using Xunit; | ||
|
|
||
| namespace Serilog.Extensions.Logging.Tests; | ||
| public class SerilogLoggerScopeTests | ||
| { | ||
| static (SerilogLoggerProvider, LogEventPropertyFactory, LogEvent) SetUp() | ||
| { | ||
| var loggerProvider = new SerilogLoggerProvider(); | ||
|
|
||
| var logEventPropertyFactory = new LogEventPropertyFactory(); | ||
|
|
||
| var dateTimeOffset = new DateTimeOffset(2000, 1, 1, 0, 0, 0, TimeSpan.Zero); | ||
| var messageTemplate = new MessageTemplate(Enumerable.Empty<Parsing.MessageTemplateToken>()); | ||
| var properties = Enumerable.Empty<LogEventProperty>(); | ||
| var logEvent = new LogEvent(dateTimeOffset, LogEventLevel.Information, null, messageTemplate, properties); | ||
|
|
||
| return (loggerProvider, logEventPropertyFactory, logEvent); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void EnrichWithDictionaryStringObject() | ||
| { | ||
| const string propertyName = "Foo"; | ||
| const string expectedValue = "Bar"; | ||
|
|
||
| var(loggerProvider, logEventPropertyFactory, logEvent) = SetUp(); | ||
|
|
||
|
|
||
| var state = new Dictionary<string, object?>() { { propertyName, expectedValue } }; | ||
|
|
||
| var loggerScope = new SerilogLoggerScope(loggerProvider, state); | ||
|
|
||
| loggerScope.EnrichAndCreateScopeItem(logEvent, logEventPropertyFactory, out LogEventPropertyValue? scopeItem); | ||
|
|
||
| Assert.Contains(propertyName, logEvent.Properties); | ||
|
|
||
| var scalarValue = logEvent.Properties[propertyName] as ScalarValue; | ||
| Assert.NotNull(scalarValue); | ||
|
|
||
| var actualValue = scalarValue.Value as string; | ||
| Assert.NotNull(actualValue); | ||
| Assert.Equal(expectedValue, actualValue); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void EnrichWithIEnumerableKeyValuePairStringObject() | ||
| { | ||
| const string propertyName = "Foo"; | ||
| const string expectedValue = "Bar"; | ||
|
|
||
| var (loggerProvider, logEventPropertyFactory, logEvent) = SetUp(); | ||
|
|
||
|
|
||
| var state = new KeyValuePair<string, object?>[] { new KeyValuePair<string, object?>(propertyName, expectedValue) }; | ||
|
|
||
| var loggerScope = new SerilogLoggerScope(loggerProvider, state); | ||
|
|
||
| loggerScope.EnrichAndCreateScopeItem(logEvent, logEventPropertyFactory, out LogEventPropertyValue? scopeItem); | ||
|
|
||
| Assert.Contains(propertyName, logEvent.Properties); | ||
|
|
||
| var scalarValue = logEvent.Properties[propertyName] as ScalarValue; | ||
| Assert.NotNull(scalarValue); | ||
|
|
||
| var actualValue = scalarValue.Value as string; | ||
| Assert.NotNull(actualValue); | ||
| Assert.Equal(expectedValue, actualValue); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void EnrichWithTupleStringObject() | ||
| { | ||
| const string propertyName = "Foo"; | ||
| const string expectedValue = "Bar"; | ||
|
|
||
| var (loggerProvider, logEventPropertyFactory, logEvent) = SetUp(); | ||
|
|
||
|
|
||
| var state = (propertyName, (object)expectedValue); | ||
|
|
||
| var loggerScope = new SerilogLoggerScope(loggerProvider, state); | ||
|
|
||
| loggerScope.EnrichAndCreateScopeItem(logEvent, logEventPropertyFactory, out LogEventPropertyValue? scopeItem); | ||
|
|
||
| Assert.Contains(propertyName, logEvent.Properties); | ||
|
|
||
| var scalarValue = logEvent.Properties[propertyName] as ScalarValue; | ||
| Assert.NotNull(scalarValue); | ||
|
|
||
| var actualValue = scalarValue.Value as string; | ||
| Assert.NotNull(actualValue); | ||
| Assert.Equal(expectedValue, actualValue); | ||
| } | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
test/Serilog.Extensions.Logging.Tests/Support/LogEventPropertyFactory.cs
This file contains hidden or 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,15 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using Serilog.Core; | ||
| using Serilog.Events; | ||
|
|
||
| namespace Serilog.Extensions.Logging.Tests.Support; | ||
| internal class LogEventPropertyFactory : ILogEventPropertyFactory | ||
| { | ||
| public LogEventProperty CreateProperty(string name, object? value, bool destructureObjects = false) | ||
| { | ||
| var scalarValue = new ScalarValue(value); | ||
| return new LogEventProperty(name, scalarValue); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the .NET Foundation copyright header intended here, and in the other test file below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what it needs to be so just copied it from another file.
I've had another look and there's a mix of files with this header,
Copyright [year] Serilog Contributors, and no header. What should I change this and the other file to?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Standard header as per serilog core without a year in all source files
Optional in test files
(I've yet to do the PR to make it completely consistent in core)
serilog/serilog#1969 (comment)