TraceId, RequestId, and TraceIdentifier in ASP.NET Core

4.7k Views Asked by At

I'm wondering how ASP.NET Core and Microsoft.Extensions.Logging assigns TraceId, RequestId, and TraceIdentifier. When looking through my log after making a request to my website I see the following information logged from Microsoft.Extensions.Logging:

TraceId: e57eb4708135dd43a914ee9e98165b1b
RequestId: 80000389-0006-ee00-b63f-84710c7967bb

I also log errors happening from ASP.NET Core through custom middleware. On the same request as above I see an error in the same log with the following information:

TraceIdentifier: 80000389-0006-ee00-b63f-84710c7967bb

The value 80000389-0006-ee00-b63f-84710c7967bb looks like the request GUID assigned by ASP.NET Core which makes sense when looking at the RequestId property from Microsoft.Extensions.Logging. But ASP.NET Core logs the request id as TraceIdentifier which feels a little weird.

I would personally prefer having the value from Microsoft.Extension.Logging's TraceId in the TraceIdentifier property when doing custom logging from ASP.NET Core middleware. Any input would be appreciated.

Update - Since writing this question I did create an issue on GitHub which were pretty much ignored and then closed because of inactivity :( https://github.com/dotnet/aspnetcore/issues/31747

1

There are 1 best solutions below

3
On

I was trying this out in .net 7 and trace id is set from part of traceparent as described here. https://www.w3.org/TR/trace-context-1/#header-name

For example: If I send the header the following value.

traceparent: 00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01

then in ASP.net Core, the

trace id is set to 0af7651916cd43dd8448eb211c80319c

parent id is set to b7ad6b7169203331

Here are more fields

Activity.TraceId:            0af7651916cd43dd8448eb211c80319c
Activity.SpanId:             04dcdddd4175b2a4
Activity.TraceFlags:         Recorded
Activity.ParentSpanId:       b7ad6b7169203331
Activity.ActivitySourceName: Microsoft.AspNetCore