/// <summary>
/// 出错处理:写日志,导航到公共出错页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Application_Error(object sender, EventArgs e)
{
if (Server.GetLastError() == null) return;
Exception ex = Server.GetLastError().GetBaseException();
string error = this.DealException(ex);
DotNet.Common.Util.Logger.WriteFileLog(error, HttpContext.Current.Request.PhysicalApplicationPath + "LogFile");
if (ex.InnerException != null)
{
error = this.DealException(ex);
DotNet.Common.Util.Logger.WriteFileLog(error, HttpContext.Current.Request.PhysicalApplicationPath + "LogFile");
}
this.Server.ClearError();
this.Response.Redirect("/Error.aspx");
}
/// <summary>
/// 处理异常,用来将主要异常信息写入文本日志
/// </summary>
/// <param name="ex"></param>
/// <returns></returns>
private string DealException(Exception ex)
{
this.Application["StackTrace"] = ex.StackTrace;
this.Application["MessageError"] = ex.Message;
this.Application["SourceError"] = ex.Source;
this.Application["TargetSite"] = ex.TargetSite.ToString();
string error = string.Format("URl:{0}n引发异常的方法:{1}n错误信息:{2}n错误堆栈:{3}n",
this.Request.RawUrl, ex.TargetSite, ex.Message, ex.StackTrace);
return error;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
namespace DotNet.Common.WebForm
{
using DotNet.Common.Util;
/// <summary>
/// 通用未捕获异常处理
/// </summary>
public class AspNetUnhandledExceptionModule : IHttpModule
{
static object syncObj = new object();
static bool isInit = false;
public AspNetUnhandledExceptionModule()
{
}
#region IHttpModule Methods
public void Init(HttpApplication context)
{
lock (syncObj)
{
if (!isInit)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
isInit = true;
}
}
}
public void Dispose()
{
}
#endregion
#region OnUnhandledException
void OnUnhandledException(object o, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject == null) return;
Exception ex = e.ExceptionObject as Exception;
string error = string.Format("引发异常的方法:{0}n错误信息:{1}n错误堆栈:{2}n",
ex.TargetSite, ex.Message, ex.StackTrace);
Logger.WriteFileLog(error, AppDomain.CurrentDomain.BaseDirectory + "LogFile");
}
#endregion
}
}
<httpModules> <add name="AspNetUnhandledExceptionModule" type="DotNet.Common.WebForm.AspNetUnhandledExceptionModule, DotNet.Common.WebForm"></add> </httpModules>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(Test), null);
}
}
protected void Test(object state)
{
int[] numArr = new int[100];
numArr[100] = 100; //异常
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有