Skip to content

Commit

Permalink
Changed AspNetAppBasePathLayoutRenderer to prioritize current directory
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Nov 19, 2022
1 parent cb51cc1 commit 85b44fd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static ISetupBuilder LoadConfigurationFromAppSettings(this ISetupBuilder

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 (normalizeAppDir.IndexOf(normalizeCurDir, StringComparison.OrdinalIgnoreCase) != 0)
if (string.IsNullOrWhiteSpace(normalizeCurDir) || normalizeAppDir.IndexOf(normalizeCurDir, StringComparison.OrdinalIgnoreCase) != 0)
{
currentBasePath = AppContext.BaseDirectory; // Avoid using Windows-System32 as current directory
}
Expand Down
80 changes: 63 additions & 17 deletions src/Shared/LayoutRenderers/AspNetAppBasePathLayoutRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Text;
#if ASP_NET_CORE
using NLog.Web.DependencyInjection;
Expand All @@ -13,6 +14,7 @@
#endif
using NLog.Config;
using NLog.LayoutRenderers;
using System.Linq;

namespace NLog.Web.LayoutRenderers
{
Expand Down Expand Up @@ -40,12 +42,13 @@ internal IHostEnvironment HostEnvironment
}
private IHostEnvironment _hostEnvironment;
private string _contentRootPath;
private static string _currentAppPath;

/// <inheritdoc />
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
var contentRootPath = _contentRootPath ?? (_contentRootPath = ResolveContentRootPath());
builder.Append(contentRootPath ?? LookupBaseDirectory());
builder.Append(contentRootPath ?? _currentAppPath);
}

private IHostEnvironment ResolveHostEnvironment()
Expand All @@ -63,41 +66,65 @@ private string ResolveContentRootPath()
var contentRootPath = HostEnvironment?.ContentRootPath;
if (string.IsNullOrEmpty(contentRootPath))
{
try
{
contentRootPath = Environment.GetEnvironmentVariable("ASPNETCORE_CONTENTROOT");
}
catch
{
// Not supported or access denied
}
contentRootPath = GetAspNetCoreEnvironment("ASPNETCORE_CONTENTROOT") ?? GetAspNetCoreEnvironment("DOTNET_CONTENTROOT");
}
#else
var contentRootPath = HostEnvironment?.MapPath("~");
#endif
return string.IsNullOrEmpty(contentRootPath) ? null : contentRootPath;
return EnsureDirectorySeparatorEnd(contentRootPath);
}

private static string LookupBaseDirectory()
private static string EnsureDirectorySeparatorEnd(string directoryPath)
{
return string.IsNullOrEmpty(directoryPath) ? null : (directoryPath.TrimEnd(Path.DirectorySeparatorChar).TrimEnd(Path.AltDirectorySeparatorChar) + Path.DirectorySeparatorChar);
}

private static string ResolveCurrentAppDirectory()
{
#if ASP_NET_CORE
var baseDirectory = AppContext.BaseDirectory;
var currentAppPath = AppContext.BaseDirectory;

try
{
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.IsNullOrWhiteSpace(normalizeCurDir) || normalizeAppDir.IndexOf(normalizeCurDir, StringComparison.OrdinalIgnoreCase) != 0)
{
currentBasePath = currentAppPath; // Avoid using Windows-System32 as current directory
}
return currentBasePath;
}
catch
{
// Not supported or access denied
return currentAppPath;
}
#else
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
#endif
if (string.IsNullOrEmpty(baseDirectory))
var currentAppPath = AppDomain.CurrentDomain.BaseDirectory;
if (string.IsNullOrEmpty(currentAppPath))
{
try
{
baseDirectory = System.IO.Directory.GetCurrentDirectory();
currentAppPath = System.IO.Directory.GetCurrentDirectory();
}
catch
{
// Not supported or access denied
}
}
return currentAppPath;
#endif
}

return baseDirectory;
/// <inheritdoc/>
protected override void InitializeLayoutRenderer()
{
if (string.IsNullOrEmpty(_currentAppPath))
{
// Capture current directory at startup, before it changes
_currentAppPath = EnsureDirectorySeparatorEnd(ResolveCurrentAppDirectory());
}
}

/// <inheritdoc/>
Expand All @@ -107,5 +134,24 @@ protected override void CloseLayoutRenderer()
_contentRootPath = null;
base.CloseLayoutRenderer();
}

#if ASP_NET_CORE
private static string GetAspNetCoreEnvironment(string variableName)
{
try
{
var environment = Environment.GetEnvironmentVariable(variableName);
if (string.IsNullOrWhiteSpace(environment))
return null;

return environment.Trim();
}
catch (Exception ex)
{
NLog.Common.InternalLogger.Error(ex, "Failed to lookup environment variable {0}", variableName);
return null;
}
}
#endif
}
}

0 comments on commit 85b44fd

Please sign in to comment.