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

            HttpRequest request = context.Request;

            try
            {
                HttpMethod    = request.RequestType;
                UserIPAddress = request.UserHostAddress;
                UserAgent     = request.UserAgent;

                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
            {
                // ignored
            }


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

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

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

                if (request.Headers != null && Config.CaptureErrorHeaders)
                {
                    if (Config.ErrorHeaderBadKeys == null)
                    {
                        Config.ErrorHeaderBadKeys = new List <string>();
                    }

                    if (!Config.ErrorHeaderBadKeys.Contains("cookie"))
                    {
                        Config.ErrorHeaderBadKeys.Add("cookie");
                    }

                    if (!Config.ErrorHeaderBadKeys.Contains("authorization"))
                    {
                        Config.ErrorHeaderBadKeys.Add("authorization");
                    }

                    Headers = ToKeyValues(request.Headers, Config.ErrorHeaderGoodKeys, Config.ErrorHeaderBadKeys);
                }

                if (request.Cookies != null && Config.CaptureErrorCookies)
                {
                    Cookies = ToKeyValues(request.Cookies, Config.ErrorCookiesGoodKeys, Config.ErrorCookiesBadKeys);
                }

                if (request.Form != null && Config.CaptureErrorPostdata)
                {
                    PostData = ToKeyValues(request.Form, null, null);
                }

                if (context.Session != null && Config.CaptureSessionVariables && Config.ErrorSessionGoodKeys.Any())
                {
                    SessionData = ToKeyValues(context.Session, Config.ErrorSessionGoodKeys, null);
                }

                if (Config.CaptureErrorPostdata)
                {
                    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
            {
                // ignored
            }
        }
Ejemplo n.º 2
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
                {
                    if (string.IsNullOrEmpty(msg.Th))
                    {
                        msg.Th = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString();
                    }
                }
                catch
                {
                    // ignore
                }

#if NETFULL
                try
                {
                    if (string.IsNullOrEmpty(msg.TransID))
                    {
                        var 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
                        var 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.Handler != null)
                        {
                            msg.TransID = System.Web.HttpContext.Current.Request.GetHashCode().ToString();
                        }
                    }
                }
                catch (System.Web.HttpException ex)
                {
                    StackifyAPILogger.Log("Request not available \r\n" + ex.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.Handler != null)
                {
                    var context = System.Web.HttpContext.Current;

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

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

                        var route = resolver.GetRoute();

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

                    if (string.IsNullOrEmpty(msg.UrlRoute))
                    {
                        if (string.IsNullOrWhiteSpace(context.Request.AppRelativeCurrentExecutionFilePath) == false)
                        {
                            HelperFunctions.CleanPartialUrl(context.Request.AppRelativeCurrentExecutionFilePath.TrimStart('~'));
                        }
                    }
                }
#endif

                _MessageBuffer.Enqueue(msg);
            }
            catch (Exception ex)
            {
                StackifyAPILogger.Log("#LogQueue #QueueLogMessage failed", ex);
            }
        }