From 41d4c4614ebee37b861511949556aed67f3d85eb Mon Sep 17 00:00:00 2001 From: joegoldman2 <147369450+joegoldman2@users.noreply.github.com> Date: Tue, 11 Jun 2024 21:29:21 +0000 Subject: [PATCH] Fix `LoggerProviderDebugView.CalculateEnabledLogLevel` when the level is `None` (#103209) * Fix LoggerProviderDebugView.CalculateEnabledLogLevel when the level is None * Apply suggestion from review * Update src/libraries/Microsoft.Extensions.Logging/src/Logger.cs Co-authored-by: James Newton-King --------- Co-authored-by: joegoldman2 <147369450+joegoldman@users.noreply.github.com> Co-authored-by: Tarek Mahmoud Sayed Co-authored-by: James Newton-King --- .../src/Logger.cs | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/libraries/Microsoft.Extensions.Logging/src/Logger.cs b/src/libraries/Microsoft.Extensions.Logging/src/Logger.cs index 1443230297fbb..cf00d35ee31b2 100644 --- a/src/libraries/Microsoft.Extensions.Logging/src/Logger.cs +++ b/src/libraries/Microsoft.Extensions.Logging/src/Logger.cs @@ -178,12 +178,31 @@ public List Providers { LoggerInformation loggerInfo = logger.Loggers[i]; string providerName = ProviderAliasUtilities.GetAlias(loggerInfo.ProviderType) ?? loggerInfo.ProviderType.Name; - MessageLogger? messageLogger = logger.MessageLoggers?.FirstOrDefault(messageLogger => messageLogger.Logger == loggerInfo.Logger); + MessageLogger? messageLogger = FirstOrNull(logger.MessageLoggers, loggerInfo.Logger); providers.Add(new LoggerProviderDebugView(providerName, messageLogger)); } return providers; + + // Find message logger or return null if there is no match. FirstOrDefault isn't used because MessageLogger is a struct. + static MessageLogger? FirstOrNull(MessageLogger[]? messageLoggers, ILogger logger) + { + if (messageLoggers is null || messageLoggers.Length == 0) + { + return null; + } + + foreach (MessageLogger item in messageLoggers) + { + if (item.Logger == logger) + { + return item; + } + } + + return null; + } } }