datalust / serilog-sinks-seq

A Serilog sink that writes events to the Seq structured log server
https://datalust.co/seq
Apache License 2.0
225 stars 50 forks source link

Accept an `ITextFormatter` to customize payload formatting #201

Closed nblumhardt closed 10 months ago

nblumhardt commented 10 months ago

Seq 2024.1 will support trace (span) ingestion, but Serilog doesn't have a built-in way to annotate LogEvents with span start times or parent span ids.

This PR adds support for passing an ITextFormatter to the various Seq() sink configuration methods, and using that formatter to construct newline-delimited JSON payloads.

Hypothetically, if an enricher provides span starts in SpanStart and parent span ids in ParentSpanId, these could be sent to Seq in the @st and @ps payload properties using:


Log.Logger = new LoggerConfiguration()
    .WriteTo.Seq(
        "https://seq.example.com",
        payloadFormatter: new ExpressionTemplate(
            "{ {@t, @mt, @l: if @l = 'Information' then undefined() else @l, @x, @sp, @tr, @ps: ParentSpanId, @st: SpanStart, @ra: {service: {name: 'Example'}}, ..rest()} }\n"))
    .CreateLogger();