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

Changed AspNetAppBasePathLayoutRenderer to prioritize current directory #887

Merged
merged 1 commit into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
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
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
76 changes: 55 additions & 21 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,53 @@ 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 TrimEndDirectorySeparator(contentRootPath);
}

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

private static string LookupBaseDirectory()
private static string ResolveCurrentAppDirectory()
{
#if ASP_NET_CORE
var baseDirectory = AppContext.BaseDirectory;
var currentAppPath = AppContext.BaseDirectory;
#else
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
var currentAppPath = AppDomain.CurrentDomain.BaseDirectory;
#endif
if (string.IsNullOrEmpty(baseDirectory))

try
{
try
{
baseDirectory = System.IO.Directory.GetCurrentDirectory();
}
catch
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)
{
// Not supported or access denied
currentBasePath = currentAppPath; // Avoid using Windows-System32 as current directory
}
return currentBasePath;
}
catch
{
// Not supported or access denied
return currentAppPath;
}
}

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

/// <inheritdoc/>
Expand All @@ -107,5 +122,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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ public void NullTest()
renderer.HostEnvironment = hostEnvironment;
string actual = renderer.Render(new LogEventInfo());

#if !ASP_NET_CORE
Assert.Equal(System.IO.Directory.GetCurrentDirectory(), actual);
#else
Assert.Equal(AppContext.BaseDirectory, actual);
#endif
Assert.Equal(System.IO.Directory.GetCurrentDirectory().TrimEnd(System.IO.Path.DirectorySeparatorChar).TrimEnd(System.IO.Path.AltDirectorySeparatorChar), actual);
}

[Fact]
Expand Down