Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 307
Victor Avatar asked May 09 '18 22:05

Victor


People also ask

Which logging mechanism is used by default in ASP.NET Core applications?

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.

Which is executed on each request in ASP.NET Core application?

A middleware is nothing but a component (class) which is executed on every request in ASP.NET Core application.

How many requests per second can ASP.NET Core handle?

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!

How do I find the correlation ID in NET Core?

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.


1 Answers

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.

like image 74
Andrew Avatar answered Sep 30 '22 09:09

Andrew