-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Fix LoggerProviderDebugView.CalculateEnabledLogLevel
when the level is None
#103209
Conversation
Why |
I'm curious if a null level means None, All, or Undefined. Why it was returning |
@@ -219,6 +219,11 @@ private sealed class LoggerProviderDebugView(string providerName, MessageLogger? | |||
return null; | |||
} | |||
|
|||
if (!logger.Value.MinLevel.HasValue) |
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 think this is the right fix.
The fix should change the line 181:
MessageLogger? messageLogger = logger.MessageLoggers?.FirstOrDefault(messageLogger => messageLogger.Logger == loggerInfo.Logger);
To:
MessageLogger? messageLogger = FirstOrNull(logger.MessageLoggers, loggerInfo.Logger);
MessageLogger? FirstOrNull(MessageLogger[]? array, ILogger logger)
{
if (array is null || array.Length == 0)
{
return null;
}
foreach (var item in array)
{
if (item.Logger == logger)
{
return item;
}
}
return null;
}
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.
To give some more details, in the failing example, logger.MessageLoggers
will be empty array. The line:
MessageLogger? messageLogger = logger.MessageLoggers?.FirstOrDefault(messageLogger => messageLogger.Logger == loggerInfo.Logger);
will return messageLogger
equal to the default
. MessageLogger
is a value type so the returned value will never be null and always has HasValue = true. Then we do new LoggerProviderDebugView(providerName, messageLogger)
passing the default value. The LoggerProviderDebugView.CalculateEnabledLogLevel
will do the check:
private static LogLevel? CalculateEnabledLogLevel(MessageLogger? logger)
{
if (logger == null)
{
return null;
}
which will never be true.
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.
CC @JamesNK
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.
Thank you @tarekgh. I didn't notice that MessageLogger
was a struct, so FirstOrDefault
won't return null. I applied and tested your suggestion.
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.
You could also do this:
MessageLogger? messageLogger = logger
.MessageLoggers
?.Cast<MessageLogger?>()
.FirstOrDefault(messageLogger => messageLogger.Logger == loggerInfo.Logger);
But adding the new method is fine. It avoids some struct generics + LINQ.
Co-authored-by: James Newton-King <james@newtonking.com>
/ba-g the failure is already tracked with the issue dotnet/dnceng#3007. |
Fixes #103208.
I didn't find any unit test for this class, I tested the fix manually.