serilog-enrichers-clientinfo icon indicating copy to clipboard operation
serilog-enrichers-clientinfo copied to clipboard

Obtaining correlation id

Open Draeggiar opened this issue 4 months ago • 1 comments

Hi, im wondering if it would be possible to add some graceful way for retrieving correlation id.

Im using it to show user information about error, so admin can get all the logs from the request correlated with it. Currently the only way is to set correlation id manually (which i would like to avoid), or to retrieve it in in some hacky way like this:

    private static Guid GetCorrelationId(HttpContext ctx)
    {
        if (ctx.Items.TryGetValue("Serilog_CorrelationId", out var correlationIdItem) &&
            correlationIdItem is LogEventProperty { Name: "CorrelationId" } correlationIdProperty)
        {
            var correlationId = ((ScalarValue)correlationIdProperty.Value).Value as string;
            return Guid.Parse(correlationId ?? throw new InvalidOperationException("Invalid correlation ID format."));
        }

        return Guid.NewGuid();
    }

Draeggiar avatar Jun 05 '25 07:06 Draeggiar