Skip to content

Commit

Permalink
Improve loading of nlog-config with .NET6 single file publish (NLog#4819
Browse files Browse the repository at this point in the history
)
  • Loading branch information
snakefoot committed Mar 3, 2022
1 parent 668c554 commit dabf48b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/NLog/Config/LoggingConfigurationFileLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public IEnumerable<string> GetAppSpecificNLogLocations(string baseDirectory, str

if (PathHelpers.IsTempDir(entryAssemblyLocation, _appEnvironment.UserTempFilePath))
{
// Handle Single File Published on NetCore 3.1 and loading side-by-side exe.nlog (Not relevant for Net5.0)
// Handle Single File Published on NetCore 3.1 and loading side-by-side exe.nlog (Not relevant for Net5.0 and newer)
string processFilePath = _appEnvironment.CurrentProcessFilePath;
if (!string.IsNullOrEmpty(processFilePath))
{
Expand All @@ -320,10 +320,12 @@ public IEnumerable<string> GetAppSpecificNLogLocations(string baseDirectory, str
string assemblyFileName = _appEnvironment.EntryAssemblyFileName;
if (!string.IsNullOrEmpty(assemblyFileName))
{
// Handle unpublished .NET Core Application
var assemblyBaseName = Path.GetFileNameWithoutExtension(assemblyFileName);
if (!string.IsNullOrEmpty(assemblyBaseName))
{
// Handle unpublished .NET Core Application, where assembly-filename has dll-extension
yield return Path.Combine(entryAssemblyLocation, assemblyBaseName + ".exe.nlog");
}

yield return Path.Combine(entryAssemblyLocation, assemblyFileName + ".nlog");
}
Expand Down
14 changes: 13 additions & 1 deletion src/NLog/Internal/AppEnvironmentWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,19 @@ private static string LookupEntryAssemblyFileName()
{
try
{
return Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly()?.Location ?? string.Empty);
var entryAssembly = System.Reflection.Assembly.GetEntryAssembly();
var assemblyLocation = entryAssembly?.Location;
if (!string.IsNullOrEmpty(assemblyLocation))
{
return Path.GetFileName(assemblyLocation);
}

// Fallback to the Assembly-Name when unable to extract FileName from Location
var assemblyName = entryAssembly?.GetName()?.Name;
if (!string.IsNullOrEmpty(assemblyName))
return assemblyName + ".dll";
else
return string.Empty;
}
catch (Exception ex)
{
Expand Down
40 changes: 37 additions & 3 deletions tests/NLog.UnitTests/ConfigFileLocatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,33 @@ public void LoadConfigFile_NetCoreUnpublished_UseEntryDirectory()
AssertResult(tmpDir, "EntryDir", "EntryDir", "Entry", result);
}

[Fact]
public void LoadConfigFile_NetCorePublished_UseBaseDirectory()
{
// Arrange
var tmpDir = Path.GetTempPath();
var appEnvMock = new AppEnvironmentMock(f => true, f => null)
{
AppDomainBaseDirectory = Path.Combine(tmpDir, "BaseDir"),
#if NETSTANDARD
AppDomainConfigurationFile = string.Empty, // .NET 6 single-publish-style
#else
AppDomainConfigurationFile = Path.Combine(tmpDir, "BaseDir", "Entry.exe.config"),
#endif
CurrentProcessFilePath = Path.Combine(tmpDir, "ProcessDir", "Entry.exe"),
EntryAssemblyLocation = string.Empty, // .NET 6 single-publish-style
EntryAssemblyFileName = "Entry.dll",
};

var fileLoader = new LoggingConfigurationFileLoader(appEnvMock);

// Act
var result = fileLoader.GetDefaultCandidateConfigFilePaths().ToList();

// Assert base-directory + process-directory + nlog-assembly-directory
AssertResult(tmpDir, "BaseDir", null, "Entry", result);
}

[Fact]
public void LoadConfigFile_NetCorePublished_UseProcessDirectory()
{
Expand Down Expand Up @@ -295,11 +322,13 @@ public void ValueWithVariableMustNotCauseInfiniteRecursion()
private static void AssertResult(string tmpDir, string appDir, string processDir, string appName, List<string> result)
{
#if NETSTANDARD
Assert.Contains(Path.Combine(tmpDir, processDir, appName + ".exe.nlog"), result, StringComparer.OrdinalIgnoreCase);
Assert.Contains(Path.Combine(tmpDir, processDir ?? appDir, appName + ".exe.nlog"), result, StringComparer.OrdinalIgnoreCase);
Assert.Contains(Path.Combine(tmpDir, appDir, "Entry.dll.nlog"), result, StringComparer.OrdinalIgnoreCase);
if (NLog.Internal.PlatformDetector.IsWin32)
{
if (appDir != processDir)
if (processDir is null)
Assert.Equal(4, result.Count); // Single File Publish on .NET 6 - Case insensitive
else if (appDir != processDir)
Assert.Equal(6, result.Count); // Single File Publish on NetCore 3.1 - Case insensitive
else
Assert.Equal(5, result.Count); // Case insensitive
Expand All @@ -311,7 +340,12 @@ private static void AssertResult(string tmpDir, string appDir, string processDir
#else
Assert.Equal(Path.Combine(tmpDir, appDir, appName + ".exe.nlog"), result.First(), StringComparer.OrdinalIgnoreCase);
if (NLog.Internal.PlatformDetector.IsWin32)
Assert.Equal(4, result.Count); // Case insensitive
{
if (processDir is null)
Assert.Equal(3, result.Count); // Case insensitive - No Assembly/Process-Directory
else
Assert.Equal(4, result.Count); // Case insensitive
}
#endif
Assert.Contains(Path.Combine(tmpDir, "BaseDir", "NLog.config"), result, StringComparer.OrdinalIgnoreCase);
Assert.Contains(Path.Combine(tmpDir, appDir, "NLog.config"), result, StringComparer.OrdinalIgnoreCase);
Expand Down

0 comments on commit dabf48b

Please sign in to comment.