Skip to content

Commit

Permalink
added unit test for #193
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeAndNil committed Oct 14, 2024
1 parent bba60f4 commit 79b57e5
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/log4net.Tests/Appender/FileAppenderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
using log4net.Repository;
using log4net.Config;
using log4net.Util;
using log4net.Core;
using System.IO;
using System.Linq;

namespace log4net.Tests.Appender;

Expand Down Expand Up @@ -100,4 +103,49 @@ public void FilenameWithPatternStringTest()

static void LogReceived(object? source, LogReceivedEventArgs e) => Assert.Fail(e.LogLog.Message);
}

/// <summary>
/// Verifies that the <see cref="FileAppender.File"/> property accepts a <see cref="PatternString"/> with a <see cref="GlobalContext"/>
/// </summary>
/// <remarks>see https://github.com/apache/logging-log4net/issues/193</remarks>
[Test]
public void FilenameWithGlobalContextPatternStringTest()
{
DirectoryInfo logs = new("./Logs");
if (logs.Exists)
{
logs.Delete(true);
}

XmlDocument log4netConfig = new();
log4netConfig.LoadXml("""
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%logger] %level - %message%newline%exception"/>
</layout>
</appender>
<appender name="GeneralFileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="Logs\file_%property{LogName}_%date{yyyyMMddHHmmss}.Log"/>
<appendToFile value="true"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%logger] %level - %message%newline%exception"/>
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="GeneralFileAppender"/>
</root>
</log4net>
""");
ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
// latest possible moment to set GlobalContext property used in filename
GlobalContext.Properties["LogName"] = "custom_log_issue_193";
XmlConfigurator.Configure(rep, log4netConfig["log4net"]!);
ILogger logger = rep.GetLogger(nameof(FilenameWithGlobalContextPatternStringTest));
logger.Log(GetType(), Level.Info, nameof(FilenameWithGlobalContextPatternStringTest), null);
logs.Refresh();
Assert.IsTrue(logs.GetFiles().Any(file => file.Name.StartsWith("file_custom_log_issue_193")));
}
}

0 comments on commit 79b57e5

Please sign in to comment.