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

117 lines
3.7 KiB
C#

using GFramework.Core.Abstractions.logging;
using GFramework.Core.logging.formatters;
using NUnit.Framework;
namespace GFramework.Core.Tests.logging;
/// <summary>
/// 测试 DefaultLogFormatter 的功能和行为
/// </summary>
[TestFixture]
public class DefaultLogFormatterTests
{
[SetUp]
public void SetUp()
{
_formatter = new DefaultLogFormatter();
}
private DefaultLogFormatter _formatter = null!;
[Test]
public void Format_WithBasicEntry_ShouldFormatCorrectly()
{
var timestamp = new DateTime(2026, 2, 26, 10, 30, 45, 123);
var entry = new LogEntry(timestamp, LogLevel.Info, "TestLogger", "Test message", null, null);
var result = _formatter.Format(entry);
Assert.That(result, Does.Contain("[2026-02-26 10:30:45.123]"));
Assert.That(result, Does.Contain("INFO"));
Assert.That(result, Does.Contain("[TestLogger]"));
Assert.That(result, Does.Contain("Test message"));
}
[Test]
public void Format_WithException_ShouldIncludeException()
{
var exception = new InvalidOperationException("Test exception");
var entry = new LogEntry(DateTime.Now, LogLevel.Error, "TestLogger", "Error occurred", exception, null);
var result = _formatter.Format(entry);
Assert.That(result, Does.Contain("Error occurred"));
Assert.That(result, Does.Contain("InvalidOperationException"));
Assert.That(result, Does.Contain("Test exception"));
}
[Test]
public void Format_WithProperties_ShouldIncludeProperties()
{
var properties = new Dictionary<string, object?>
{
["UserId"] = 12345,
["UserName"] = "TestUser"
};
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", "User action", null, properties);
var result = _formatter.Format(entry);
Assert.That(result, Does.Contain("User action"));
Assert.That(result, Does.Contain("|"));
Assert.That(result, Does.Contain("UserId=12345"));
Assert.That(result, Does.Contain("UserName=TestUser"));
}
[Test]
public void Format_WithNullProperty_ShouldHandleNull()
{
var properties = new Dictionary<string, object?>
{
["Key1"] = null
};
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", "Test message", null, properties);
var result = _formatter.Format(entry);
Assert.That(result, Does.Contain("Key1="));
}
[Test]
public void Format_WithAllLogLevels_ShouldFormatCorrectly()
{
var levels = new[]
{ LogLevel.Trace, LogLevel.Debug, LogLevel.Info, LogLevel.Warning, LogLevel.Error, LogLevel.Fatal };
var expectedStrings = new[] { "TRACE", "DEBUG", "INFO", "WARNING", "ERROR", "FATAL" };
for (int i = 0; i < levels.Length; i++)
{
var entry = new LogEntry(DateTime.Now, levels[i], "TestLogger", "Test", null, null);
var result = _formatter.Format(entry);
Assert.That(result, Does.Contain(expectedStrings[i]));
}
}
[Test]
public void Format_WithLongMessage_ShouldNotTruncate()
{
var longMessage = new string('A', 1000);
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", longMessage, null, null);
var result = _formatter.Format(entry);
Assert.That(result, Does.Contain(longMessage));
}
[Test]
public void Format_WithSpecialCharacters_ShouldPreserveCharacters()
{
var message = "Test\nNew\tLine\r\nSpecial: <>&\"'";
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", message, null, null);
var result = _formatter.Format(entry);
Assert.That(result, Does.Contain(message));
}
}