From 863f2ac9a7a35f9f80ed7f1e59f389c6d4286f20 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Sun, 8 Dec 2019 23:55:28 +0100 Subject: [PATCH] Fix SonarLint warning about reducing number of conditional expressions --- .../Logging/NLogLoggerProvider.cs | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/NLog.Extensions.Logging/Logging/NLogLoggerProvider.cs b/src/NLog.Extensions.Logging/Logging/NLogLoggerProvider.cs index 5698a861..a8adff9e 100644 --- a/src/NLog.Extensions.Logging/Logging/NLogLoggerProvider.cs +++ b/src/NLog.Extensions.Logging/Logging/NLogLoggerProvider.cs @@ -106,13 +106,7 @@ private static void RegisterHiddenAssembliesForCallSite() var allAssemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (var assembly in allAssemblies) { - if (assembly.FullName.StartsWith("NLog.Extensions.Logging,", StringComparison.OrdinalIgnoreCase) - || assembly.FullName.StartsWith("NLog.Web,", StringComparison.OrdinalIgnoreCase) - || assembly.FullName.StartsWith("NLog.Web.AspNetCore,", StringComparison.OrdinalIgnoreCase) - || assembly.FullName.StartsWith("Microsoft.Extensions.Logging,", StringComparison.OrdinalIgnoreCase) - || assembly.FullName.StartsWith("Microsoft.Extensions.Logging.Abstractions,", StringComparison.OrdinalIgnoreCase) - || assembly.FullName.StartsWith("Microsoft.Extensions.Logging.Filter,", StringComparison.OrdinalIgnoreCase) - || assembly.FullName.StartsWith("Microsoft.Logging,", StringComparison.OrdinalIgnoreCase)) + if (ShouldAddHiddenAssembly(assembly)) { LogManager.AddHiddenAssembly(assembly); } @@ -127,7 +121,31 @@ private static void RegisterHiddenAssembliesForCallSite() #endif } -#if NETCORE1_0 +#if !NETCORE1_0 + private static bool ShouldAddHiddenAssembly(Assembly assembly) + { + var assemblyFullName = assembly?.FullName; + if (string.IsNullOrEmpty(assemblyFullName)) + return false; + + foreach (var hiddenAssemblyPrefix in HiddenAssemblyPrefixes) + if (assemblyFullName.StartsWith(hiddenAssemblyPrefix, StringComparison.OrdinalIgnoreCase)) + return true; + + return false; + } + + private static readonly string[] HiddenAssemblyPrefixes = new[] + { + "NLog.Extensions.Logging,", + "NLog.Web,", + "NLog.Web.AspNetCore,", + "Microsoft.Extensions.Logging,", + "Microsoft.Extensions.Logging.Abstractions,", + "Microsoft.Extensions.Logging.Filter,", + "Microsoft.Logging," + }; +#else private static void SafeAddHiddenAssembly(string assemblyName, bool logOnException = true) { try @@ -145,7 +163,7 @@ private static void SafeAddHiddenAssembly(string assemblyName, bool logOnExcepti } } #endif - } + } }