标签:register tsp use 分享图片 api bubuko 技术 trace tostring
本文主要是说明core怎么使用链路追踪
/// <summary> /// 注册zipkinTrace /// </summary> /// <param name="app"></param> /// <param name="loggerFactory"></param> /// <param name="lifetime"></param> public void RegisterZipkinTrace(IApplicationBuilder app, ILoggerFactory loggerFactory, IApplicationLifetime lifetime) { lifetime.ApplicationStarted.Register(() => { TraceManager.SamplingRate = 1.0f;//记录数据密度,1.0代表全部记录 var logger = new TracingLogger(loggerFactory, "zipkin4net");//内存数据 var httpSender = new HttpZipkinSender("http://120.78.146.192:9411", "application/json"); var tracer = new ZipkinTracer(httpSender, new JSONSpanSerializer());//注册zipkin TraceManager.RegisterTracer(tracer);//注册 TraceManager.Start(logger);//放到内存中的数据 //File.AppendAllText("D:" + this.GetType().Name + ".txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss" + logger)); }); lifetime.ApplicationStopped.Register(() => TraceManager.Stop()); app.UseTracing("net_core_api");//这边的名字可自定义 }
然后在Configure中添加RegisterZipkinTrace(app, loggerFactory, lifetime)注册
var myTraceId = ConvertHelper.EncodeLongToLowerHexString(Trace.Current.CurrentSpan.TraceId); var mySpanId = ConvertHelper.EncodeLongToLowerHexString(Trace.Current.CurrentSpan.SpanId);
获取出来的值不是真正的spanId和traceId,还需要进行编码:
public static string EncodeLongToLowerHexString(long value) { return value.ToString("x16"); }
好了,现在通过这个方法就可以获取出真正的spanid和traceid,然后输出在日志 以便进行查询日志
菜鸟代码,大神勿喷 如有不对的地方,欢迎指正
标签:register tsp use 分享图片 api bubuko 技术 trace tostring
原文地址:https://www.cnblogs.com/wuyabaibsd/p/9829272.html