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..8067bf4f 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,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; + /// + protected override void InitializeLayoutRenderer() + { + if (string.IsNullOrEmpty(_currentAppPath)) + { + // Capture current directory at startup, before it changes + _currentAppPath = EnsureDirectorySeparatorEnd(ResolveCurrentAppDirectory()); + } } /// @@ -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 } } \ No newline at end of file