Skip to content

Commit c3b83ec

Browse files
committed
Add draft of tests for file rolling regression
1 parent 05e5c66 commit c3b83ec

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using log4net;
5+
using log4net.Config;
6+
using NUnit.Framework;
7+
8+
namespace log4net.Tests.Integration
9+
{
10+
[TestFixture]
11+
public class Log4NetIntegrationTests
12+
{
13+
[SetUp]
14+
public void SetUp()
15+
{
16+
var filesFromPreviousRuns =
17+
Directory.EnumerateFiles(TestContext.CurrentContext.TestDirectory)
18+
.Where(filePath => Path.GetFileName(filePath).StartsWith("integrationTestLogFile", StringComparison.InvariantCultureIgnoreCase))
19+
.ToList();
20+
filesFromPreviousRuns.ForEach(File.Delete);
21+
var directoriesFromPreviousRuns =
22+
Directory.EnumerateDirectories(TestContext.CurrentContext.TestDirectory)
23+
.Where(dirPath => Path.GetDirectoryName(dirPath)?.StartsWith("integrationTestLogDir", StringComparison.InvariantCultureIgnoreCase) == true)
24+
.ToList();
25+
directoriesFromPreviousRuns.ForEach(d => Directory.Delete(d, true));
26+
}
27+
28+
29+
[Test]
30+
public void Log4Net_WritesLogFile_AndContentIsCorrect()
31+
{
32+
// Arrange: configure log4net from config file
33+
var config = "log4net.integration.basic.config";
34+
string configPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "Integration", config);
35+
var repo = LogManager.CreateRepository(Guid.NewGuid().ToString());
36+
XmlConfigurator.Configure(repo, new FileInfo(configPath));
37+
38+
// Act: log
39+
var log = LogManager.GetLogger(repo.Name, "IntegrationTestLogger");
40+
log.Info("Hello integration test");
41+
log.Error("This is an error");
42+
repo.Shutdown();
43+
44+
// Assert: log file exists and contains expected content
45+
string[] logLines = File.ReadAllLines("integrationTestLogFile_integration.log");
46+
Assert.That(logLines.Length, Is.EqualTo(2));
47+
Assert.That(logLines[0], Does.Contain("Hello integration test"));
48+
Assert.That(logLines[1], Does.Contain("This is an error"));
49+
}
50+
51+
[Test]
52+
public void Log4Net_WritesLogFile_AndContentIsCorrectAfterRestart()
53+
{
54+
for (int i = 0; i < 10; i++)
55+
{
56+
// Arrange: configure log4net from config file
57+
var config = "log4net.integration.basic.config";
58+
string configPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "Integration", config);
59+
var repo = LogManager.CreateRepository(Guid.NewGuid().ToString());
60+
XmlConfigurator.Configure(repo, new FileInfo(configPath));
61+
62+
// Act: log
63+
var log = LogManager.GetLogger(repo.Name, "IntegrationTestLogger");
64+
log.Info("Hello integration test");
65+
log.Error("This is an error");
66+
repo.Shutdown();
67+
}
68+
69+
// Assert: log file exists and contains expected content
70+
string[] logLines = File.ReadAllLines("integrationTestLogFile_integration.log");
71+
Assert.That(logLines.Length, Is.EqualTo(20));
72+
for (int i = 0; i < 10; i++)
73+
{
74+
Assert.That(logLines[i * 2], Does.Contain("Hello integration test"));
75+
Assert.That(logLines[i * 2 + 1], Does.Contain("This is an error"));
76+
}
77+
}
78+
79+
[Test]
80+
public void Log4Net_WritesLogFile_WithRollAndNoAppend_AndContentIsCorrectAfterRestart()
81+
{
82+
for (int i = 0; i < 10; i++)
83+
{
84+
// Arrange: configure log4net from config file
85+
var config = "log4net.roll.config.xml";
86+
string configPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "Integration", config);
87+
var repo = LogManager.CreateRepository(Guid.NewGuid().ToString());
88+
XmlConfigurator.Configure(repo, new FileInfo(configPath));
89+
90+
// Act: log
91+
var log = LogManager.GetLogger(repo.Name, "log");
92+
for (int j = 0; j < 10; ++j)
93+
{
94+
log.Info($"Hello, log4net! {i} {j}");
95+
}
96+
repo.Shutdown();
97+
}
98+
99+
// Assert: log file exists and contains expected content
100+
string[] logFiles = Directory.GetFiles("integrationTestLogDir_1");
101+
Assert.That(logFiles.Length, Is.EqualTo(10));
102+
}
103+
}
104+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<log4net>
3+
<appender name="FileAppender" type="log4net.Appender.FileAppender,log4net">
4+
<file value="integrationTestLogFile_integration.log" />
5+
<appendToFile value="true" />
6+
<layout type="log4net.Layout.PatternLayout">
7+
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
8+
</layout>
9+
</appender>
10+
<root>
11+
<level value="DEBUG" />
12+
<appender-ref ref="FileAppender" />
13+
</root>
14+
</log4net>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<log4net>
4+
<appender name="Log"
5+
type="log4net.Appender.RollingFileAppender">
6+
7+
<file type="log4net.Util.PatternString">
8+
<conversionPattern value="integrationTestLogDir_1\\Logging.log"/>
9+
</file>
10+
11+
<param name="StaticLogFileName" value="true"/>
12+
<param name="AppendToFile" value="false"/>
13+
<rollingStyle value="Size"/>
14+
<maximumFileSize value="1MB"/>
15+
<maxSizeRollBackups value="100"/>
16+
<CountDirection value="1"/>
17+
<preserveLogFileNameExtension value="true"/>
18+
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
19+
20+
<layout type="log4net.Layout.PatternLayout">
21+
<param name="ConversionPattern"
22+
value="%date [%3thread] %-5level %logger{1} %-20message %n"/>
23+
</layout>
24+
</appender>
25+
26+
<logger name="log">
27+
<level value="debug" />
28+
<appender-ref ref="Log" />
29+
</logger>
30+
</log4net>
31+
</configuration>

src/log4net.Tests/log4net.Tests.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,13 @@
3434
<ItemGroup Condition="'$(TargetFramework)'=='net8.0'">
3535
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNetTestSdkPackageVersion)" />
3636
</ItemGroup>
37+
<ItemGroup>
38+
<None Update="Integration\log4net.roll.config.xml">
39+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
40+
</None>
41+
<None Update="Integration\log4net.integration.basic.config">
42+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
43+
</None>
44+
</ItemGroup>
3745
<Import Project="../MonoForFramework.targets" />
3846
</Project>

0 commit comments

Comments
 (0)