public ThreadLocalData GetTraceLog() { ThreadLocalData data = new ThreadLocalData(); try { object o = BTCallContextData.GetData(BTCallContextData.LogTraceKey); if (o != null) { data = o as ThreadLocalData; } else { data = new ThreadLocalData() { TraceId = Guid.NewGuid().ToString("N"), StartTime = DateTime.Now, TraceSecondId = System.Environment.MachineName, FromUrl = "Localhost" }; //BTCallContextData.SetData("BTTrace", data); } } catch (Exception) { // } return(data); }
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请求 try { if (HttpContext.Current != null && HttpContext.Current.Request != null && HttpContext.Current.Request.Url != null) { //服务转发过来的请求 data = HttpContext.Current.Request.Headers["BTProcessInfo"] == null ? null : JsonConvert.DeserializeObject <ThreadLocalData>(HttpContext.Current.Request.Headers["BTProcessInfo"]); if (data != null) { //Helper.LogHelper.WriteLog("Server:请求线程数据0000"); data.FromUrl = data.FromUrl; // data.ToUrl = HttpContext.Current.Request.Url.ToSafeString(""); data.TraceSecondId = string.IsNullOrEmpty(ThreadSlot.GetClientID()) ? null : ThreadSlot.GetClientID().Split('|')[0]; data.StartTime = data.StartTime == DateTime.MinValue ? DateTime.Now : data.StartTime; } else { // Helper.LogHelper.WriteLog("Server:请求线程数据1111"); //app终端 请求带有requestToken string requestToken = HttpContext.Current.Request.Headers["traceToken"] == null ? string.Empty : HttpContext.Current.Request.Headers["traceToken"].ToString(); //AccessToken 获取到用户token 判断是不是调试用户 add by weiyz 2019-12-9 string ISDEBUG = HttpContext.Current.Request.Headers.AllKeys.Contains("DEBUGEMP") == false ? string.Empty : HttpContext.Current.Request.Headers["DEBUGEMP"].ToString(); //Helper.LogHelper.WriteLog("Server:请求线程数据2222:" + ISDEBUG); //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 = HttpContext.Current.Request.Url.AbsoluteUri; data.StartTime = DateTime.Now; data.TraceSecondId = string.IsNullOrEmpty(ThreadSlot.GetClientID()) ? null : ThreadSlot.GetClientID().Split('|')[0]; data.IsDebug = ISDEBUG == "1"; } } 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"); }); } } catch { data = CallContext.LogicalGetData("tp") == null ? null : CallContext.LogicalGetData("tp") as ThreadLocalData; } _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); //} // } }