我正在使用.net core 3.0预览版7,开放式跟踪和Jaeger。一切运行良好,但是,有一件事尚不清楚,即“ GlobalTracer.Tracer”和“ Diagnostics”的跟踪器ID的值是否不同。
[14:59:19 INF {"SourceContext": "Service1", "RequestId": "0HLOR1KJB6A9N:00000001", "RequestPath": "/Ping", "SpanId": "440b50a4411904c8", "TraceId": "6bf28db9-46118e6c32a3875b", "ParentId": "", "Environment": "Development", "ApplicationName": "Lead-Service", "TraceId_OpenTracing": "440b50a4411904c8"}] 123456
请查看“ TracerId”和“ TraceId_OpenTracing”的值
那么,我们如何配置为相同的值?
Workaround
public class OpenTracingContextLogEventEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
var tracer = GlobalTracer.Instance;
if (tracer?.ActiveSpan == null)
{
return;
}
logEvent.AddOrUpdateProperty(propertyFactory.CreateProperty("TraceId", tracer.ActiveSpan.Context.TraceId));
logEvent.AddOrUpdateProperty(propertyFactory.CreateProperty("SpanId", tracer.ActiveSpan.Context.SpanId));
}
}