Fix LoggerProviderDebugView.CalculateEnabledLogLevel when the level is None#103209
Fix LoggerProviderDebugView.CalculateEnabledLogLevel when the level is None#103209tarekgh merged 3 commits intodotnet:mainfrom joegoldman2:fix/103208
LoggerProviderDebugView.CalculateEnabledLogLevel when the level is None#103209Conversation
|
Why |
I'm curious if a null level means None, All, or Undefined. Why it was returning |
| return null; | ||
| } | ||
|
|
||
| if (!logger.Value.MinLevel.HasValue) |
There was a problem hiding this comment.
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.
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.
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.
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.