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

前端 未结 3 1080
星月不相逢
星月不相逢 2021-01-01 14:39

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 req

相关标签:
3条回答
  • 2021-01-01 14:45

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

    0 讨论(0)
  • 2021-01-01 15:00

    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.

    0 讨论(0)
  • 2021-01-01 15:01

    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.

    0 讨论(0)
提交回复
热议问题