I'm using Serilog and Seq in a .net core web api to handle the logging. I've added a reference to Steve Gordon's CorrelationId Middleware to get the X-Correlation-ID from the headers (or create a new one) to update the TraceIdentifier.
I've then added another middleware class to push the correlation id into the log context, but this isn't working correctly:
public class LogCorrelationIdMiddleware
{
private readonly RequestDelegate _next;
public LogCorrelationIdMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext context)
{
// doesn't work - blank CorrelationId in Seq
using (LogContext.PushProperty("CorrelationId", context.Request.HttpContext.TraceIdentifier)) { return _next(context); }
// works - correct CorrelationId2 in Seq
//using (LogContext.PushProperty("CorrelationId2", context.Request.HttpContext.TraceIdentifier)) { return _next(context); }
}
}
Any ideas why?