Search code examples
asp.net-web-apiowinserilogowin-middleware

How to add a request identifier to OWIN requests using Serilog?


I'm trying to get the same as with using SerilogWeb.Classic.WebApi using this:

app.Use(async (owinContext, next) =>
{
    using (Serilog.Context.LogContext.PushProperty("HttpRequestId", Guid.NewGuid()))
    {
        await next();
    }
});

and this template

{Timestamp:yyyy-MM-dd HH:mm:ss.ffffff zzz} [{Level:u3}] {HttpRequestId}: {SourceContext}: {Message} {Scope}{NewLine}{Exception}

But HtpRequestId always comes up empty in the log:

2017-09-18 23:26:13.056013 +01:00 [WRN] : <SourceContext>: <message>

What am I doing wrong?


Solution

  • Have you added Enrich.FromLogContext() to your Serilog configuration? This opt-in is required before ambient contextual properties will be picked up and added to events.