Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            ServiceContainer.Resolve <IInitRequestScopeContext>().BeginRequest(filterContext.HttpContext.Request);

            if (!filterContext.HasMarkerAttribute <NonTracingAttribute>())
            {
                var request  = filterContext.HttpContext.Request;
                var response = filterContext.HttpContext.Response;

                Header reqHeader = TracingContextData.GetDefaultRequestHeader();

                TracingContextData.SetSubRpcID(reqHeader.RpcID + ".0");
                TracingContextData.SetRequestHeader(reqHeader);

                if (!filterContext.HasMarkerAttribute <NotToLogAttribute>())
                {
                    TraceLogs trace = new TraceLogs();
                    trace.ContextType = ContextType.Server.ToString();
                    trace.StartTime   = DateTime.Now;
                    trace.MachineAddr = Util.TracingContextHelper.GetServerAddress();
                    trace.TraceId     = reqHeader.TraceID;
                    trace.RpcId       = reqHeader.RpcID;
                    trace.Protocol    = request.Url.Scheme;

                    trace.Environment = this.environment ?? EnvironmentConfig.Environment;
                    trace.SystemID    = this.systemID ?? EnvironmentConfig.SystemID;
                    trace.SystemName  = this.systemName ?? EnvironmentConfig.SystemName;

                    //srs.InvokeID = string.Format("{0}_{1}", filterContext.ActionDescriptor.ControllerDescriptor.ControllerName.ToLower(), filterContext.ActionDescriptor.ActionName.ToLower());

                    //InvokeID
                    trace.InvokeID = request.Url.AbsolutePath;
                    string folder = filterContext.HttpContext.Request.Headers[Config.ResponseHeaderFolderKey];
                    if (!string.IsNullOrWhiteSpace(folder))
                    {
                        trace.ServerHost = request.Url.Host + folder;
                    }
                    else
                    {
                        trace.ServerHost = request.Url.Host;
                    }

                    TraceExtensionOnActionExecuting(filterContext, trace);

                    Util.TracingContextHelper.SetContextItem(Config.ServerRSKey, trace);
                }
            }

            base.OnActionExecuting(filterContext);
        }
        private static void Client_OnRequest(HttpRequestMessage request, RpcContext rpcContext)
        {
            var rpcId         = string.Empty;
            var requestHeader = TracingContextData.GetRequestHeader();

            if (requestHeader == null)
            {
                requestHeader = TracingContextData.GetDefaultRequestHeader();
                TracingContextData.SetRequestHeader(requestHeader);
                //HttpContentData.SetSubRpcID(modelHeader.RpcID + ".0");
                //rpcId = requestHeader.RpcID + ".0";
            }
            //else
            //{
            //    rpcId = Util.VersionIncr(TracingContextData.GetSubRpcID());
            //}
            rpcId = Util.VersionIncr(TracingContextData.GetSubRpcID());
            TracingContextData.SetSubRpcID(rpcId);

            rpcContext.Items[TraceIDKey] = requestHeader.TraceID;
            rpcContext.Items[RpcIDKey]   = rpcId;

            //post/put content is ObjectContent
            var oc = request.Content as ObjectContent;

            if (oc != null)
            {
                var reqModel = oc.Value as IRequestModel <Header>;
                if (reqModel != null)
                {
                    if (reqModel.Header == null)
                    {
                        reqModel.Header = new Header();
                    }

                    reqModel.Header.TraceID = requestHeader.TraceID;
                    reqModel.Header.RpcID   = rpcId;
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="actionContext"></param>
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            ServiceContainer.Resolve <IInitRequestScopeContext>().BeginRequest(actionContext.Request);

            if (!actionContext.HasMarkerAttribute <NonTracingAttribute>())
            {
                var request = actionContext.Request;
                IRequestModel <Header> reqModel = null;
                Header reqHeader = null;

                if (actionContext.ActionArguments.Count > 0)
                {
                    foreach (var dic in actionContext.ActionArguments)
                    {
                        //if (dic.Value is RequestModel)
                        //{
                        //    reqModel = dic.Value as RequestModel;
                        //    break;
                        //}
                        reqModel = dic.Value as IRequestModel <Header>;
                        if (reqModel != null)
                        {
                            break;
                        }
                    }
                }
                if (reqModel == null && actionContext.Request.Content != null && string.Equals(actionContext.Request.Method.Method, "post", StringComparison.CurrentCultureIgnoreCase))
                {
                    try
                    {
                        reqModel = actionContext.Request.Content.ReadAsAsync <RequestModel>().Result;
                    }
                    catch { }
                    if (reqModel != null)
                    {
                        actionContext.ActionArguments.Add(Guid.NewGuid().ToString("N"), reqModel);
                    }
                }

                if (reqModel != null && reqModel.Header != null)
                {
                    reqHeader = reqModel.Header;
                    if (string.IsNullOrWhiteSpace(reqHeader.TraceID))
                    {
                        reqHeader.TraceID = Generate.GenerateId();// Util.GetUniqueCode32();
                    }
                    if (string.IsNullOrWhiteSpace(reqHeader.RpcID))
                    {
                        reqHeader.RpcID = "0";
                    }

                    //HttpContentData.SetTrackID(reqHeader.TraceID);
                    //HttpContentData.SubRpcID = reqHeader.RpcID + ".0";
                    //var header = HttpContentData.CloneRequestHeader(reqModel.Header);
                    //header.RpcID = header.RpcID + ".0";
                }
                else
                {
                    reqHeader = TracingContextData.GetDefaultRequestHeader();
                    //HttpContentData.SetTrackID(reqHeader.TraceID);
                }
                TracingContextData.SetSubRpcID(reqHeader.RpcID + ".0");
                TracingContextData.SetRequestHeader(reqHeader);
                //HttpContentData.RequestHeader = reqHeader;

                //Not To Log
                if (!actionContext.HasMarkerAttribute <NotToLogAttribute>())
                {
                    TraceLogs trace = new TraceLogs();
                    trace.ContextType = ContextType.Server.ToString();
                    trace.StartTime   = DateTime.Now;
                    trace.MachineAddr = Util.TracingContextHelper.GetServerAddress();
                    trace.TraceId     = reqHeader.TraceID;
                    trace.RpcId       = reqHeader.RpcID;
                    trace.Protocol    = string.Format("{0}/{1}", actionContext.Request.RequestUri.Scheme, actionContext.Request.Version);

                    trace.Environment = this.environment ?? EnvironmentConfig.Environment;
                    trace.SystemID    = this.systemID ?? EnvironmentConfig.SystemID;
                    trace.SystemName  = this.systemName ?? EnvironmentConfig.SystemName;

                    //InvokeID
                    trace.InvokeID = request.RequestUri.AbsolutePath;
                    IEnumerable <string> folder;
                    if (actionContext.Request.Headers.TryGetValues(Config.ResponseHeaderFolderKey, out folder))
                    {
                        trace.ServerHost = actionContext.Request.RequestUri.Host + folder.FirstOrDefault();
                    }
                    else
                    {
                        trace.ServerHost = actionContext.Request.RequestUri.Host;
                    }

                    //SearchKey
                    var searchKey = reqModel as ISearchKey;
                    if (searchKey != null)
                    {
                        trace.SearchKey = searchKey.GetSearchKey();
                    }

                    TraceExtensionOnActionExecuting(actionContext, trace);

                    //srs.InvokeID = string.Format("{0}_{1}", actionContext.ControllerContext.ControllerDescriptor.ControllerName.ToLower(), actionContext.ActionDescriptor.ActionName.ToLower());

                    //if (actionContext.ActionArguments != null && actionContext.ActionArguments.Count > 0)
                    //{
                    //    srs.Extension.Add(Config.ParamsKey, actionContext.ActionArguments);
                    //}

                    //ServerRS Log Data TODO

                    Util.TracingContextHelper.SetContextItem(Config.ServerRSKey, trace);
                }
            }

            base.OnActionExecuting(actionContext);
        }