feat(logging): default TaskId enricher with passing tests
This commit is contained in:
15
src/ClaudeDo.Logging/DefaultTaskIdEnricher.cs
Normal file
15
src/ClaudeDo.Logging/DefaultTaskIdEnricher.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using Serilog.Core;
|
||||||
|
using Serilog.Events;
|
||||||
|
|
||||||
|
namespace ClaudeDo.Logging;
|
||||||
|
|
||||||
|
/// <summary>Ensures every log event carries a TaskId property (defaulting to "-")
|
||||||
|
/// so the output template's [{TaskId}] column never renders the raw token.</summary>
|
||||||
|
public sealed class DefaultTaskIdEnricher : ILogEventEnricher
|
||||||
|
{
|
||||||
|
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
|
||||||
|
{
|
||||||
|
if (!logEvent.Properties.ContainsKey("TaskId"))
|
||||||
|
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("TaskId", "-"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
namespace ClaudeDo.Logging;
|
|
||||||
|
|
||||||
internal static class Placeholder;
|
|
||||||
@@ -25,6 +25,7 @@
|
|||||||
<ProjectReference Include="..\..\src\ClaudeDo.Worker\ClaudeDo.Worker.csproj" />
|
<ProjectReference Include="..\..\src\ClaudeDo.Worker\ClaudeDo.Worker.csproj" />
|
||||||
<ProjectReference Include="..\..\src\ClaudeDo.Data\ClaudeDo.Data.csproj" />
|
<ProjectReference Include="..\..\src\ClaudeDo.Data\ClaudeDo.Data.csproj" />
|
||||||
<ProjectReference Include="..\..\src\ClaudeDo.Ui\ClaudeDo.Ui.csproj" />
|
<ProjectReference Include="..\..\src\ClaudeDo.Ui\ClaudeDo.Ui.csproj" />
|
||||||
|
<ProjectReference Include="..\..\src\ClaudeDo.Logging\ClaudeDo.Logging.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
using ClaudeDo.Logging;
|
||||||
|
using Serilog;
|
||||||
|
using Serilog.Context;
|
||||||
|
using Serilog.Core;
|
||||||
|
using Serilog.Events;
|
||||||
|
|
||||||
|
namespace ClaudeDo.Worker.Tests.Logging;
|
||||||
|
|
||||||
|
public sealed class DefaultTaskIdEnricherTests
|
||||||
|
{
|
||||||
|
private sealed class CollectingSink : ILogEventSink
|
||||||
|
{
|
||||||
|
public List<LogEvent> Events { get; } = new();
|
||||||
|
public void Emit(LogEvent logEvent) => Events.Add(logEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void AddsDash_WhenNoTaskIdInScope()
|
||||||
|
{
|
||||||
|
var sink = new CollectingSink();
|
||||||
|
using var logger = new LoggerConfiguration()
|
||||||
|
.Enrich.FromLogContext()
|
||||||
|
.Enrich.With(new DefaultTaskIdEnricher())
|
||||||
|
.WriteTo.Sink(sink)
|
||||||
|
.CreateLogger();
|
||||||
|
|
||||||
|
logger.Information("hello");
|
||||||
|
|
||||||
|
var prop = Assert.Single(sink.Events).Properties["TaskId"];
|
||||||
|
Assert.Equal("\"-\"", prop.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void KeepsPushedTaskId_WhenInScope()
|
||||||
|
{
|
||||||
|
var sink = new CollectingSink();
|
||||||
|
using var logger = new LoggerConfiguration()
|
||||||
|
.Enrich.FromLogContext()
|
||||||
|
.Enrich.With(new DefaultTaskIdEnricher())
|
||||||
|
.WriteTo.Sink(sink)
|
||||||
|
.CreateLogger();
|
||||||
|
|
||||||
|
using (LogContext.PushProperty("TaskId", "task-42"))
|
||||||
|
logger.Information("hello");
|
||||||
|
|
||||||
|
var prop = Assert.Single(sink.Events).Properties["TaskId"];
|
||||||
|
Assert.Equal("\"task-42\"", prop.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user