示例#1
0
        protected void Application_Error()
        {
            var exception     = Server.GetLastError();
            var httpException = exception as HttpException;

            Response.Clear();
            Server.ClearError();
            var routeData = new RouteData();

            routeData.Values["controller"] = "Error";
            routeData.Values["action"]     = "Error";
            routeData.Values["exception"]  = exception;
            routeData.Values["errorCode"]  = 500;
            Response.StatusCode            = 500;
            if (httpException != null)
            {
                Response.StatusCode = httpException.GetHttpCode();
                switch (Response.StatusCode)
                {
                case 403:
                    routeData.Values["errorCode"] = 403;
                    break;

                case 404:
                    routeData.Values["errorCode"] = 404;
                    break;

                default:
                    routeData.Values["errorCode"] = Response.StatusCode;
                    break;
                }
                try
                {
                    LogHelper.AddSystemLog(new SystemLog()
                    {
                        Name       = $"ApplicationError - {exception.Message}",
                        Component  = "ApplicationError",
                        CreatedBy  = "System",
                        CreatedOn  = DateTime.Now,
                        StackTrace = exception.StackTrace
                    });
                }
                catch (Exception ex)
                {
                }
            }
            var fromAddress = SettingHelper.GetOrCreate(Constants.SystemSettings.EmailsSourceEmail, "*****@*****.**").Value;
            var toAddress   = SettingHelper.GetOrCreate(Constants.SystemSettings.EmailsErrorEmail, "*****@*****.**").Value;

            new EmailHelper().SendMail(fromAddress, new List <string>()
            {
                toAddress
            }, "General Error", $"Message: {exception.Message}. InnerException: {exception.InnerException}. StackTrace: {exception.StackTrace}");
            IController errorsController = new ErrorController();
            var         rc = new RequestContext(new HttpContextWrapper(Context), routeData);

            errorsController.Execute(rc);
        }
        protected void LogError(string message)
        {
            var url = "http://" + Request.Headers["host"] + Request.RawUrl;

            LogHelper.AddSystemLog(LogType.Error, "Url: " + url + "<br/>" + message);
        }