-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Unexpected behavior with ConsoleLoggerProvider & LoggerExternalScopeProvider #39022
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
I checked your sample, it is because for ConsoleLoggerProvider you have IncludeScopes option enabled:
|
HI |
@Ninds so basically
would need to be called before the scope provider setup. That's why ILogger defined in your "if (itWorks)" code block works and not the one define underneath. |
You shouldn't be setting the scope provider yourself, that's the job of the LoggerFactory. What exactly are you trying to do? |
And to further elaborate, I believe when you use LoggerFactory your code sample should look something like below: using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole(opt => opt.IncludeScopes = true);
});
var logger = loggerFactory.CreateLogger<Program>(); But (a heads up), when you update to using .NET5 preview8 or later you'll notice due to some API deprecations you'll end up using the below sample instead: using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddSimpleConsole(opt => opt.IncludeScopes = true);
});
var logger = loggerFactory.CreateLogger<Program>(); Refer to: #34742 which introduced the changes. |
Thank you. That's good to know.
Nothing specific, I was on an exploration exercise. The original context was to have a way of adding attributes to the logging output at the Microsoft.Extensions.Logging level without having to resort to the underlying implementation library , for example :
or
Example of attributes I'd like to add are :
However, I was mistaken about how scopes work in the Logging API and thought that this couldn't be achieved with simple a I subsequently did this test and realised that scopes indeed achieve exactly what I was after. On the other hand it also raised a further question; "How does this (magic) work ?" Moreover, I still find the initial observation counter-intuitive. |
@Drawaes just explained the magic with reference to |
There are 2 scenarios :
Scenario 1
The output is not what is expected in that the scope information is missing from the output:
Scenario 2
The output is what is expected in that the scope information is present in the output:
A reproducer can be found here : ScopedLoggerDemo.cs
I see the same behaviour in 3.1.5 and 5.0.0-preview6
Perhaps the behaviour is by design but it does seem odd.
The text was updated successfully, but these errors were encountered: