protected void Application_Error(Object sender, EventArgs e)
{
try
{
Exception exception = HttpContext.Current.Server.GetLastError();
// 如果有异常,则记录异常
if (exception != null)
{
exception = exception.GetBaseException();
log4net.ILog log = log4net.LogManager.GetLogger("ErrorLogFileAppender");
//先注释掉,用于开发调试之用
// 保存异常记录
System.Text.StringBuilder errorMessage = new System.Text.StringBuilder();
errorMessage.Append(Environment.NewLine);
errorMessage.Append("异常消息:");
errorMessage.Append(exception.Message);
errorMessage.Append(Environment.NewLine);
errorMessage.Append("异常用户:");
errorMessage.Append(Environment.NewLine);
errorMessage.Append("异常时间:");
errorMessage.Append(System.DateTime.Now.ToString());
errorMessage.Append(Environment.NewLine);
errorMessage.Append("异常位置:");
errorMessage.Append(Environment.NewLine);
errorMessage.Append(exception.StackTrace);
errorMessage.Append(Environment.NewLine);
OperationLogBL logBl = new OperationLogBL();
OperationLogData data = new OperationLogData();
data.OperateDate = DateTime.Now;
data.OperatorId = CurrentUser.Info.UserID;
data.OperatorName = CurrentUser.Info.UserName;
data.OperatorEnName = CurrentUser.Info.UserNameEn;
data.TypeNo = (int)Model.LogType.错误日志;
data.Remark = errorMessage.ToString();
data.ModifiedId = exception.Source;
data.ModifiedItem = Request.UserHostAddress;
data.OldValue = Request.RawUrl.ToString();
logBl.Add(data);
} // end if (exception != null)
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
// 跳转至 [异常提示] 页面
if (ConfigurationSettings.AppSettings["EnableErrorPage"].ToLower() == "true")
{
string url = ConfigurationSettings.AppSettings["NoRightRedirect"];
Response.Redirect(url);
}
}
{
try
{
Exception exception = HttpContext.Current.Server.GetLastError();
// 如果有异常,则记录异常
if (exception != null)
{
exception = exception.GetBaseException();
log4net.ILog log = log4net.LogManager.GetLogger("ErrorLogFileAppender");
//先注释掉,用于开发调试之用
// 保存异常记录
System.Text.StringBuilder errorMessage = new System.Text.StringBuilder();
errorMessage.Append(Environment.NewLine);
errorMessage.Append("异常消息:");
errorMessage.Append(exception.Message);
errorMessage.Append(Environment.NewLine);
errorMessage.Append("异常用户:");
errorMessage.Append(Environment.NewLine);
errorMessage.Append("异常时间:");
errorMessage.Append(System.DateTime.Now.ToString());
errorMessage.Append(Environment.NewLine);
errorMessage.Append("异常位置:");
errorMessage.Append(Environment.NewLine);
errorMessage.Append(exception.StackTrace);
errorMessage.Append(Environment.NewLine);
OperationLogBL logBl = new OperationLogBL();
OperationLogData data = new OperationLogData();
data.OperateDate = DateTime.Now;
data.OperatorId = CurrentUser.Info.UserID;
data.OperatorName = CurrentUser.Info.UserName;
data.OperatorEnName = CurrentUser.Info.UserNameEn;
data.TypeNo = (int)Model.LogType.错误日志;
data.Remark = errorMessage.ToString();
data.ModifiedId = exception.Source;
data.ModifiedItem = Request.UserHostAddress;
data.OldValue = Request.RawUrl.ToString();
logBl.Add(data);
} // end if (exception != null)
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
// 跳转至 [异常提示] 页面
if (ConfigurationSettings.AppSettings["EnableErrorPage"].ToLower() == "true")
{
string url = ConfigurationSettings.AppSettings["NoRightRedirect"];
Response.Redirect(url);
}
}
net的错误机制处理:
。<customErrors> 配置节支持内部 <error> 标记,该标记将 HTTP 状态代码与自定义错误页关联。例如:
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="/genericerror.htm"> <error statusCode="500" redirect="/error/callsupport.htm"/> <error statusCode="404" redirect="/error/notfound.aspx"/>
<error statusCode="403" redirect="/error/noaccess.aspx"/> </customErrors> </system.web></configuration>mode
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="/genericerror.htm"> <error statusCode="500" redirect="/error/callsupport.htm"/> <error statusCode="404" redirect="/error/notfound.aspx"/>
<error statusCode="403" redirect="/error/noaccess.aspx"/> </customErrors> </system.web></configuration>mode
mode:指示自定义错误是启用、禁用还是只向远程计算机显示。值:On、Off、RemoteOnly(默认值)。