构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(13)-系统日志和异常的处理③
2013-11-22 22:11
417 查看
系列目录
上一节我们讲了如何捕获异常和记录日志,这一节我们讲,没有捕获的或者忘记捕获的异常包括404错误等,我们统一处理这个异常。
这一讲是利用 Application_Error 捕获所有异常,全局的异常处理为了减少代码,统一异常处理,Application_Error位于Global.asax里面,
protected void Application_Error(object sender, EventArgs e)
当一个异常在调用堆栈中没有被处理,也没有被框架代码处理时,我们说这个异常未处理,它将被ASP.NET捕获
它将捕获所有 Application 级别的 UnhandleException 和 HttpException(比如:访问的页面不存在等)
总之,在这里处理的话,那么在页面中的所有 try/catch 处理都可以不要了,但是我们为了记录日志,在BLL层还是要try catch
对此未处理错误的处理方法是显示一个页面,列出该未处理异常的详细情况。
我们通过 Application_Error事件把错误写进对应的文件里面或者数据库中。
嘿嘿,我创造了一个错误 Convert.ToInt16("dddd");下面是错误的显示页面
Error.cshtml
由于系统是后台系统,我并没有做得很漂亮的错误页面(实际很丑),大家发货你的想象力和美工能力,造一个好看的,记得共享给我,有奖
上一节我们讲了如何捕获异常和记录日志,这一节我们讲,没有捕获的或者忘记捕获的异常包括404错误等,我们统一处理这个异常。
这一讲是利用 Application_Error 捕获所有异常,全局的异常处理为了减少代码,统一异常处理,Application_Error位于Global.asax里面,
protected void Application_Error(object sender, EventArgs e)
当一个异常在调用堆栈中没有被处理,也没有被框架代码处理时,我们说这个异常未处理,它将被ASP.NET捕获
它将捕获所有 Application 级别的 UnhandleException 和 HttpException(比如:访问的页面不存在等)
总之,在这里处理的话,那么在页面中的所有 try/catch 处理都可以不要了,但是我们为了记录日志,在BLL层还是要try catch
对此未处理错误的处理方法是显示一个页面,列出该未处理异常的详细情况。
我们通过 Application_Error事件把错误写进对应的文件里面或者数据库中。
/// <summary> /// 全局的异常处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Application_Error(object sender, EventArgs e) { string s = HttpContext.Current.Request.Url.ToString(); HttpServerUtility server = HttpContext.Current.Server; if (server.GetLastError() != null) { Exception lastError = server.GetLastError(); // 此处进行异常记录,可以记录到数据库或文本,也可以使用其他日志记录组件。 ExceptionHander.WriteException(lastError); Application["LastError"] = lastError; int statusCode = HttpContext.Current.Response.StatusCode; string exceptionOperator = "/SysException/Error"; try { if (!String.IsNullOrEmpty(exceptionOperator)) { exceptionOperator = new System.Web.UI.Control().ResolveUrl(exceptionOperator); string url = string.Format("{0}?ErrorUrl={1}", exceptionOperator, server.UrlEncode(s)); string script = String.Format("<script language='javascript' type='text/javascript'>window.top.location='{0}';</script>", url); Response.Write(script); Response.End(); } } catch { } } }
嘿嘿,我创造了一个错误 Convert.ToInt16("dddd");下面是错误的显示页面
@model App.Admin.Controllers.BaseException @{ ViewBag.Title = "异常处理页面"; Layout = "~/Views/Shared/_Index_Layout.cshtml"; } <h2>系统错误</h2> <div style="text-align:center;"> <table width="100%" class="blueTab" border="0" cellspacing="1" cellpadding="1"> <tr> <td colspan="3"> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td > 错误处理页面</td> </tr> </tbody> </table> </td> </tr> <tr id="youhaotishi" > <td colspan="2" align="left"> 欢迎您光临本网站!网站运行发生错误,请与管理员联系。错误原因可能如下: <br /> 1.非法访问页面. <br /> 2.您输入的数据错误. <br /> 3.您访问的页面不存在. <br /> 4.内容不存在,或已被删除. <br /> 5.系统忙,请稍候再试. </td> </tr> <tbody id="detailInformation" style="display: none;"> <tr> <td width="20%" class="alignRight" nowrap> <strong>出错页面:</strong> </td> <td class="alignLeft"> @Html.DisplayFor(model => model.ErrorPageUrl) </td> </tr> <tr> <td class="alignRight" nowrap> <strong>异常名称:</strong> </td> <td class="alignLeft"> @Html.DisplayFor(model => model.ExceptionName) </td> </tr> <tr> <td class="alignRight" nowrap> <strong>异常信息:</strong> </td> <td class="alignLeft"> @Html.DisplayFor(model => model.ExceptionMessage) </td> </tr> <tr id="trInnerExceptionName" runat="server"> <td class="alignRight" nowrap> <strong>内部异常名称:</strong> </td> <td class="alignLeft"> @Html.DisplayFor(model => model.InnerExceptionName) </td> </tr> <tr id="trInnerExceptionMessage" runat="server"> <td class="alignRight" nowrap> <strong>内部异常信息:</strong> </td> <td class="alignLeft"> @Html.DisplayFor(model => model.InnerExceptionMessage) </td> </tr> <tr id="trExceptionMethod" runat="server"> <td class="alignRight" nowrap> <strong>方法名称:</strong> </td> <td class="alignLeft" style="background-color: #ffffcc;"> @Html.DisplayFor(model => model.TargetSite) </td> </tr> <tr id="trExceptionSource" runat="server"> <td class="alignRight" nowrap> <strong>源文件:</strong> </td> <td class="alignLeft" style="background-color: #ffffcc;"> @Html.DisplayFor(model => model.SourceErrorFile) </td> </tr> <tr id="trExceptionRowId" runat="server"> <td class="alignRight" nowrap> <strong>行号:</strong> </td> <td class="alignLeft" style="background-color: #ffffcc; color: Red"> @Html.DisplayFor(model => model.SourceErrorRowID) </td> </tr> <tr runat="server" id="trStack" visible="false"> <td class="alignRight"> <strong>堆栈跟踪:</strong> </td> <td class="alignLeft" style="background-color: #ffffcc;"> <code> <pre id="litStack"><textarea name="errormsg" cols="80" rows="30" readonly="readonly">@Html.DisplayFor(model => model.StackInfo) </textarea> </pre> </code> </td> </tr> </tbody> </table> <a id="showMessage" href="#" onclick="ShowErrorMessage();return false;">显示详细信息</a> </div> <script type="text/javascript"> var isShowMessage = true; function ShowErrorMessage() { var obj = document.getElementById("showMessage") var detailInformation = document.getElementById("detailInformation"); var youhaotishi = document.getElementById("youhaotishi"); if (isShowMessage) { obj.innerText = "隐藏详细信息"; isShowMessage = false; detailInformation.style.display = "block"; youhaotishi.style.display = "none"; } else { obj.innerText = "显示详细信息"; isShowMessage = true; detailInformation.style.display = "none"; youhaotishi.style.display = "block"; } } </script>
Error.cshtml
由于系统是后台系统,我并没有做得很漂亮的错误页面(实际很丑),大家发货你的想象力和美工能力,造一个好看的,记得共享给我,有奖
相关文章推荐
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(13)-系统日志和异常的处理③
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(12)-系统日志和异常的处理②
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(12)-系统日志和异常的处理②
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(16)-权限管理系统-漂亮的验证码
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(10)-系统菜单栏[附源码]
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(27)-权限管理系统-分配用户给角色
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(28)-系统小结
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(19)-权限管理系统-用户登录
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(28)-系统小结
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(28)-系统小结
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(24)-权限管理系统-将权限授权给角色
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(37)-文章发布系统④-百万级数据和千万级数据简单测试
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(15)-权限管理系统准备
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(37)-文章发布系统④-百万级数据和千万级数据简单测试
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(14)-系统小结
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(34)-文章发布系统①-简要分析
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(15)-权限管理系统准备
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(3)-漂亮系统登陆界面
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(20)-权限管理系统-根据权限获取菜单
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(14)-系统小结