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

145 lines
5.0 KiB
C#

using GFramework.Core.Abstractions.logging;
using NUnit.Framework;
namespace GFramework.Core.Tests.logging;
/// <summary>
/// 测试 LogEntry 的功能和行为
/// </summary>
[TestFixture]
public class LogEntryTests
{
[Test]
public void Constructor_WithAllParameters_ShouldCreateEntry()
{
var timestamp = DateTime.Now;
var properties = new Dictionary<string, object?> { ["Key1"] = "Value1" };
var exception = new InvalidOperationException("Test");
var entry = new LogEntry(timestamp, LogLevel.Info, "TestLogger", "Test message", exception, properties);
Assert.That(entry.Timestamp, Is.EqualTo(timestamp));
Assert.That(entry.Level, Is.EqualTo(LogLevel.Info));
Assert.That(entry.LoggerName, Is.EqualTo("TestLogger"));
Assert.That(entry.Message, Is.EqualTo("Test message"));
Assert.That(entry.Exception, Is.SameAs(exception));
Assert.That(entry.Properties, Is.SameAs(properties));
}
[Test]
public void Constructor_WithNullException_ShouldWork()
{
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", "Test message", null, null);
Assert.That(entry.Exception, Is.Null);
}
[Test]
public void Constructor_WithNullProperties_ShouldWork()
{
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", "Test message", null, null);
Assert.That(entry.Properties, Is.Null);
}
[Test]
public void GetAllProperties_WithNoProperties_ShouldReturnContextProperties()
{
LogContext.Clear();
using (LogContext.Push("ContextKey", "ContextValue"))
{
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", "Test message", null, null);
var allProps = entry.GetAllProperties();
Assert.That(allProps.Count, Is.EqualTo(1));
Assert.That(allProps["ContextKey"], Is.EqualTo("ContextValue"));
}
LogContext.Clear();
}
[Test]
public void GetAllProperties_WithProperties_ShouldReturnOnlyProperties()
{
LogContext.Clear();
var properties = new Dictionary<string, object?> { ["PropKey"] = "PropValue" };
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", "Test message", null, properties);
var allProps = entry.GetAllProperties();
Assert.That(allProps.Count, Is.EqualTo(1));
Assert.That(allProps["PropKey"], Is.EqualTo("PropValue"));
}
[Test]
public void GetAllProperties_WithBothPropertiesAndContext_ShouldMerge()
{
LogContext.Clear();
using (LogContext.Push("ContextKey", "ContextValue"))
{
var properties = new Dictionary<string, object?> { ["PropKey"] = "PropValue" };
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", "Test message", null, properties);
var allProps = entry.GetAllProperties();
Assert.That(allProps.Count, Is.EqualTo(2));
Assert.That(allProps["ContextKey"], Is.EqualTo("ContextValue"));
Assert.That(allProps["PropKey"], Is.EqualTo("PropValue"));
}
LogContext.Clear();
}
[Test]
public void GetAllProperties_WithConflictingKeys_ShouldPreferEntryProperties()
{
LogContext.Clear();
using (LogContext.Push("Key1", "ContextValue"))
{
var properties = new Dictionary<string, object?> { ["Key1"] = "PropValue" };
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", "Test message", null, properties);
var allProps = entry.GetAllProperties();
Assert.That(allProps.Count, Is.EqualTo(1));
Assert.That(allProps["Key1"], Is.EqualTo("PropValue")); // 日志属性优先
}
LogContext.Clear();
}
[Test]
public void GetAllProperties_WithEmptyPropertiesAndEmptyContext_ShouldReturnEmpty()
{
LogContext.Clear();
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", "Test message", null, null);
var allProps = entry.GetAllProperties();
Assert.That(allProps.Count, Is.EqualTo(0));
}
[Test]
public void RecordEquality_WithSameValues_ShouldBeEqual()
{
var timestamp = DateTime.Now;
var properties = new Dictionary<string, object?> { ["Key1"] = "Value1" };
var entry1 = new LogEntry(timestamp, LogLevel.Info, "TestLogger", "Test message", null, properties);
var entry2 = new LogEntry(timestamp, LogLevel.Info, "TestLogger", "Test message", null, properties);
Assert.That(entry1, Is.EqualTo(entry2));
}
[Test]
public void RecordEquality_WithDifferentValues_ShouldNotBeEqual()
{
var timestamp = DateTime.Now;
var entry1 = new LogEntry(timestamp, LogLevel.Info, "TestLogger", "Test message 1", null, null);
var entry2 = new LogEntry(timestamp, LogLevel.Info, "TestLogger", "Test message 2", null, null);
Assert.That(entry1, Is.Not.EqualTo(entry2));
}
}