.NET Core中HttpContext TraceIdentifier是如何生成的?

32

HttpContext TraceIdentifier(也称为Correlation-Id)是如何生成的?

我通过控制器请求页面,得到以下TraceId:0HLEACIU86PT6:0000000D

该页面触发了一个ajax调用,它有以下TraceId:0HLEACIU86PT7:00000005

您可以看到它们非常相似。 它是基于时间吗?

为什么我没有得到相同的TraceIdentifier?

如何确保相同的TraceIdentifier?


4
页面请求和AJAX请求是两个独立的请求,因此具有不同的TraceId。 - Brad
2个回答

56

Kestrel生成请求ID的格式为{ConnectionId}:{Request number}。连接ID是使用字母表1-9和A - V对长整型数进行Base32编码的结果。请求计数表示在该连接上发出的请求数量。特定连接上的第n个请求的ID为{ConnectionId}:{n}。

https://github.com/aspnet/KestrelHttpServer/blob/a48222378b8249a26b093b5b835001c7c7b45815/src/Kestrel.Core/Internal/Infrastructure/CorrelationIdGenerator.cs

https://github.com/aspnet/KestrelHttpServer/blob/0aff4a0440c2f393c0b98e9046a8e66e30a56cb0/src/Kestrel.Core/Internal/Http/Http1Connection.cs#L446


2
ConnectionId 是如何生成的? - Konrad
2
https://github.com/aspnet/KestrelHttpServer/blob/a48222378b8249a26b093b5b835001c7c7b45815/src/Kestrel.Core/Internal/Infrastructure/CorrelationIdGenerator.cs - davidfowl
5
@davidfowl,有没有一种方法可以覆盖Kestrel生成CorrelationId的方式?我希望它总是使用GUID。 - Dave Black
嗨@DaveBlack,HttpContext.TraceIdentifier已定义了get和set,因此您可以在中间件中覆盖它。https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.httpcontext.traceidentifier?view=aspnetcore-5.0 - Kiran B

1

2
这个实现在Kestrel中没有被使用。 - davidfowl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接