Ejemplo n.º 1
0
        private void OnUnhandledApplicationException(object sender, EventArgs e)
        {
            StringBuilder message = new StringBuilder("<html><head><style>" +
                                                      "body, table { font-size: 12px; font-family: Arial, sans-serif; }\r\n" +
                                                      "table tr td { padding: 4px; }\r\n" +
                                                      ".header { font-weight: 900; font-size: 14px; color: #fff; background-color: #2b4e74; }\r\n" +
                                                      ".header2 { font-weight: 900; background-color: #c0c0c0; }\r\n" +
                                                      "</style></head><body><table><tr><td class=\"header\">\r\n\r\nUnhandled Exception logged by LogModule.dll:\r\n\r\nappId=");
            string appId = (string)AppDomain.CurrentDomain.GetData(".appId");

            if (appId != null)
            {
                message.Append(appId);
            }
            message.Append("</td></tr>");
            HttpServerUtility server           = HttpContext.Current.Server;
            Exception         currentException = server.GetLastError();

            if (currentException != null)
            {
                message.AppendFormat(
                    "<tr><td class=\"header2\">TYPE</td></tr><tr><td>{0}</td></tr><tr><td class=\"header2\">REQUEST</td></tr><tr><td>{3}</td></tr><tr><td class=\"header2\">MESSAGE</td></tr><tr><td>{1}</td></tr><tr><td class=\"header2\">STACK TRACE</td></tr><tr><td>{2}</td></tr>",
                    currentException.GetType().FullName,
                    currentException.Message,
                    currentException.StackTrace,
                    HttpContext.Current != null ? HttpContext.Current.Request.FilePath : "n/a");
                server.ClearError();
            }
            message.Append("</table></body></html>");
            HttpContext.Current.Response.Write(message.ToString());
            server.ClearError();
        }
Ejemplo n.º 2
0
        public static void Error(Exception ex = null)
        {
            Exception         error  = ex;
            HttpServerUtility Server = null;

            try
            {
                var server = HttpContext.Current == null ? null : HttpContext.Current.Server;
                error  = ex ?? (server == null ? null : server.GetLastError());
                Server = server;
            }
            catch {; }

            string strError = error == null ? "-" : error.ToString();

            if (Log != null)
            {
                Log.Write(" Error details " + strError
                          + (error == null || error.StackTrace == null ? String.Empty
                            : "\n" + error.StackTrace.ToString())); // .GetBaseException().StackTrace.ToString());
            }
            if (Server == null)
            {
                Debugger.Break();
                return;
            }
            if (error == null || error.InnerException != null)
            {
                Debugger.Break();
            }

            var Request  = HttpContext.Current.Request;
            var Response = HttpContext.Current.Response;

            Log.Write("Global Application_Error: url=" + Request.Url
                      + ": \n ip=" + Request.UserHostAddress + " refer=" + Request.UrlReferrer);

            if (strError.IndexOf(".XPathException") > 0 ||
                strError.IndexOf(".HttpCompileException") > 0 ||
                strError.IndexOf("Could not load") > 0)
            {
                Server.ClearError();
                return;
            }

            Trace.Write("Error " + error.ToString());

            Response.Write("<br/><span class=\"error\"><pre>Xml Error: "
                           + strError.Replace(": error", ":<br/><b> error</b>") + "</pre></span>");
            Server.ClearError();
        }
        internal static bool HandleEFMigrationException(Exception ex, HttpServerUtility server, HttpResponse response, HttpContext context)
        {
            var _service      = new EFMigrationService();
            var isEfException = (
                ex.Message.ToLower().Contains("context has changed") ||
                ex.Message.ToLower().Contains("contains no mapped tables") ||
                (ex is System.Data.Entity.Core.EntityCommandExecutionException && ex.InnerException?.Message?.ToLower()?.Contains("invalid column name") == true) ||
                ex is SqlException
                );

            if (isEfException && !_service.IsLatestVersion())
            {
                server.ClearError();

                var routeData = new RouteData();
                routeData.Values.Add("controller", "EFMigrationsManager");

                if (_service.IsAuthorizedUser())
                {
                    routeData.Values.Add("action", "publish");
                }
                else
                {
                    routeData.Values.Add("action", "DbMaintenance");
                }
                HttpContext.Current.Response.RedirectToRoute(routeData.Values);
                return(true);
            }
            return(false);
        }
Ejemplo n.º 4
0
        public static void HandleLastException(HttpServerUtility httpServerUtility, HttpRequest httpRequest, HttpResponse httpResponse)
        {
            Exception exception = httpServerUtility.GetLastError();

            httpServerUtility.ClearError();

            try
            {
                httpResponse.Clear();
            }
            catch
            {
                LogService.Log(exception, LogType.Fatal);

                return;
            }

            string msg = HandleException(exception, LogType.Fatal).Item1;


            if (httpRequest.IsAjaxRequest())
            {
                var jsonResult = new JsonResultModel(msg, true);

                string response = JsonConvert.SerializeObject(jsonResult);

                httpResponse.Write(response);
            }
            else
            {
                httpResponse.Redirect(string.Format("~/Errors?message={0}", msg));
            }
        }
Ejemplo n.º 5
0
        public void OnError(Object sender, EventArgs e)
        {
            HttpContext       Context  = HttpContext.Current;
            HttpResponse      Response = Context.Response;
            HttpRequest       Request  = Context.Request;
            HttpServerUtility Server   = Context.Server;

            #region 记录错误日志

            Exception ex  = Server.GetLastError();
            String    msg = String.Format("\r\n\tIP:{0}\r\n\t页面:{1}\r\n\t消息:{2}\r\n\t类型:{3}\r\n\t用户:{4}",
                                          Request.UserHostAddress,
                                          Request.Url.AbsoluteUri,
                                          ex.Message,
                                          ex.GetType(),
                                          Request.UserAgent);
            ILog logger = LogManager.GetLogger("Error");
            logger.Debug(msg);

            #endregion

            Server.ClearError();
            Response.Clear();
            Response.StatusCode             = 410;
            Response.ContentType            = "text/html";
            Response.TrySkipIisCustomErrors = true;
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Context.ApplicationInstance.CompleteRequest();
        }
        public static void Execute(HttpServerUtility server, HttpResponse response)
        {
            Exception exc = server.GetLastError();

            if (exc.GetType() == typeof(HttpException))
            {
                if (exc.Message.Contains("NoCatch") || exc.Message.Contains("maxUrlLength"))
                {
                    return;
                }

                server.Transfer("HttpErrorPage.aspx");
            }
            else if (exc.GetType() == typeof(DinazorAuthorizationException))
            {
                RedirectDinazorResult.RedirectWithData(new DinazorResult()
                {
                    Status  = ResultStatus.Unauthorized,
                    Message = exc.Message
                });
            }

            Log.Error(exc);
            server.ClearError();
        }
Ejemplo n.º 7
0
        public static void Notify(bool allowRedirect, string additionalErrorMessage, Exception lastError)
        {
            HttpRequest       rq  = HttpContext.Current.Request;
            HttpResponse      rsp = HttpContext.Current.Response;
            HttpServerUtility sv  = HttpContext.Current.Server;

            // get error details from system
            if (lastError == null)
            {
                lastError = sv.GetLastError();
            }

            // get full error details containing source snippets and pretty stack trace
            var dump = new Logging.DiagnosticData(lastError);

            dump.AdditionalMessage = additionalErrorMessage;
            sv.ClearError();

            // log the error report by sending to twitch or emailing
            bool wasLogged = LogErrorReport(dump);

            // show nice user friendly screen
            // for developers also show full details on screen - show error on screen for requests from beweb office
            if (dump.HttpStatusCode == 404)
            {
                PageNotFound(Web.LocalUrl, dump);
            }
            else
            {
                DisplayErrorPage(dump.HttpStatusCode, null, dump, wasLogged);
            }
        }
        public static void Application_OnError()
        {
            HttpServerUtility Server = HttpContext.Current.Server;
            Exception         ex     = Server.GetLastError();

            if (ex != null)
            {
                while (ex.InnerException != null)
                {
                    ex = ex.InnerException;
                }
                string        sException = ex.GetType().Name;
                StringBuilder sbMessage  = new StringBuilder();
                sbMessage.Append(ex.Message);
                // 03/10/2006 Paul.  .NET 2.0 returns lowercase type names. Use typeof instead.
                if (ex.GetType() == typeof(FileNotFoundException))
                {
                    // We can get this error for forbidden files such as web.config and global.asa.
                    //return ; // Return would work if 404 entry was made in web.config.
                    //Response.Redirect("~/Home/FileNotFound.aspx?aspxerrorpath=" + Server.UrlEncode(Request.Path));
                    sbMessage = new StringBuilder("File Not Found");
                }
                // 03/10/2006 Paul.  .NET 2.0 returns lowercase type names. Use typeof instead.
                else if (ex.GetType() == typeof(HttpException))
                {
                    HttpException exHttp    = (HttpException)ex;
                    int           nHttpCode = exHttp.GetHttpCode();
                    if (nHttpCode == 403)
                    {
                        //return ; // Return would work if 403 entry was made in web.config.
                        //Response.Redirect("~/Home/AccessDenied.aspx?aspxerrorpath=" + Server.UrlEncode(Request.Path));
                        sbMessage = new StringBuilder("Access Denied");
                    }
                    else if (nHttpCode == 404)
                    {
                        //return ; // Return would work if 404 entry was made in web.config.
                        //Response.Redirect("~/Home/FileNotFound.aspx?aspxerrorpath=" + Server.UrlEncode(Request.Path));
                        sbMessage = new StringBuilder("File Not Found");
                    }
                }
                // 03/10/2006 Paul.  .NET 2.0 returns lowercase type names. Use typeof instead.
                else if (ex.GetType() == typeof(HttpCompileException))
                {
                    HttpCompileException    exCompile = (HttpCompileException)ex;
                    CompilerErrorCollection col       = exCompile.Results.Errors;
                    foreach (CompilerError err in col)
                    {
                        sbMessage.Append("  ");
                        sbMessage.Append(err.ErrorText);
                    }
                }
                SplendidError.SystemError(new StackTrace(true).GetFrame(0), sbMessage.ToString());
                Server.ClearError();
                string sQueryString = String.Format("aspxerrorpath={0}&Exception={1}&Message={2}", Server.UrlEncode(HttpContext.Current.Request.Path), sException, Server.UrlEncode(sbMessage.ToString()));
                HttpContext.Current.Response.Redirect("~/Home/ServerError.aspx?" + sQueryString);
            }
        }
Ejemplo n.º 9
0
        public static void Execute(HttpServerUtility server, HttpResponse response)
        {
            Exception exc = server.GetLastError();

            if (exc.GetType() == typeof(HttpException))
            {
                if (exc.Message.Contains("NoCatch") || exc.Message.Contains("maxUrlLength"))
                {
                    return;
                }

                server.Transfer("HttpErrorPage.aspx");
            }
            Log.Error(exc);
            server.ClearError();
        }
 public override void ClearError()
 {
     _httpServerUtility.ClearError();
 }
Ejemplo n.º 11
0
 public override void ClearError()
 {
     _server.ClearError();
 }
        public void Methods_Deny_Unrestricted()
        {
            hsu.ClearError();

            Assert.IsNull(hsu.GetLastError(), "GetLastError");

            Assert.IsNotNull(hsu.HtmlDecode(String.Empty), "HtmlDecode(string)");
            hsu.HtmlDecode(String.Empty, sw);

            Assert.IsNotNull(hsu.HtmlEncode(String.Empty), "HtmlEncode(string)");
            hsu.HtmlEncode(String.Empty, sw);

            try
            {
                Assert.IsNull(hsu.MapPath(String.Empty), "MapPath(string)");
            }
            catch (NullReferenceException)
            {
                // ms 1.x
            }

            try
            {
                hsu.Transfer("/");
            }
            catch (NullReferenceException)
            {
                // ms
            }
            try
            {
                hsu.Transfer("/", true);
            }
            catch (NullReferenceException)
            {
                // ms
            }
            try
            {
                hsu.Transfer("/", false);
            }
            catch (NullReferenceException)
            {
                // ms
            }
#if NET_2_0
            try
            {
                hsu.Transfer((IHttpHandler)null, true);
            }
            catch (NullReferenceException)
            {
                // ms
            }
            try
            {
                hsu.Transfer((IHttpHandler)null, false);
            }
            catch (NullReferenceException)
            {
                // ms
            }
#endif
            try
            {
                Assert.IsNotNull(hsu.UrlDecode(url), "UrlDecode(string)");
            }
            catch (NullReferenceException)
            {
                // ms
            }
            try
            {
                hsu.UrlDecode("http://www.mono-project.com/", sw);
            }
            catch (NullReferenceException)
            {
                // ms
            }

            Assert.IsNotNull(hsu.UrlEncode(String.Empty), "UrlEncode(string)");
            hsu.UrlEncode(String.Empty, sw);

            Assert.IsNotNull(hsu.UrlPathEncode(String.Empty), "UrlPathEncode(string)");
        }