您的位置:首页 > 移动开发 > Unity3D

构建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事件把错误写进对应的文件里面或者数据库中。

/// <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
由于系统是后台系统,我并没有做得很漂亮的错误页面(实际很丑),大家发货你的想象力和美工能力,造一个好看的,记得共享给我,有奖
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐