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

Fix SonarLint warning about reducing number of conditional expressions #375

Merged
merged 1 commit into from
Dec 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions src/NLog.Extensions.Logging/Logging/NLogLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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
Expand All @@ -145,7 +163,7 @@ private static void SafeAddHiddenAssembly(string assemblyName, bool logOnExcepti
}
}
#endif
}
}
}