public object Clone() { ThreadLocalData cLocalData = new ThreadLocalData(); cLocalData.TraceLayer = this.TraceLayer + ".1"; cLocalData.TraceId = this.TraceId; cLocalData.FromUrl = this.FromUrl; cLocalData.ToUrl = this.ToUrl; cLocalData.StartTime = this.StartTime; cLocalData.EndTime = this.EndTime; return(cLocalData); }
/// <summary> /// 设置调用结束时间 /// </summary> public static void SetTraceEndTime() { //if (_bagLocal.Value != null && _bagLocal.Value.TraceId != null) //{ // _bagLocal.Value.EndTime = DateTime.Now; //} try { if (CallContext.LogicalGetData("tp") != null) { ThreadLocalData tld = (ThreadLocalData)CallContext.LogicalGetData("tp"); tld.EndTime = DateTime.Now; CallContext.LogicalSetData("tp", tld); } } catch (Exception) { // } }
/// <summary> /// 初始化线程本地数据 /// </summary> private static void InitThreadSlot() { //lock ("s") //{ //if (_bagLocal.Value == null || string.IsNullOrEmpty(_bagLocal.Value.TraceId)) //{ ThreadLocalData data = null; //http请求 if (BtHttpContext.Current != null && BtHttpContext.Current.Request != null && BtHttpContext.Current.Request.Host != null) { //服务转发过来的请求 data = string.IsNullOrEmpty(BtHttpContext.Current.Request.Headers["BTProcessInfo"]) ? null : JsonConvert.DeserializeObject <ThreadLocalData>(BtHttpContext.Current.Request.Headers["BTProcessInfo"]); if (data != null) { data.FromUrl = data.FromUrl; // data.ToUrl = BtHttpContext.RequestUrl; data.TraceSecondId = string.IsNullOrEmpty(ThreadSlot.GetClientID()) ? null : ThreadSlot.GetClientID().Split('|')[0]; data.StartTime = data.StartTime == DateTime.MinValue ? DateTime.Now : data.StartTime; } else { //app终端 请求带有requestToken string requestToken = string.IsNullOrEmpty(BtHttpContext.Current.Request.Headers["traceToken"]) ? string.Empty : BtHttpContext.Current.Request.Headers["traceToken"].ToString(); //LogService.Default.Debug(HttpContext.Current.Request.Url.AbsoluteUri + "-traceToken-" + (string.IsNullOrEmpty(requestToken) ? "notoken" : requestToken)); if (data == null && (!string.IsNullOrEmpty(requestToken))) { data = new ThreadLocalData(); data.TraceId = requestToken;//HttpContext.Current.Request.Headers["traceToken"].ToString(); data.FromUrl = GetAppOS(); data.ToUrl = BtHttpContext.RequestUrl; data.StartTime = DateTime.Now; data.TraceSecondId = string.IsNullOrEmpty(ThreadSlot.GetClientID()) ? null : ThreadSlot.GetClientID().Split('|')[0]; } } if (data != null) { CallContext.LogicalSetData("tp", data); } //ExecutionContext.RestoreFlow(); } else { data = CallContext.LogicalGetData("tp") == null ? null : CallContext.LogicalGetData("tp") as ThreadLocalData; //ThreadPool.QueueUserWorkItem(p => { object s = CallContext.LogicalGetData("tp"); }); } _bagLocal.Value = data == null ? new ThreadLocalData() { TraceId = Guid.NewGuid().ToString("N"), StartTime = DateTime.Now, TraceSecondId = string.IsNullOrEmpty(ThreadSlot.GetClientID()) ? null : ThreadSlot.GetClientID().Split('|')[0], FromUrl = GetCurrentWebSiteName() } : data; if (CallContext.LogicalGetData("tp") == null) { CallContext.LogicalSetData("tp", _bagLocal.Value); } //LogService.Default.Debug("clientID=" + _bagLocal.Value.TraceSecondId); //} // } }