Skip to content
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

Added notes about Log Scope usage to README #182

Merged
merged 2 commits into from
Jan 8, 2021
Merged

Added notes about Log Scope usage to README #182

merged 2 commits into from
Jan 8, 2021

Conversation

Rabadash8820
Copy link
Contributor

The ability to pass any IEnumerable<KeyValuePair<string, object>> to ILogger.BeginScope is an extremely useful feature, but unfortunately it doesn't seem to be officially documented anywhere. I only discovered it while digging through this repo's source code! I assume this repo is the correct location for this information, as it only applies when one is using both Serilog and the Microsoft.Extensions.Logging library, but I could see where it might be better off in the Serilog wiki. Looking forward to your feedback!

The ability to pass any `IEnumerable<KeyValuePair<string, object>>` to `ILogger.BeginScope` is an _extremely_ useful feature, but unfortunately it doesn't seem to be officially documented anywhere. I only discovered it while digging through this repo's source code! I assume this repo is the correct location for this information, as it only applies when one is using both Serilog and the _Microsoft.Extensions.Logging_ library, but I could see where it might be better off in the Serilog wiki. Looking forward to your feedback!
README.md Outdated Show resolved Hide resolved
A far better alternative is to use the `BeginScope<TState>(TState state)` method. If you provide any `IEnumerable<KeyValuePair<string, object>>` to this method, then we will output the key/value pairs as structured properties _without_ the `Scope` property, as in this example:

```cs
var scopeProps = new Dictionary<string, object> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try

var scopeProps = new { TransactionId = 12345, ResponseJson = jsonString };

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh no I did not, good idea. Unfortunately, looks like anonymous types still generate a Scope property:

... Scope=["{ TransactionId = 12345, ResponseJson = "{\"Key1\":\"Value1\", \"Key2\":\"Value2\"}" }"]

@Rabadash8820
Copy link
Contributor Author

Rabadash8820 commented Jan 6, 2021

@sungam3r So is there any interest in merging this PR or nah?

@skomis-mm
Copy link

Looks good 👍

@nblumhardt nblumhardt merged commit 3930717 into serilog:dev Jan 8, 2021
@nblumhardt nblumhardt mentioned this pull request Nov 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants