In a ASP.NET Core 2 application, I need a unique identifier (e.g. Guid) for each request so I can include that id in each log and understand the sequence of logs of each request.
This is not hard to write it myself, but I wonder if there is a builtin feature that I can use or a ASP.NET Core 2 way of achieving this.
The default ASP.NET Core configures the following logging providers: Console, Debug, EventSource, and EventLog (on Windows). You can override the default set of logging providers by calling ClearProviders and adding the specific logging providers that you need.
A middleware is nothing but a component (class) which is executed on every request in ASP.NET Core application.
ASP.NET Core – Exceeds 1.15 Million request/s, 12.6 Gbps NET Core teams and the Open Source . NET community for quite a milestone in performance!
The CustomMiddleware class logs the correlation ID if it is found in the request header. If a correlation ID is not available in the request header, a new correlation ID is generated and added to the request and response headers. Note that a correlation ID here is a GUID — a globally unique identifier.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With