mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
- 将所有DateTime.Now替换为DateTime.UtcNow以确保时区一致性 - 修复文档中的时间戳记录方式 - 更新测试代码中的时间戳生成逻辑 - 统一框架各模块的时间记录标准
145 lines
5.0 KiB
C#
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.UtcNow;
|
|
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.UtcNow, LogLevel.Info, "TestLogger", "Test message", null, null);
|
|
|
|
Assert.That(entry.Exception, Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void Constructor_WithNullProperties_ShouldWork()
|
|
{
|
|
var entry = new LogEntry(DateTime.UtcNow, 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.UtcNow, 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.UtcNow, 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.UtcNow, 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.UtcNow, 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.UtcNow, 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.UtcNow;
|
|
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.UtcNow;
|
|
|
|
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));
|
|
}
|
|
} |