mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
- 为 AsyncLogAppender 添加完整功能测试,包括异步写入、缓冲区管理、并发处理等场景 - 为 CachedLoggerFactory 添加缓存机制测试,验证相同名称和级别的日志记录器重用 - 为 CompositeFilter 添加过滤器组合测试,验证多个过滤器的逻辑组合功能 - 为 CompositeLogger 添加复合日志记录器测试,验证多追加器写入和级别过滤功能 - 为 ConsoleAppender 添加控制台追加器测试,验证格式化输出和过滤器支持 - 为 DefaultLogFormatter 添加默认格式化器测试,验证基本格式化和异常处理功能 - 为 FileAppender 添加文件追加器测试,验证文件写入、目录创建和追加模式功能 - 为 JsonLogFormatter 添加 JSON 格式化器测试,验证 JSON 输出和属性序列化功能 - 为 LogContext 添加日志上下文测试,验证属性推送和作用域管理功能
153 lines
5.4 KiB
C#
153 lines
5.4 KiB
C#
using System.Text.Json;
|
|
using GFramework.Core.Abstractions.logging;
|
|
using GFramework.Core.logging.formatters;
|
|
using NUnit.Framework;
|
|
|
|
namespace GFramework.Core.Tests.logging;
|
|
|
|
/// <summary>
|
|
/// 测试 JsonLogFormatter 的功能和行为
|
|
/// </summary>
|
|
[TestFixture]
|
|
public class JsonLogFormatterTests
|
|
{
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_formatter = new JsonLogFormatter();
|
|
}
|
|
|
|
private JsonLogFormatter _formatter = null!;
|
|
|
|
[Test]
|
|
public void Format_WithBasicEntry_ShouldProduceValidJson()
|
|
{
|
|
var timestamp = new DateTime(2026, 2, 26, 10, 30, 45, 123, DateTimeKind.Utc);
|
|
var entry = new LogEntry(timestamp, LogLevel.Info, "TestLogger", "Test message", null, null);
|
|
|
|
var result = _formatter.Format(entry);
|
|
|
|
Assert.That(() => JsonDocument.Parse(result), Throws.Nothing);
|
|
|
|
var doc = JsonDocument.Parse(result);
|
|
Assert.That(doc.RootElement.GetProperty("level").GetString(), Is.EqualTo("INFO"));
|
|
Assert.That(doc.RootElement.GetProperty("logger").GetString(), Is.EqualTo("TestLogger"));
|
|
Assert.That(doc.RootElement.GetProperty("message").GetString(), Is.EqualTo("Test message"));
|
|
}
|
|
|
|
[Test]
|
|
public void Format_WithException_ShouldIncludeExceptionDetails()
|
|
{
|
|
var exception = new InvalidOperationException("Test exception");
|
|
var entry = new LogEntry(DateTime.Now, LogLevel.Error, "TestLogger", "Error occurred", exception, null);
|
|
|
|
var result = _formatter.Format(entry);
|
|
|
|
var doc = JsonDocument.Parse(result);
|
|
var exceptionObj = doc.RootElement.GetProperty("exception");
|
|
|
|
Assert.That(exceptionObj.GetProperty("type").GetString(), Does.Contain("InvalidOperationException"));
|
|
Assert.That(exceptionObj.GetProperty("message").GetString(), Is.EqualTo("Test exception"));
|
|
Assert.That(exceptionObj.TryGetProperty("stackTrace", out _), Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public void Format_WithProperties_ShouldIncludePropertiesObject()
|
|
{
|
|
var properties = new Dictionary<string, object?>
|
|
{
|
|
["UserId"] = 12345,
|
|
["UserName"] = "TestUser",
|
|
["IsActive"] = true
|
|
};
|
|
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", "User action", null, properties);
|
|
|
|
var result = _formatter.Format(entry);
|
|
|
|
var doc = JsonDocument.Parse(result);
|
|
var propsObj = doc.RootElement.GetProperty("properties");
|
|
|
|
Assert.That(propsObj.GetProperty("userId").GetInt32(), Is.EqualTo(12345));
|
|
Assert.That(propsObj.GetProperty("userName").GetString(), Is.EqualTo("TestUser"));
|
|
Assert.That(propsObj.GetProperty("isActive").GetBoolean(), Is.True);
|
|
}
|
|
|
|
[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);
|
|
|
|
var doc = JsonDocument.Parse(result);
|
|
var propsObj = doc.RootElement.GetProperty("properties");
|
|
|
|
Assert.That(propsObj.GetProperty("key1").ValueKind, Is.EqualTo(JsonValueKind.Null));
|
|
}
|
|
|
|
[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);
|
|
|
|
var doc = JsonDocument.Parse(result);
|
|
Assert.That(doc.RootElement.GetProperty("level").GetString(), Is.EqualTo(expectedStrings[i]));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void Format_WithSpecialCharacters_ShouldEscapeCorrectly()
|
|
{
|
|
var message = "Test \"quoted\" and \n newline";
|
|
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", message, null, null);
|
|
|
|
var result = _formatter.Format(entry);
|
|
|
|
var doc = JsonDocument.Parse(result);
|
|
Assert.That(doc.RootElement.GetProperty("message").GetString(), Is.EqualTo(message));
|
|
}
|
|
|
|
[Test]
|
|
public void Format_ShouldUseIso8601Timestamp()
|
|
{
|
|
var timestamp = new DateTime(2026, 2, 26, 10, 30, 45, 123, DateTimeKind.Utc);
|
|
var entry = new LogEntry(timestamp, LogLevel.Info, "TestLogger", "Test", null, null);
|
|
|
|
var result = _formatter.Format(entry);
|
|
|
|
var doc = JsonDocument.Parse(result);
|
|
var timestampStr = doc.RootElement.GetProperty("timestamp").GetString();
|
|
|
|
Assert.That(timestampStr, Does.Contain("2026-02-26"));
|
|
Assert.That(timestampStr, Does.Contain("T"));
|
|
}
|
|
|
|
[Test]
|
|
public void Format_WithComplexProperties_ShouldSerializeCorrectly()
|
|
{
|
|
var properties = new Dictionary<string, object?>
|
|
{
|
|
["Number"] = 123,
|
|
["String"] = "test",
|
|
["Boolean"] = true,
|
|
["Null"] = null,
|
|
["Array"] = new[] { 1, 2, 3 }
|
|
};
|
|
var entry = new LogEntry(DateTime.Now, LogLevel.Info, "TestLogger", "Test", null, properties);
|
|
|
|
var result = _formatter.Format(entry);
|
|
|
|
Assert.That(() => JsonDocument.Parse(result), Throws.Nothing);
|
|
}
|
|
} |