.NET Core 获取 SkyWalking 当前的 TraceId
注意
2019-6-26 追记:新版本中已经找不到 GlobalTraceId
属性了。
安装
SkyWalking.Agent.AspNetCore
包当前最新版本是 0.6.0。
batchInstall-Package SkyWalking.Agent.AspNetCore
通过
ContextManager
中的GlobalTraceId
获取当前的TraceId
csharp/// <summary> /// Trace Id /// </summary> public string TraceId { get; set; } = SkyWalking.Context.ContextManager.GlobalTraceId;
- csharp
namespace SkyWalking.Context { /// <summary> /// Context manager controls the whole context of tracing. Since .NET server application runs as same as Java, /// We also provide the CONTEXT propagation based on ThreadLocal mechanism. /// Meaning, each segment also related to singe thread. /// </summary> public class ContextManager : ITracingContextListener, IIgnoreTracerContextListener { static ContextManager() { var manager = new ContextManager(); TracingContext.ListenerManager.Add(manager); IgnoredTracerContext.ListenerManager.Add(manager); } private static readonly AsyncLocal<ITracerContext> _context = new AsyncLocal<ITracerContext>(); public static string GlobalTraceId { get { if (_context.Value != null) { return _context.Value.GetReadableGlobalTraceId(); } return "N/A"; } } } }