Пример #1
0
        private void AddContextualInformation(AirbrakeNotice notice, MethodBase catchingMethod, Dictionary<string, string> info = null)
        {
            var component = String.Empty;
            var action = String.Empty;

            if ((notice.Error != null) && (notice.Error.Backtrace != null) && notice.Error.Backtrace.Any())
            {
                // TODO: We should perhaps check whether the topmost back trace is in fact a Controller+Action by performing some sort of heuristic (searching for "Controller" etc.). @asbjornu
                var backtrace = notice.Error.Backtrace.First();
                action = backtrace.Method;
                component = backtrace.File;
            }
            else if (catchingMethod != null)
            {
                action = catchingMethod.Name;

                if (catchingMethod.DeclaringType != null)
                    component = catchingMethod.DeclaringType.FullName;
            }

            var request = new AirbrakeRequest("http://example.com/", component)
            {
                Action = action
            };

            var cgiData = new List<AirbrakeVar>
            {
                new AirbrakeVar("Environment.MachineName", Environment.MachineName),
                new AirbrakeVar("Environment.OSversion", Environment.OSVersion),
                new AirbrakeVar("Environment.Version", Environment.Version)
            };

            var parameters = new List<AirbrakeVar>();
            var session = new List<AirbrakeVar>();
            var httpContext = HttpContext.Current;

            if (httpContext != null)
            {
                var httpRequest = httpContext.Request;
                request.Url = httpRequest.Url.ToString();

                cgiData.AddRange(BuildVars(httpRequest.Headers));
                cgiData.AddRange(BuildVars(httpRequest.Cookies));
                parameters.AddRange(BuildVars(httpRequest.Params));
                session.AddRange(BuildVars(httpContext.Session));

                if (httpContext.User != null)
                    cgiData.Add(new AirbrakeVar("User.Identity.Name", httpContext.User.Identity.Name));

                var browser = httpRequest.Browser;

                if (browser != null)
                {
                    cgiData.Add(new AirbrakeVar("Browser.Browser", browser.Browser));
                    cgiData.Add(new AirbrakeVar("Browser.ClrVersion", browser.ClrVersion));
                    cgiData.Add(new AirbrakeVar("Browser.Cookies", browser.Cookies));
                    cgiData.Add(new AirbrakeVar("Browser.Crawler", browser.Crawler));
                    cgiData.Add(new AirbrakeVar("Browser.EcmaScriptVersion", browser.EcmaScriptVersion));
                    cgiData.Add(new AirbrakeVar("Browser.JavaApplets", browser.JavaApplets));
                    cgiData.Add(new AirbrakeVar("Browser.MajorVersion", browser.MajorVersion));
                    cgiData.Add(new AirbrakeVar("Browser.MinorVersion", browser.MinorVersion));
                    cgiData.Add(new AirbrakeVar("Browser.Platform", browser.Platform));
                    cgiData.Add(new AirbrakeVar("Browser.W3CDomVersion", browser.W3CDomVersion));
                }
            }

            if (info != null)
            {
                foreach (var item in info)
                {
                    if (item.Key.ToLower().StartsWith("environment"))
                    {
                        cgiData.Add(new AirbrakeVar(item.Key, item.Value));
                    }
                    else if (item.Key.ToLower().StartsWith("session"))
                    {
                        session.Add(new AirbrakeVar(item.Key, item.Value));
                    }
                    else
                    {
                        parameters.Add(new AirbrakeVar(item.Key, item.Value));
                    }
                }
            }

            request.CgiData = cgiData.ToArray();
            request.Params = parameters.Any() ? parameters.ToArray() : null;
            request.Session = session.Any() ? session.ToArray() : null;
            notice.Request = request;
        }
        private void AddContextualInformation(AirbrakeNotice notice, MethodBase catchingMethod)
        {
            var component = String.Empty;
            var action = String.Empty;

            if ((notice.Error != null) && (notice.Error.Backtrace != null) && notice.Error.Backtrace.Any())
            {
                // TODO: We should perhaps check whether the topmost back trace is in fact a Controller+Action by performing some sort of heuristic (searching for "Controller" etc.). @asbjornu
                var backtrace = notice.Error.Backtrace.First();
                action = backtrace.Method;
                component = backtrace.File;
            }
            else if (catchingMethod != null)
            {
                action = catchingMethod.Name;

                if (catchingMethod.DeclaringType != null)
                    component = catchingMethod.DeclaringType.FullName;
            }

            var request = new AirbrakeRequest("http://example.com/", component)
            {
                Action = action
            };

            var cgiData = new List<AirbrakeVar>
            {
                new AirbrakeVar("Environment.MachineName", Environment.MachineName),
                new AirbrakeVar("Environment.OSversion", Environment.OSVersion),
                new AirbrakeVar("Environment.Version", Environment.Version)
            };

            var parameters = new List<AirbrakeVar>();
            var session = new List<AirbrakeVar>();

            request.CgiData = cgiData.ToArray();
            request.Params = parameters.Any() ? parameters.ToArray() : null;
            request.Session = session.Any() ? session.ToArray() : null;
            notice.Request = request;
        }