How to unique identify each request in a ASP.NET Core 2 application (for logging)

Sounds like you're looking for a correlation id. Maybe Correlation ID Middleware would help?


You may use System.Diagnostics.Activity class (source). From Activity User Guide

Activity.Id serves as hierarchical Request-Id in terms of HTTP Correlation Protocol

This is used for example by Microsoft/ApplicationInsights-aspnetcore for app monitoring.


ASP.NET Core also has HttpContext.TraceIdentifier which uniquely identifies the current request.

It is generated for each incoming request by Kestrel, so isn't suitable for correlating calls to one service from another, but is suitable for correlating logs for a single request together, or to correlating an error message that might be displayed to the user with the corresponding logs.