示例#1
0
        //private struct ThreadUsage
        //{
        //    public string SrcMethod { get; set; }
        //    public string TransID { get; set; }
        //}
        //ConcurrentDictionary<string, ThreadUsage> _ThreadInfo = new ConcurrentDictionary<string, ThreadUsage>();
        /// <summary>
        /// Should call CanSend() before this. Did not also put that call in here to improve performance. Makes more sense to do it earlier so it can skip other steps up the chain.
        /// </summary>
        /// <param name="msg"></param>
        public void QueueLogMessage(Models.LogMsg msg)
        {
            try
            {
                if (msg == null)
                    return;

                if (!_TimerStarted)
                {
                    EnsureTimer();
                }

                //try
                //{
                //    //Thread # for the OS, not .net
                //    if (string.IsNullOrEmpty(msg.ThOs))
                //    {
                //        msg.ThOs = AppDomain.GetCurrentThreadId().ToString();
                //    }
                //}
                //catch
                //{
                //}

                try
                {

                    if (string.IsNullOrEmpty(msg.Th))
                    {
                        msg.Th = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString();
                    }
                }
                catch
                {
                }

                try
                {
                    if (string.IsNullOrEmpty(msg.TransID))
                    {

                        Object stackifyRequestID = CallContext.LogicalGetData("Stackify-RequestID");

                        if (stackifyRequestID != null)
                        {
                            msg.TransID = stackifyRequestID.ToString();
                        }
                    }

                    if (string.IsNullOrEmpty(msg.TransID))
                    {
                        //gets from Trace.CorrelationManager.ActivityId but doesnt assume it is guid since it technically doesn't have to be
                        //not calling the CorrelationManager method because it blows up if it isn't a guid
                        Object correltionManagerId = CallContext.LogicalGetData("E2ETrace.ActivityID");

                        if (correltionManagerId != null && correltionManagerId is Guid && ((Guid)correltionManagerId) != Guid.Empty)
                        {
                            msg.TransID = correltionManagerId.ToString();
                        }
                    }

                    if (string.IsNullOrEmpty(msg.TransID))
                    {
                        if (_IsWebApp && System.Web.Hosting.HostingEnvironment.IsHosted
                                 && System.Web.HttpContext.Current != null &&
                                 System.Web.HttpContext.Current.Request != null)
                        {
                            msg.TransID = System.Web.HttpContext.Current.Request.GetHashCode().ToString();
                        }

                    }
                }
                catch(Exception ex)
                {
                    StackifyAPILogger.Log("Error figuring out TransID \r\n" + ex.ToString());
                }

                if (_IsWebApp && System.Web.Hosting.HostingEnvironment.IsHosted
                    && System.Web.HttpContext.Current != null &&
                    System.Web.HttpContext.Current.Request != null)
                {
                    var context = System.Web.HttpContext.Current;

                    msg.UrlFull = context.Request.Url.ToString();

                    if (context.Items != null && context.Items.Contains("Stackify.ReportingUrl"))
                    {
                        msg.UrlRoute = context.Items["Stackify.ReportingUrl"].ToString();
                    }
                    else
                    {
                        RouteResolver resolver = new RouteResolver(context);

                        var route = resolver.GetRoute();

                        if (!string.IsNullOrEmpty(route.Action))
                        {
                            msg.UrlRoute = route.ToString();
                        }
                    }

                    if (string.IsNullOrEmpty(msg.UrlRoute))
                    {
                        HelperFunctions.CleanPartialUrl(
                            context.Request.AppRelativeCurrentExecutionFilePath.TrimStart('~'));
                    }

                }

                _MessageBuffer.Enqueue(msg);

            }
            catch
            {

            }
        }
        private void Load(HttpContext context)
        {
            if (context == null || context.Request == null)
                return;

            HttpRequest request = context.Request;
            
            try
            {
                HttpMethod = request.RequestType;
                UserIPAddress = request.UserHostAddress;

                if (context.Items != null && context.Items.Contains("Stackify.ReportingUrl"))
                {
                    ReportingUrl = context.Items["Stackify.ReportingUrl"].ToString();
                }

                //We be nice to detect if it is a web sockets connection and denote that in the protocol field
                //do we care about things like HTTP/1.1 or the port?
                if (request.IsSecureConnection)
                {
                    RequestProtocol = "https";
                }
                else
                {
                    RequestProtocol = "http";
                }

                if (request.Url != null)
                {
                    RequestUrl = request.Url.ToString();
                }

                if (request.AppRelativeCurrentExecutionFilePath != null)
                {
                    RequestUrlRoot = request.AppRelativeCurrentExecutionFilePath.TrimStart('~');
                }

                RouteResolver resolver = new RouteResolver(context);

                var route = resolver.GetRoute();

                MVCArea = route.Area;
                MVCController = route.Controller;
                MVCAction = route.Action;

                if (string.IsNullOrEmpty(ReportingUrl) && route != null && !string.IsNullOrEmpty(route.Action))
                {
                    ReportingUrl = route.ToString();
                }
            }
            catch (Exception)
            {

            }

           

            try
            {
                if (request.QueryString != null)
                {
                    QueryString = ToKeyValues(request.QueryString, false);
                }

                if (request.ServerVariables != null)
                {
                    List<string> badKeys = new List<string>();
                    badKeys.AddRange(new string[] { "all_http", "all_raw", "http_cookie" });

                    var serverVars = ToKeyValues(request.ServerVariables, false, badKeys);
                    foreach (var serverVar in serverVars)
                    {
                        _Error.ServerVariables[serverVar.Key] = serverVar.Value;
                    }
                }

                if (request.Headers != null)
                {
                    List<string> badKeys = new List<string>();
                    badKeys.AddRange(new string[] { "cookie" });

                    Headers = ToKeyValues(request.Headers, false, badKeys);
                }

                if (request.Cookies != null)
                {
                    Cookies = ToKeyValues(request.Cookies, true);

                    //if (Cookies != null)
                    //{
                    //    foreach (var item in Cookies)
                    //    {
                    //        Cookies[item.Key] = "X-MASKED-X";
                    //    }
                    //}

                    //if (Cookies.ContainsKey(".ASPXAUTH"))
                    //{
                    //    Cookies[".ASPXAUTH"] = "X-MASKED-X";
                    //}
                }

                if (request.Form != null)
                {
                    PostData = ToKeyValues(request.Form, true);

                    //if (PostData != null)
                    //{
                    //    foreach (var item in PostData)
                    //    {
                    //        PostData[item.Key] = "X-MASKED-X";
                    //    }
                    //}
                }

                if (context.Session != null)
                {
                    SessionData = ToKeyValues(context.Session, true);
                }

                var contentType = context.Request.Headers["Content-Type"];

                if (contentType != "text/html" && contentType != "application/x-www-form-urlencoded" && context.Request.RequestType != "GET")
                {
                    int length = 4096;
                    string postBody = new StreamReader(context.Request.InputStream).ReadToEnd();
                    if (postBody.Length < length)
                    {
                        length = postBody.Length;
                    }

                    PostDataRaw = postBody.Substring(0, length);
                }
            }
            catch (Exception)
            {
            }
        }