Skip to content

Obtaining correlation id #51

@Draeggiar

Description

@Draeggiar

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();
    }

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions