From 63d5e9a04ff4fb919be2ddc846cb38b2530ee67e Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Sat, 19 Nov 2022 15:11:09 +0100 Subject: [PATCH] Changed AspNetAppBasePathLayoutRenderer to prioritize current directory --- .../Config/SetupBuilderExtensions.cs | 2 +- .../AspNetAppBasePathLayoutRenderer.cs | 76 ++++++++++++++----- .../AspNetAppBasePathLayoutRendererTests.cs | 6 +- 3 files changed, 57 insertions(+), 27 deletions(-) diff --git a/src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs b/src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs index b5dc4793..f49b485a 100644 --- a/src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs +++ b/src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs @@ -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 } diff --git a/src/Shared/LayoutRenderers/AspNetAppBasePathLayoutRenderer.cs b/src/Shared/LayoutRenderers/AspNetAppBasePathLayoutRenderer.cs index ca979d58..c051b1bd 100644 --- a/src/Shared/LayoutRenderers/AspNetAppBasePathLayoutRenderer.cs +++ b/src/Shared/LayoutRenderers/AspNetAppBasePathLayoutRenderer.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Text; #if ASP_NET_CORE using NLog.Web.DependencyInjection; @@ -13,6 +14,7 @@ #endif using NLog.Config; using NLog.LayoutRenderers; +using System.Linq; namespace NLog.Web.LayoutRenderers { @@ -40,12 +42,13 @@ internal IHostEnvironment HostEnvironment } private IHostEnvironment _hostEnvironment; private string _contentRootPath; + private static string _currentAppPath; /// protected override void Append(StringBuilder builder, LogEventInfo logEvent) { var contentRootPath = _contentRootPath ?? (_contentRootPath = ResolveContentRootPath()); - builder.Append(contentRootPath ?? LookupBaseDirectory()); + builder.Append(contentRootPath ?? _currentAppPath); } private IHostEnvironment ResolveHostEnvironment() @@ -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; + /// + protected override void InitializeLayoutRenderer() + { + if (string.IsNullOrEmpty(_currentAppPath)) + { + // Capture current directory at startup, before it changes + _currentAppPath = TrimEndDirectorySeparator(ResolveCurrentAppDirectory()); + } } /// @@ -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 } } \ No newline at end of file diff --git a/tests/Shared/LayoutRenderers/AspNetAppBasePathLayoutRendererTests.cs b/tests/Shared/LayoutRenderers/AspNetAppBasePathLayoutRendererTests.cs index a5f4f4ba..f29bfb29 100644 --- a/tests/Shared/LayoutRenderers/AspNetAppBasePathLayoutRendererTests.cs +++ b/tests/Shared/LayoutRenderers/AspNetAppBasePathLayoutRendererTests.cs @@ -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]