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

Context data capture does not work with Microsoft.Extensions.Logging and the built-in Console log output #2261

Closed
nr-ahemsath opened this issue Feb 22, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@nr-ahemsath
Copy link
Member

See this issue for more context: #2251

There is currently the following comments and code in the wrapper for Microsoft.Extensions.Logging that does context data capture:

// MEL keeps an array of scope handlers. We can ask one of them for the current scope data.

// Get the array of ScopeLoggers (logger.ScopeLoggers[])
var getLoggersArrayFunc = _getLoggersArray ??= VisibilityBypasser.Instance.GeneratePropertyAccessor<dynamic>(logger.GetType(), "ScopeLoggers");
var loggers = getLoggersArrayFunc(logger);

// Get the first ScopeLogger in the array (logger.ScopeLoggers[0])
// If there is more than one scope logger, they've all received the same data, so the first
// one should be fine
 object firstLogger = loggers.GetValue(0);

However, the statement "if there is more than one scope logger, they've all the received the same data" does not appear to be true in at least one scenario. The linked feature request describes the scenario of having an ASP.NET Core webapi application configured to use MEL's built-in Console logger with the "IncludeScopes" option set to true. Debugging through the agent in this scenario, I found that there were three ScopeLoggers in the array, and only the last one had the ExternalScopeProvider property set to something other than null.

I'm not really sure what the difference is between the scenario the customer described and how our integration tests for context data capture with MEL differ (other than that the integration tests use a simple console app, not an ASP.NET Core web app, which might have other logging configuration baked in going on).

@nr-ahemsath nr-ahemsath added the bug Something isn't working label Feb 22, 2024
@workato-integration
Copy link

@lowell-trimble
Copy link
Contributor

lowell-trimble commented Mar 7, 2024

Yes, this is a bug I've also encountered. It's actually the last ScopeLogger which has the ExternalScopeProvider set.

To avoid this bug, you have to remove all other logging providers so that the Console logging provider is the one and only provider, making it both the first and last.

This can be seen here in Microsoft's repo, that the ExternalScopeProvider is only set on the last ScopeLogger, the rest are null.
https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.Logging/src/LoggerFactory.cs#L250

lowell-trimble added a commit to lowell-trimble/newrelic-dotnet-agent that referenced this issue Mar 7, 2024
@lowell-trimble lowell-trimble mentioned this issue Mar 7, 2024
4 tasks
lowell-trimble added a commit to lowell-trimble/newrelic-dotnet-agent that referenced this issue Mar 7, 2024
nrcventura pushed a commit that referenced this issue Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants