mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 18:52:08 +08:00
- 将所有DateTime.Now替换为DateTime.UtcNow以确保时区一致性 - 修复文档中的时间戳记录方式 - 更新测试代码中的时间戳生成逻辑 - 统一框架各模块的时间记录标准
166 lines
5.4 KiB
C#
166 lines
5.4 KiB
C#
using System.IO;
|
|
using GFramework.Core.Abstractions.logging;
|
|
using GFramework.Core.logging.appenders;
|
|
using NUnit.Framework;
|
|
|
|
namespace GFramework.Core.Tests.logging;
|
|
|
|
/// <summary>
|
|
/// 测试 RollingFileAppender 的功能和行为
|
|
/// </summary>
|
|
[TestFixture]
|
|
public class RollingFileAppenderTests
|
|
{
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_testDir = Path.Combine(Path.GetTempPath(), $"rolling_test_{Guid.NewGuid()}");
|
|
Directory.CreateDirectory(_testDir);
|
|
_testFilePath = Path.Combine(_testDir, "app.log");
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
if (Directory.Exists(_testDir))
|
|
{
|
|
try
|
|
{
|
|
Directory.Delete(_testDir, true);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
}
|
|
|
|
private string _testDir = null!;
|
|
private string _testFilePath = null!;
|
|
|
|
[Test]
|
|
public void Constructor_WithInvalidMaxFileSize_ShouldThrowArgumentException()
|
|
{
|
|
Assert.Throws<ArgumentException>(() => new RollingFileAppender(_testFilePath, maxFileSize: 0));
|
|
Assert.Throws<ArgumentException>(() => new RollingFileAppender(_testFilePath, maxFileSize: -1));
|
|
}
|
|
|
|
[Test]
|
|
public void Constructor_WithInvalidMaxFileCount_ShouldThrowArgumentException()
|
|
{
|
|
Assert.Throws<ArgumentException>(() => new RollingFileAppender(_testFilePath, maxFileCount: 0));
|
|
Assert.Throws<ArgumentException>(() => new RollingFileAppender(_testFilePath, maxFileCount: -1));
|
|
}
|
|
|
|
[Test]
|
|
public void Append_WhenFileSizeExceedsLimit_ShouldRollFiles()
|
|
{
|
|
using (var appender = new RollingFileAppender(_testFilePath, maxFileSize: 500, maxFileCount: 3))
|
|
{
|
|
// 写入足够多的日志触发轮转
|
|
for (int i = 0; i < 20; i++)
|
|
{
|
|
var entry = new LogEntry(DateTime.UtcNow, LogLevel.Info, "TestLogger",
|
|
$"This is a test message number {i} with some padding to increase size", null, null);
|
|
appender.Append(entry);
|
|
}
|
|
|
|
appender.Flush();
|
|
}
|
|
|
|
// 检查是否生成了多个文件
|
|
var files = Directory.GetFiles(_testDir, "*.log").OrderBy(f => f).ToArray();
|
|
Assert.That(files.Length, Is.GreaterThan(1));
|
|
}
|
|
|
|
[Test]
|
|
public void Append_ShouldNotExceedMaxFileCount()
|
|
{
|
|
const int maxFileCount = 3;
|
|
using (var appender = new RollingFileAppender(_testFilePath, maxFileSize: 300, maxFileCount: maxFileCount))
|
|
{
|
|
// 写入大量日志触发多次轮转
|
|
for (int i = 0; i < 50; i++)
|
|
{
|
|
var entry = new LogEntry(DateTime.UtcNow, LogLevel.Info, "TestLogger",
|
|
$"This is a test message number {i} with some padding to increase size significantly", null, null);
|
|
appender.Append(entry);
|
|
}
|
|
|
|
appender.Flush();
|
|
}
|
|
|
|
var files = Directory.GetFiles(_testDir, "*.log");
|
|
Assert.That(files.Length, Is.LessThanOrEqualTo(maxFileCount));
|
|
}
|
|
|
|
[Test]
|
|
public void Append_RolledFiles_ShouldHaveCorrectNaming()
|
|
{
|
|
using (var appender = new RollingFileAppender(_testFilePath, maxFileSize: 400, maxFileCount: 3))
|
|
{
|
|
for (int i = 0; i < 30; i++)
|
|
{
|
|
var entry = new LogEntry(DateTime.UtcNow, LogLevel.Info, "TestLogger",
|
|
$"Test message {i} with padding to trigger rolling", null, null);
|
|
appender.Append(entry);
|
|
}
|
|
|
|
appender.Flush();
|
|
}
|
|
|
|
var files = Directory.GetFiles(_testDir, "*.log").Select(Path.GetFileName).OrderBy(f => f).ToArray();
|
|
|
|
// 应该有 app.log, app.1.log, app.2.log 等
|
|
Assert.That(files, Does.Contain("app.log"));
|
|
if (files.Length > 1)
|
|
{
|
|
Assert.That(files.Any(f => f.StartsWith("app.") && f.EndsWith(".log") && f != "app.log"), Is.True);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void Append_AfterDispose_ShouldThrowObjectDisposedException()
|
|
{
|
|
var appender = new RollingFileAppender(_testFilePath);
|
|
appender.Dispose();
|
|
|
|
var entry = new LogEntry(DateTime.UtcNow, LogLevel.Info, "TestLogger", "Test message", null, null);
|
|
|
|
Assert.Throws<ObjectDisposedException>(() => appender.Append(entry));
|
|
}
|
|
|
|
[Test]
|
|
public void Append_WithSmallMaxFileSize_ShouldRollFrequently()
|
|
{
|
|
using (var appender = new RollingFileAppender(_testFilePath, maxFileSize: 200, maxFileCount: 5))
|
|
{
|
|
for (int i = 0; i < 10; i++)
|
|
{
|
|
var entry = new LogEntry(DateTime.UtcNow, LogLevel.Info, "TestLogger",
|
|
"This is a longer message to trigger rolling more frequently", null, null);
|
|
appender.Append(entry);
|
|
}
|
|
|
|
appender.Flush();
|
|
}
|
|
|
|
var files = Directory.GetFiles(_testDir, "*.log");
|
|
Assert.That(files.Length, Is.GreaterThan(1));
|
|
}
|
|
|
|
[Test]
|
|
public void Flush_ShouldEnsureDataWritten()
|
|
{
|
|
using (var appender = new RollingFileAppender(_testFilePath))
|
|
{
|
|
var entry = new LogEntry(DateTime.UtcNow, LogLevel.Info, "TestLogger", "Test message", null, null);
|
|
|
|
appender.Append(entry);
|
|
appender.Flush();
|
|
}
|
|
|
|
Assert.That(File.Exists(_testFilePath), Is.True);
|
|
var content = File.ReadAllText(_testFilePath);
|
|
Assert.That(content, Does.Contain("Test message"));
|
|
}
|
|
} |