/// <summary>
        /// 根据异常,调整Response的Status和StatusDescription
        /// </summary>
        private static void ChangeResponseStatus()
        {
            if (HttpContext.Current.Error != null)
            {
                HttpContext.Current.Response.StatusCode = ApplicationErrorLogSection.GetSection().HttpStatusCode;
                //HttpContext.Current.Response.SubStatusCode = 1024;

                //HttpContext.Current.Response.StatusDescription = HttpContext.Current.Error.Message;
            }
        }
示例#2
0
        public static bool TryWriteAppLog(this Exception ex, string detail)
        {
            bool result = false;

            if (ex != null)
            {
                LogEntity logEntity = new LogEntity(ex);

                logEntity.LogEventType = ApplicationErrorLogSection.GetSection().GetExceptionLogEventType(ex);

                logEntity.EventID    = ApplicationErrorEventID;
                logEntity.StackTrace = detail;

                HttpContext context = HttpContext.Current;

                try
                {
                    if (context != null)
                    {
                        string[] paths = context.Request.ApplicationPath.Split('/');

                        logEntity.Source = paths[paths.Length - 1];

                        logEntity.Title = string.Format("{0}应用页面错误", context.Request.ApplicationPath);
                        logEntity.ExtendedProperties.Add("RequestUrl", context.Request.Url.AbsoluteUri);
                        logEntity.ExtendedProperties.Add("UserHostAddress", context.Request.UserHostAddress);

                        if (HttpContext.Current.User != null)
                        {
                            logEntity.ExtendedProperties.Add("UserLogOnName", HttpContext.Current.User.Identity.Name);
                        }
                    }
                }
                catch
                {
                }

                result = TryWriteAppLog(logEntity);
            }

            return(result);
        }
示例#3
0
        /// <summary>
        /// 是否允许向客户端输出异常详细信息
        /// </summary>
        public static bool AllowResponseExceptionStackTrace()
        {
            bool result = true;

            OutputStackTraceMode mode = ApplicationErrorLogSection.GetSection().OutputStackTrace;

            switch (mode)
            {
            case OutputStackTraceMode.ByCompilationMode:
                result = IsWebApplicationCompilationDebug();
                break;

            case OutputStackTraceMode.True:
                result = true;
                break;

            case OutputStackTraceMode.False:
                result = false;
                break;
            }

            return(result);
        }
        private void page_PreRender(object sender, EventArgs e)
        {
            Page page = (Page)sender;

            page.ClientScript.RegisterClientScriptBlock(this.GetType(),
                                                        "notifyMailAddress",
                                                        string.Format("var mseeageNotifyMailAddress = \"{0}\";",
                                                                      WebUtility.CheckScriptString(ApplicationErrorLogSection.GetSection().NotifyMailAddress)),
                                                        true);
        }
        private static void ResponseErrorMessage(string strErrorMsg, string strStackTrace)
        {
            string errorFormat = ResourceHelper.LoadStringFromResource(Assembly.GetExecutingAssembly(),
                                                                       "MCS.Web.Library.HttpModules.ApplicationErrorModuleTemplate.htm");

            Page   page     = HttpContext.Current.CurrentHandler is Page ? (Page)HttpContext.Current.CurrentHandler : new Page();
            string imageUrl = ApplicationErrorLogSection.GetSection().LogoImage;

            if (imageUrl.IsNullOrEmpty())
            {
                imageUrl = page.ClientScript.GetWebResourceUrl(typeof(ApplicationErrorModule),
                                                               "MCS.Web.Library.HttpModules." +
                                                               Translator.Translate(Define.DefaultCategory, "ApplicationErrorModuleTemplate_stopLogo.gif"));
            }

            string goBackBtnDisplay = HttpContext.Current.Request.UrlReferrer != null ? "inline" : "none";
            string closePromptValue = HttpContext.Current.Request.UrlReferrer != null ? "true" : "false";

            string[] strArray = errorFormat.Split('$');
            for (int i = 0; i < strArray.Length; i++)
            {
                switch (strArray[i])
                {
                case "globalStyle":
                    if (ApplicationErrorLogSection.GetSection().GlobalStyle.IsNotEmpty())
                    {
                        strArray[i] = string.Format("<link href=\"{0}\" type=\"text/css\" rel=\"stylesheet\" />",
                                                    ApplicationErrorLogSection.GetSection().GlobalStyle);
                    }
                    else
                    {
                        strArray[i] = string.Empty;
                    }
                    break;

                case "imageUrl":
                    strArray[i] = imageUrl;
                    break;

                case "goBackBtnDisplay":
                    strArray[i] = goBackBtnDisplay;
                    break;

                case "closePromptValue":
                    strArray[i] = closePromptValue;
                    break;

                case "copyHint":
                    strArray[i] = HttpUtility.HtmlAttributeEncode(Translator.Translate(Define.DefaultCategory, "复制信息"));
                    break;

                case "copyCompletedPrompt":
                    strArray[i] = HttpUtility.HtmlAttributeEncode(Translator.Translate(Define.DefaultCategory, "复制信息完成"));
                    break;

                case "errorMessage":
                    strArray[i] = HttpUtility.HtmlEncode(strErrorMsg);
                    break;

                case "errorStackTrace":
                    string errorMessage = string.Empty;

                    if (WebUtility.AllowResponseExceptionStackTrace())
                    {
                        errorMessage += "\r\n" + strStackTrace;
                    }

                    strArray[i] = errorMessage.Replace("\r\n", "<br/>");
                    break;

                case "clickToDetail":
                    strArray[i] = Translator.Translate(Define.DefaultCategory, "点击此处展开详细信息......");
                    break;

                case "clickToCloseDetail":
                    strArray[i] = Translator.Translate(Define.DefaultCategory, "点击此处关闭详细信息......");
                    break;

                case "ConfirmClose":
                    strArray[i] = Translator.Translate(Define.DefaultCategory, "您确认要关闭窗口吗?");
                    break;

                case "errorMessageHead":
                    strArray[i] = Translator.Translate(Define.DefaultCategory, "错误提示");
                    break;

                case "returnBtn":
                    strArray[i] = Translator.Translate(Define.DefaultCategory, "返回");
                    break;

                case "closeBtn":
                    strArray[i] = Translator.Translate(Define.DefaultCategory, "关闭");
                    break;

                case "notifyMailAddress":
                    string mailAddress = ApplicationErrorLogSection.GetSection().NotifyMailAddress;

                    if (mailAddress.IsNotEmpty())
                    {
                        strArray[i] = HttpUtility.HtmlAttributeEncode(string.Format(@"mailto://{0}", mailAddress));
                    }
                    else
                    {
                        strArray[i] = "#";
                    }

                    break;
                }
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(string.Join("", strArray));
            HttpContext.Current.Response.End();
        }