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

Documentation: Pushing properties to logevents #212

Closed
svrooij opened this issue Oct 9, 2020 · 3 comments · Fixed by #217
Closed

Documentation: Pushing properties to logevents #212

svrooij opened this issue Oct 9, 2020 · 3 comments · Fixed by #217

Comments

@svrooij
Copy link
Contributor

svrooij commented Oct 9, 2020

This integration is great, it enables developers to just use the default ILogger in dotnet and configure the logging with serilog. I was however missing the following information, as found here

It would be great to have this added to the main readme of this module.

1. If the state argument is IEnumerable<KeyValuePair<string, object>>, the values are attached to the event directly
This may not be entirely intuitive, if you think of Scope as a trail of breadcrumbs. If you’ve dealt with correlating log events in a distributed application, you’ll recognize that often you’re interested in log events related to a specific transaction, customer, request, thread, machine or endpoint.

In the absence of any other API for the purpose, BeginScope() is how these correlation ids need to be attached in Microsoft.Extensions.Logging:

using (logger.BeginScope(new Dictionary<string, object>{
    ["CustomerId"] = 12345,
    ["OrderId"] = 54
}))
{
    logger.LogInformation("Processing credit card payment");
}
In this case, events created in the scope will have two additional properties attached: CustomerId and OrderId.

Types that implement IEnumerable<KeyValuePair<string, object>> rarely have any other useful representation; ToString() on a dictionary generally just provides the type name. These values therefore don’t result in a breadcrumb-like Scope property.

Something like:

Pushing properties to the logger

When using the ILogger from Microsoft.Extensions.Logging you cannot push properties like you would with the ILogger from Serilog. If you still want to push properties you can do with the following code:

using (logger.BeginScope(new Dictionary<string, object>
            {
                ["UserId"] = "xxx",
                ["ExtraProperty"] = "yyy",
            }))
{
   // UserId and ExtraProperty are set for all logging events in these brackets
}

The above code results in the same as if using the regular serilog ILogger with this code

using (logger.PushProperty("UserId", "xxx"))
using (logger.PushProperty("ExtraProperty", "yyy"))
{
    // UserId and ExtraProperty are set for all logging events in these brackets
}
@nblumhardt
Copy link
Member

Great suggestion, thanks 👍

@svrooij
Copy link
Contributor Author

svrooij commented Oct 16, 2020

Want me to create a PR? Do I do that against master?

@nblumhardt
Copy link
Member

dev is the best branch for incoming changes; thanks!

svrooij added a commit to svrooij/serilog-aspnetcore that referenced this issue Oct 22, 2020
nblumhardt pushed a commit that referenced this issue Oct 26, 2020
Docs: Added pushing properties to Readme

Fixed #212

Co-authored-by: Ivan Maximov <[email protected]>
Co-authored-by: Stephan van Rooij <[email protected]>
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 a pull request may close this issue.

2 participants