Skip to content

Commit

Permalink
Fix TrimDirectorySeparators to handle root-path on Windows and Linux …
Browse files Browse the repository at this point in the history
…to load Nlog.config from root-path
  • Loading branch information
snakefoot committed Feb 24, 2021
1 parent d901724 commit 56cac47
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/NLog/Internal/PathHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ internal static string CombinePaths(string path, string dir, string file)
/// <returns>never null</returns>
public static string TrimDirectorySeparators(string path)
{
return path?.TrimEnd(DirectorySeparatorChars) ?? string.Empty;
var newpath = path?.TrimEnd(DirectorySeparatorChars) ?? string.Empty;
if (newpath.EndsWith(":", System.StringComparison.OrdinalIgnoreCase))
return path; // Support root-path on Windows
else if (string.IsNullOrEmpty(newpath) && !string.IsNullOrEmpty(path))
return path; // Support root-path on Linux
else
return newpath;
}

public static bool IsTempDir(string directory, string tempDir)
Expand Down
4 changes: 3 additions & 1 deletion tests/NLog.UnitTests/ConfigFileLocatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ namespace NLog.UnitTests
using System.Linq;
using System.Text;
using Microsoft.CSharp;
using Xunit;
using NLog.Config;
using NLog.UnitTests.Mocks;
using Xunit;

public sealed class ConfigFileLocatorTests : NLogTestBase, IDisposable
{
Expand Down Expand Up @@ -126,6 +126,8 @@ public static IEnumerable<object[]> GetConfigFile_absolutePath_loads_testData()
var d = Path.DirectorySeparatorChar;
var baseDir = Path.GetTempPath();
var dirInBaseDir = $"{baseDir}dir1";
var rootBaseDir = Path.GetPathRoot(baseDir);
yield return new object[] { "nlog.config", $"{rootBaseDir}nlog.config", $"{rootBaseDir}nlog.config", rootBaseDir };
yield return new object[] { $"{baseDir}configfile", $"{baseDir}configfile", $"{baseDir}configfile", dirInBaseDir };
yield return new object[] { "nlog.config", $"{baseDir}dir1{d}nlog.config", $"{baseDir}dir1{d}nlog.config", dirInBaseDir }; //exists
yield return new object[] { "nlog.config", $"{baseDir}dir1{d}nlog2.config", null, dirInBaseDir }; //not existing, fallback
Expand Down

0 comments on commit 56cac47

Please sign in to comment.