⚙️ The ID That Arrived After the Log Already Left
Middleware in ASP.NET Core runs in exactly the order it’s registered, and that order applies just as strictly to logging scopes as it does to everything else: middleware registered before the component that generates a request’s correlation ID has already executed – and in many logging setups already written its own log entries – by the time that ID exists. Teams that add request logging early in the pipeline, for visibility into every request regardless of what later middleware decides to do, often add correlation ID generation afterward without reordering anything, and then spend time debugging why the very first log line of a request, and sometimes several that follow, never carry the ID that every later line in the same request consistently has.
🔎 The Problem
var app = builder.Build();
// Registered FIRST: logs every incoming request immediately.
app.UseMiddleware<RequestLoggingMiddleware>();
// Registered SECOND: generates the correlation ID and pushes it
// into the logging scope - but RequestLoggingMiddleware already
// ran and already logged this request WITHOUT an ID by the time
// execution ever reaches this line.
app.UseMiddleware<CorrelationIdMiddleware>();
app.MapControllers();
// Inside RequestLoggingMiddleware:
public async Task InvokeAsync(HttpContext context)
{
_logger.LogInformation("Request started: {Path}", context.Request.Path);
// No correlation ID in scope yet - CorrelationIdMiddleware
// hasn't run. This line, and this line only, is missing it.
await _next(context);
}
✅ Fix: Generate the Correlation ID Before Anything Logs
- Register the correlation ID middleware as the very first entry in the pipeline, ahead of any middleware that writes a log line, so every single log entry for a request – including the first one – has the ID available in scope.
- Where the ID also needs to come from an incoming request header, when a client or upstream service already supplies one, read that header and fall back to generating a new ID in the same early middleware, rather than handling header-reading and generation in two separately-ordered components.
- Add a test that asserts the very first log entry written for a request contains the correlation ID, not just a log entry somewhere in the middle of the request, since checking only a later log line can pass even when the earliest ones are silently missing it.
⚠️ Why This Is Easy to Miss
- Nearly all log lines for a given request do carry the correlation ID correctly, since most logging happens inside controller actions and downstream services that run well after both middleware components – only the handful of very early pipeline log lines are ever affected.
- Middleware registration order is just a sequence of method calls that all compile and run without error regardless of what order they’re in, so there’s no build-time signal that two specific lines need to be swapped for the logs to be complete.
A correlation ID only correlates the logs that were written after it existed – put the generator anywhere but first, and some lines of every request will never be found by it.
