Skip to content

Commit

Permalink
Fix Sonar code smell (#1046)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored May 26, 2024
1 parent 046657f commit 24baed0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
31 changes: 16 additions & 15 deletions src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,11 @@ public static class SetupBuilderExtensions
public static ISetupBuilder LoadConfigurationFromAppSettings(this ISetupBuilder setupBuilder, string basePath = null, string environment = null, string nlogConfigSection = "NLog", bool optional = true, bool reloadOnChange = false)
{
environment = environment ?? GetAspNetCoreEnvironment("ASPNETCORE_ENVIRONMENT") ?? GetAspNetCoreEnvironment("DOTNET_ENVIRONMENT") ?? "Production";
basePath = basePath ?? GetAspNetCoreEnvironment("ASPNETCORE_CONTENTROOT") ?? GetAspNetCoreEnvironment("DOTNET_CONTENTROOT");

var currentBasePath = basePath;
if (currentBasePath is null)
{
currentBasePath = Environment.CurrentDirectory;

var normalizeCurDir = Path.GetFullPath(currentBasePath).TrimEnd(Path.DirectorySeparatorChar).TrimEnd(Path.AltDirectorySeparatorChar).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
var normalizeAppDir = Path.GetFullPath(AppContext.BaseDirectory).TrimEnd(Path.DirectorySeparatorChar).TrimEnd(Path.AltDirectorySeparatorChar).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
if (string.IsNullOrWhiteSpace(normalizeCurDir) || normalizeAppDir.IndexOf(normalizeCurDir, StringComparison.OrdinalIgnoreCase) != 0)
{
currentBasePath = AppContext.BaseDirectory; // Avoid using Windows-System32 as current directory
}
}
basePath = basePath ?? GetAspNetCoreEnvironment("ASPNETCORE_CONTENTROOT") ?? GetAspNetCoreEnvironment("DOTNET_CONTENTROOT") ?? ResolveCurrentAppDirectory();

var builder = new ConfigurationBuilder()
// Host Configuration
.SetBasePath(currentBasePath)
.SetBasePath(basePath)
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
.AddEnvironmentVariables(prefix: "DOTNET_")
// App Configuration
Expand Down Expand Up @@ -92,6 +79,20 @@ public static ISetupBuilder LoadConfigurationFromAppSettings(this ISetupBuilder
}
}

private static string ResolveCurrentAppDirectory()
{
var currentBasePath = Environment.CurrentDirectory;

var normalizeCurDir = Path.GetFullPath(currentBasePath).TrimEnd(Path.DirectorySeparatorChar).TrimEnd(Path.AltDirectorySeparatorChar).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
var normalizeAppDir = Path.GetFullPath(AppContext.BaseDirectory).TrimEnd(Path.DirectorySeparatorChar).TrimEnd(Path.AltDirectorySeparatorChar).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
if (string.IsNullOrWhiteSpace(normalizeCurDir) || !normalizeCurDir.StartsWith(normalizeAppDir, StringComparison.OrdinalIgnoreCase))
{
currentBasePath = AppContext.BaseDirectory; // Avoid using Windows-System32 as current directory
}

return currentBasePath;
}

private static bool IsLoggingConfigurationLoaded(LoggingConfiguration cfg)
{
return cfg?.LoggingRules?.Count > 0 && cfg?.AllTargets?.Count > 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private static string ResolveCurrentAppDirectory()
var currentBasePath = Environment.CurrentDirectory;
var normalizeCurDir = Path.GetFullPath(currentBasePath).TrimEnd(Path.DirectorySeparatorChar).TrimEnd(Path.AltDirectorySeparatorChar).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
var normalizeAppDir = Path.GetFullPath(currentAppPath).TrimEnd(Path.DirectorySeparatorChar).TrimEnd(Path.AltDirectorySeparatorChar).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
if (string.IsNullOrEmpty(normalizeCurDir) || normalizeAppDir.IndexOf(normalizeCurDir, StringComparison.OrdinalIgnoreCase) != 0)
if (string.IsNullOrEmpty(normalizeCurDir) || !normalizeCurDir.StartsWith(normalizeAppDir, StringComparison.OrdinalIgnoreCase))
{
currentBasePath = currentAppPath; // Avoid using Windows-System32 as current directory
}
Expand Down

0 comments on commit 24baed0

Please sign in to comment.