4

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

ThomasArdal
  • 4,999
  • 4
  • 33
  • 73
  • Hi @ThomasArdal, what does MEls mean? Could you please share some code here? – Rena Apr 13 '21 at 08:00
  • MEL. That's Microsoft.Extensions.Logging. I have updated the post. Thanks – ThomasArdal Apr 13 '21 at 08:15
  • Ok.So it seems you want to get the TraceId in TraceIdentifier? Maybe I think you want the TraceId value belongs to TraceIdentifier property? Becasue TraceId could not exsits in a property. – Rena Apr 13 '21 at 08:27
  • I think my question is more about why ASP.NET Core puts the request ID in the TraceIdentifier property. There are advantages in using TraceId since this would support W3C Trace Context too. – ThomasArdal Apr 13 '21 at 08:30
  • Fine. I think your question here related to the development principal. You need raise such question on github and the PG will tell you the reason. – Rena Apr 13 '21 at 08:37

1 Answers1

1

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
Xavier John
  • 8,474
  • 3
  • 37
  • 51
  • You're right. Setting `traceparent` as the request header do actually correlate log messages across both Microsoft.Extensions.Logging and middleware. It still doesn't set the `TraceId` value in the `TraceIdentifier` field. But I don't think that's the purpose of the field. `TraceIdentifier` contains the request ID and has nothing to do with the trace context, really. – ThomasArdal Mar 17 '23 at 10:47
  • traceId is part of Activity.Current?.Id – Xavier John Mar 17 '23 at 22:21
  • 1
    It is, yes. If you want the `TraceId` only it's on `Activity.Current?.TraceId`. – ThomasArdal Mar 18 '23 at 07:55