Search code examples
c#loggingowinserilogowin-middleware

Serilog Owin Middleware


I'm trying to add a simple enrichment using LogContext in an Owin pipeline

My Logger configuration

// configure logger
Log.Logger = new LoggerConfiguration()
            .Enrich.WithProperty("B", 2)
            .ReadFrom.AppSettings()
            .Enrich.FromLogContext()
            .CreateLogger();

app.Use(typeof(LoggerMiddleware));

My Owin middleware

public class LoggerMiddleware : OwinMiddleware
{
    public LoggerMiddleware(OwinMiddleware next) : base(next)
    {
    }

    public override async Task Invoke(IOwinContext context)
    {
        using (LogContext.PushProperty("A", 1))
        {
            await Next.Invoke(context);
        }
    }
}

In the log I can see the property B, 2 but not A, 1.

What am I doing wrong?


Solution

  • So apperantly this has to do with this issue. A workaround that worked for me is changing the order of the owin pipeline and placing the logging middleware after the authentication

    AuthConfig.Configure(app);
    LogConfig.Configure(app);
    WebApiConfig.Configure(app);