Ejemplo n.º 1
0
        /// <exclude />
        public static void Application_Error(object sender, EventArgs e)
        {
            var       httpApplication = (sender as HttpApplication);
            Exception exception       = httpApplication.Server.GetLastError();

            var eventType = TraceEventType.Error;

            var httpContext = httpApplication.Context;

            if (httpContext != null)
            {
                bool is404 = (exception is HttpException && ((HttpException)exception).GetHttpCode() == 404);

                if (is404)
                {
                    string rawUrl = httpContext.Request.RawUrl;

                    if (!UrlUtils.IsAdminConsoleRequest(rawUrl))
                    {
                        string customPageNotFoundUrl = HostnameBindingsFacade.GetCustomPageNotFoundUrl();

                        if (!customPageNotFoundUrl.IsNullOrEmpty())
                        {
                            if (rawUrl == customPageNotFoundUrl)
                            {
                                throw new HttpException(500, "'Page not found' url isn't handled. Url: '{0}'".FormatWith(rawUrl));
                            }

                            httpContext.Server.ClearError();
                            httpContext.Response.Clear();

                            httpContext.Response.Redirect(customPageNotFoundUrl, true);

                            return;
                        }

                        eventType = TraceEventType.Verbose;
                    }
                }

                // Logging request url
                if (LogApplicationLevelErrors)
                {
                    HttpRequest request = null;

                    try
                    {
                        request = httpContext.Request;
                    }
                    catch
                    {
                        // Request may not be available at this point
                    }

                    if (request != null)
                    {
                        LoggingService.LogEntry("Application Error",
                                                "Failed to process '{0}' request to url '{1}'"
                                                .FormatWith(request.RequestType, request.RawUrl),
                                                LoggingService.Category.General, eventType);
                    }
                }
            }

            if (LogApplicationLevelErrors)
            {
                while (exception != null)
                {
                    LoggingService.LogEntry("Application Error", exception.ToString(), LoggingService.Category.General, eventType);
                    exception = exception.InnerException;
                }
            }
        }