Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)delv
published in(发表于) 2014/1/10 6:28:23
ASP.NET技巧:错误处理封装_[Asp.Net教程]_0

ASP.NET技巧:错误处理封装_[Asp.Net教程]

/*----------------------------------------------------------------
* Copyright (C)
* 版权所有。
*
* 文件名 :ErrorManager.cs
* 功能描述:asp.net中统一的错误修理,与本类相配套需要增加一个错误信息显示页面,如error.aspx
*
* 使用说明:1. 在Application_Start()中启动定时器(定时清空错误信息):ErrorManager.Instance.Start(),
* 默认12小时运行一次,或用ErrorManager.Instance.SetTimerInterval()设置。
* 2. 在Application_Error()中,当发生错误时,保存这个错误信息并转到error.aspx中显示这个错误
* string key = ErrorManager.Instance.AddError();
* Response.Redirect("error.aspx?key=" + key);
* 3. 在error.aspx中通过url传来的key,取得并显示错误信息:
* string err = ErrorManager.Instance.GetError(key)
* err中前19个字符是错误发生的时间,后面是错误信息。
* 4. 为了捕捉Session超时的错误,而不是返回Session[key]是null的错误信息,本类增加了GetSession()
* 和SetSession函数来统一管理Session,以后aspx中不能直接读取Session,而必须通过本类来读取。
*
*
* 创建标识:
*
* 修改标识:
* 修改描述:
*
* 修改标识:
* 修改描述:
*----------------------------------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;


/**////


/// Summary description for Error
///

public class ErrorManager
{
private System.Timers.Timer m_timer;
private Hashtable m_htErr;


/**////


/// 私有的构造函数
///

private ErrorManager()
{
this.m_timer = new System.Timers.Timer();
this.m_timer.Enabled = false;
this.m_timer.Interval = 12 * 60 * 60 * 1000; //默认12个小时执行一次
this.m_timer.Elapsed += new System.Timers.ElapsedEventHandler(m_timer_Elapsed);
this.m_htErr = new Hashtable();
}


/**////


/// 单例模式的接口
///

public static readonly ErrorManager Instance = new ErrorManager();


/**////


/// 设置定时器的频率,单位是毫秒
///

/// 毫秒
public void SetTimerInterval(int Interval)
{
this.m_timer.Interval = Interval;
}


/**////


/// 定时器开始
///

public void TimerStart()
{
this.m_timer.Enabled = true;
}


/**////


/// 定时器结束
///

public void TimerStop()
{
this.m_timer.Enabled = false;
}


/**////


/// 发生了一个错误,把错误信息保存起来,并返回错误的id,便于页面中读取
///

/// 返回错误的id
public string AddError()
{
string key = Guid.NewGuid().ToString();
string msg = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
+ HttpContext.Current.Server.GetLastError().GetBaseException().Message;
this.m_htErr.Add(key, msg);


HttpContext.Current.Server.ClearError();


return key;
}


/**////


/// 返回指定Key的错误信息,前19个字符是错误发生的时间
///

/// key,是一个guid
/// 返回错误信息
public string GetError(string key)
{
return this.m_htErr[key].ToString();
}


/**////


/// 定时在Hashtable中清理错误信息
///

///
///
private void m_timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
ArrayList list = new ArrayList();
lock (this.m_htErr)
{
DateTime now = DateTime.Now;
TimeSpan ts;
foreach (string key in this.m_htErr.Keys)
{
//前19个字符是错误发生的日期,yyyy-MM-dd HH:mm:ss
string time = this.m_htErr[key].ToString().Substring(0, 19);
ts = now - Convert.ToDateTime(time);
if (ts.TotalMinutes > 20) //把20分钟前的错误信息从hashtable中清除
list.Add(key);
}


foreach (string key in list)
{
this.m_htErr.Remove(key);
}
}


}


Session操作的封装#region Session操作的封装
/**////


/// 取得指定键值的Session
///

/// 键值
/// 键内容值
public object GetSession(string key)
{
object val = HttpContext.Current.Session[key];
if (val == null)
throw new Exception("页面超时,请重新登录。");


return val;
}


/**////


/// 设置Session
///

/// 键值
/// 键内容
public void SetSession(string key, object val)
{
HttpContext.Current.Session[key] = val;
}
#endregion
}


来源:网络







If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759