AppfailReporting.Model.FailOccurrenceFactory.FromException C# (CSharp) Method

FromException() static private method

static private FromException ( System.Web.HttpContextBase httpContext, Exception e ) : FailOccurrenceDto
httpContext System.Web.HttpContextBase
e System.Exception
return FailOccurrenceDto
        internal static FailOccurrenceDto FromException(HttpContextBase httpContext, Exception e)
        {
            var baseException = e.GetBaseException();

            var request = httpContext.Request;
            var urlReferrer = request.UrlReferrer != null ? request.UrlReferrer.ToString() : null;

            var requestUrl = request.Url != null ? request.Url.ToString() : null;
            
            string user = null;

            if (ConfigurationModel.Instance.ReportCurrentUsername)
            {
                if (ConfigurationModel.Instance.PopulateUsernameFrom != null)
                {
                    user = ConfigurationModel.Instance.PopulateUsernameFrom();
                }
                else if (httpContext.User != null && httpContext.User.Identity != null)
                {
                    user = httpContext.User.Identity.Name;
                }
            }
            
            string machineName = GetMachineName(httpContext);

            // Filter query & post value pairs according to locally defined rules
            string[][] postValuePairs = null;
            string[][] queryValuePairs = null;
            string[][] serverVariables = null;
            string[][] cookies = null;

            try
            {
                queryValuePairs = CreateCopy(request.QueryString, x => Appfail.IsPostFiltered(x));
                postValuePairs = CreateCopy(request.Form, x => Appfail.IsPostFiltered(x));
                serverVariables = CreateCopy(request.ServerVariables, x => Appfail.IsServerVariableFiltered(x));
                cookies = CreateCopy(request.Cookies, x => Appfail.IsCookieFiltered(x));
            }
            catch (HttpRequestValidationException)
            {}

            var httpException = e as HttpException;
            
            // Report all exceptions, starting from the top and moving toward the base exception
            var allExceptions = new List<ExceptionDto>();

            var nextException = e;

            while (nextException != null)
            {
                allExceptions.Insert(0, new ExceptionDto(nextException.StackTrace, nextException.Message, nextException.GetType().FullName));
                nextException = nextException.InnerException;
            }

            var report = new FailOccurrenceDto(allExceptions.ToArray(),
                                            requestUrl,
                                            request.HttpMethod,
                                            urlReferrer,
                                            DateTime.UtcNow,
                                            user,
                                            postValuePairs,
                                            queryValuePairs,
                                            request.UserAgent,
                                            serverVariables,
                                            cookies,
                                            machineName,
                                            
                                            httpException != null ? httpException.GetHttpCode() : (int?)null);

            return report;
        }
    }