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

            var eventType = TraceEventType.Error;

            var httpContext = httpApplication.Context;

            if (httpContext != null)
            {
                bool is404 = exception is HttpException httpException &&
                             httpException.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: '{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 '{request.RequestType}' request to url '{request.RawUrl}'",
                                                LoggingService.Category.General, eventType);
                    }
                }
            }

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