GeWuYou abdf4cc690 test(logging): 添加日志系统单元测试
- 为 AsyncLogAppender 添加完整功能测试,包括异步写入、缓冲区管理、并发处理等场景
- 为 CachedLoggerFactory 添加缓存机制测试,验证相同名称和级别的日志记录器重用
- 为 CompositeFilter 添加过滤器组合测试,验证多个过滤器的逻辑组合功能
- 为 CompositeLogger 添加复合日志记录器测试,验证多追加器写入和级别过滤功能
- 为 ConsoleAppender 添加控制台追加器测试,验证格式化输出和过滤器支持
- 为 DefaultLogFormatter 添加默认格式化器测试,验证基本格式化和异常处理功能
- 为 FileAppender 添加文件追加器测试,验证文件写入、目录创建和追加模式功能
- 为 JsonLogFormatter 添加 JSON 格式化器测试,验证 JSON 输出和属性序列化功能
- 为 LogContext 添加日志上下文测试,验证属性推送和作用域管理功能
2026-02-26 19:57:42 +08:00

172 lines
4.9 KiB
C#

using System.IO;
using GFramework.Core.Abstractions.logging;
using GFramework.Core.logging.appenders;
using GFramework.Core.logging.formatters;
using NUnit.Framework;
namespace GFramework.Core.Tests.logging;
/// <summary>
/// 测试 FileAppender 的功能和行为
/// </summary>
[TestFixture]
public class FileAppenderTests
{
[SetUp]
public void SetUp()
{
_testFilePath = Path.Combine(Path.GetTempPath(), $"test_log_{Guid.NewGuid()}.log");
}
[TearDown]
public void TearDown()
{
if (File.Exists(_testFilePath))
{
try
{
File.Delete(_testFilePath);
}
catch
{
}
}
}
private string _testFilePath = null!;
[Test]
public void Constructor_WithNullFilePath_ShouldThrowArgumentException()
{
Assert.Throws<ArgumentException>(() => new FileAppender(null!));
}
[Test]
public void Constructor_WithEmptyFilePath_ShouldThrowArgumentException()
{
Assert.Throws<ArgumentException>(() => new FileAppender(""));
}
[Test]
public void Constructor_ShouldCreateDirectoryIfNotExists()
{
var dirPath = Path.Combine(Path.GetTempPath(), $"test_dir_{Guid.NewGuid()}");
var filePath = Path.Combine(dirPath, "test.log");
try
{
using var appender = new FileAppender(filePath);
Assert.That(Directory.Exists(dirPath), Is.True);
}
finally
{
if (Directory.Exists(dirPath))
{
Directory.Delete(dirPath, true);
}
}
}
[Test]
public void Append_ShouldWriteToFile()
{
using (var appender = new FileAppender(_testFilePath))
{
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", "Test message", null, null);
appender.Append(entry);
appender.Flush();
}
var content = File.ReadAllText(_testFilePath);
Assert.That(content, Does.Contain("Test message"));
Assert.That(content, Does.Contain("INFO"));
}
[Test]
public void Append_MultipleEntries_ShouldWriteAll()
{
using (var appender = new FileAppender(_testFilePath))
{
for (int i = 0; i < 10; i++)
{
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", $"Message {i}", null, null);
appender.Append(entry);
}
appender.Flush();
}
var lines = File.ReadAllLines(_testFilePath);
Assert.That(lines.Length, Is.EqualTo(10));
for (int i = 0; i < 10; i++)
{
Assert.That(lines[i], Does.Contain($"Message {i}"));
}
}
[Test]
public void Append_WithJsonFormatter_ShouldWriteJson()
{
using (var appender = new FileAppender(_testFilePath, new JsonLogFormatter()))
{
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", "Test message", null, null);
appender.Append(entry);
appender.Flush();
}
var content = File.ReadAllText(_testFilePath);
Assert.That(content, Does.Contain("\"message\""));
Assert.That(content, Does.Contain("\"level\""));
}
[Test]
public void Append_AfterDispose_ShouldThrowObjectDisposedException()
{
var appender = new FileAppender(_testFilePath);
appender.Dispose();
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", "Test message", null, null);
Assert.Throws<ObjectDisposedException>(() => appender.Append(entry));
}
[Test]
public void Append_WithAppendMode_ShouldAppendToExistingFile()
{
// 第一次写入
using (var appender1 = new FileAppender(_testFilePath))
{
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", "First message", null, null);
appender1.Append(entry);
appender1.Flush();
}
// 第二次写入
using (var appender2 = new FileAppender(_testFilePath))
{
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", "Second message", null, null);
appender2.Append(entry);
appender2.Flush();
}
var lines = File.ReadAllLines(_testFilePath);
Assert.That(lines.Length, Is.EqualTo(2));
Assert.That(lines[0], Does.Contain("First message"));
Assert.That(lines[1], Does.Contain("Second message"));
}
[Test]
public void Flush_ShouldEnsureDataWritten()
{
using var appender = new FileAppender(_testFilePath);
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", "Test message", null, null);
appender.Append(entry);
appender.Flush();
// 立即读取文件应该能看到内容
var content = File.ReadAllText(_testFilePath);
Assert.That(content, Does.Contain("Test message"));
}
}