Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<RunAnalyzersDuringLiveAnalysis>true</RunAnalyzersDuringLiveAnalysis>
<_SkipUpgradeNetAnalyzersNuGetWarning>true</_SkipUpgradeNetAnalyzersNuGetWarning>
<AccelerateBuildsInVisualStudio>true</AccelerateBuildsInVisualStudio>
<SatelliteResourceLanguages>en;en-US</SatelliteResourceLanguages>
</PropertyGroup>
<PropertyGroup Label="Package Versions">
<SystemConfigurationConfigurationManagerPackageVersion>4.5.0</SystemConfigurationConfigurationManagerPackageVersion>
Expand Down
62 changes: 52 additions & 10 deletions src/log4net.Tests/Integration/Log4NetIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
using System.Linq;
using log4net.Appender;
using log4net.Config;
using log4net.Core;
using log4net.Layout;
using log4net.Repository;
using log4net.Repository.Hierarchy;
using NUnit.Framework;
using LoggerHierarchy = log4net.Repository.Hierarchy.Hierarchy;

namespace log4net.Tests.Integration
{
Expand Down Expand Up @@ -172,17 +176,24 @@ public void Log4Net_WritesLogFile_WithMaxSizeRoll_Config_Works()
public void Log4Net_WritesLogFile_WithDateAndSizeRoll_Config_Works()
{
DirectoryInfo logDir = CreateLogDirectory("integrationTestLogDir_maxsizerolldate");
(ILog log, ILoggerRepository repo) = ArrangeLogger("log4net.maxsizeroll_date.config");
MockDateTime mockDateTime = new();
repo.GetAppenders().OfType<RollingFileAppender>().First().DateTimeStrategy = mockDateTime;
// Write enough lines to trigger rolling by size and date
for (int i = 1; i < 10000; ++i)
DateTime startDate = new(2025, 12, 08, 15, 55, 50);
MockDateTime mockDateTime = new(startDate); // start at the end of a minute
(ILog log, ILoggerRepository repo) = ArrangeCompositeLogger(mockDateTime);
// distribute 10.000 log entries over 60 seconds
TimeSpan stepIncrement = new(TimeSpan.FromSeconds(60).Ticks / 10000);
// 1000 entries (each 100 bytes) -> ~100KB total - 10 rolls expected - 4 will survive
for (int i = 1; i < 1000; ++i)
{
log.Debug($"DateRoll entry {i}");
if (i % 5000 == 0)
{
mockDateTime.Offset = TimeSpan.FromMinutes(1); // allow time for date to change if needed
}
log.Debug($"DateRoll entry {i:D5}");
mockDateTime.Now += stepIncrement;
}
// switch to next minute to force date roll
mockDateTime.Now = startDate.AddSeconds(10);
// 1000 entries (each 100 bytes) -> ~100KB total - 10 rolls expected - 4 will survive
for (int i = 1; i < 1000; ++i)
{
log.Debug($"DateRoll entry {i:D5}");
mockDateTime.Now += stepIncrement;
}
repo.Shutdown();
// Assert: rolled files exist (date+size pattern)
Expand Down Expand Up @@ -270,5 +281,36 @@ private static (ILog log, ILoggerRepository repo) ArrangeLogger(string configFil
ILog log = LogManager.GetLogger(repo.Name, "IntegrationTestLogger");
return (log, repo);
}

private static (ILog log, ILoggerRepository repo) ArrangeCompositeLogger(RollingFileAppender.IDateTime dateTime)
{
LoggerHierarchy repo = (LoggerHierarchy)LogManager.CreateRepository(Guid.NewGuid().ToString());
PatternLayout layout = new() { ConversionPattern = "%d{yyyy/MM/dd HH:mm:ss.fff} %m-%M%n" };
layout.ActivateOptions();
RollingFileAppender rollingAppender = new()
{
Name = "LogFileAppender",
File = "integrationTestLogDir_maxsizerolldate/.log",
AppendToFile = true,
RollingStyle = RollingFileAppender.RollingMode.Composite,
DatePattern = "HH-mm",
DateTimeStrategy = dateTime,
MaximumFileSize = "10KB",
MaxSizeRollBackups = 3,
StaticLogFileName = false,
CountDirection = 1,
PreserveLogFileNameExtension = true,
LockingModel = new FileAppender.NoLock(),
Layout = layout
};
rollingAppender.ActivateOptions();
repo.Configured = true;
Logger logger = (Logger)repo.GetLogger("IntegrationTestLogger");
logger.Level = Level.Debug;
logger.AddAppender(rollingAppender);
logger.Additivity = false;
return (log: new LogImpl(logger), repo);
}

}
}
9 changes: 2 additions & 7 deletions src/log4net.Tests/Integration/MockDateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,8 @@ namespace log4net.Tests.Integration;
/// <summary>
/// Mock implementation of <see cref="RollingFileAppender.IDateTime"/>
/// </summary>
internal sealed class MockDateTime : RollingFileAppender.IDateTime
internal sealed class MockDateTime(DateTime now) : RollingFileAppender.IDateTime
{
/// <summary>
/// Offset to apply to the current time.
/// </summary>
internal TimeSpan Offset { get; set; }

/// <inheritdoc/>
public DateTime Now => DateTime.Now + Offset;
public DateTime Now { get; set; } = now;
}
25 changes: 0 additions & 25 deletions src/log4net.Tests/Integration/log4net.maxsizeroll_date.config

This file was deleted.

17 changes: 1 addition & 16 deletions src/log4net.Tests/log4net.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNetTestSdkPackageVersion)" />
</ItemGroup>
<ItemGroup>
<None Update="Integration\log4net.maxsizeroll.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Integration\log4net.maxsizeroll_date.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Integration\log4net.no_file_name.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Integration\log4net.roll.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Integration\log4net.roll.config.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Integration\log4net.integration.basic.config">
<None Update="Integration\*.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down
Loading