asp.net错误处理封装

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

None.gif
using System;
None.gif
using System.Data;
None.gif
using System.Configuration;
None.gif
using System.Web;
None.gif
using System.Web.Security;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.WebControls.WebParts;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.Collections;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**//// <summary>
InBlock.gif
/// Summary description for Error
ExpandedBlockEnd.gif
/// </summary>

None.gifpublic class ErrorManager
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
private System.Timers.Timer m_timer;
InBlock.gif    
private Hashtable m_htErr;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 私有的构造函数
ExpandedSubBlockEnd.gif    
/// </summary>

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

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 单例模式的接口
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public static readonly ErrorManager Instance = new ErrorManager();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 设置定时器的频率,单位是毫秒
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <param name="Interval">毫秒</param>

InBlock.gif    public void SetTimerInterval(int Interval)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.m_timer.Interval = Interval;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 定时器开始
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public void TimerStart()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.m_timer.Enabled = true;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 定时器结束
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public void TimerStop()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.m_timer.Enabled = false;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 发生了一个错误,把错误信息保存起来,并返回错误的id,便于页面中读取
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <returns>返回错误的id</returns>

InBlock.gif    public string AddError()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string key = Guid.NewGuid().ToString();
InBlock.gif        
string msg = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
InBlock.gif            
+ HttpContext.Current.Server.GetLastError().GetBaseException().Message;
InBlock.gif        
this.m_htErr.Add(key, msg);
InBlock.gif
InBlock.gif        HttpContext.Current.Server.ClearError();
InBlock.gif
InBlock.gif        
return key;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 返回指定Key的错误信息,前19个字符是错误发生的时间
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="key">key,是一个guid</param>
ExpandedSubBlockEnd.gif    
/// <returns>返回错误信息</returns>

InBlock.gif    public string GetError(string key)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return this.m_htErr[key].ToString();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 定时在Hashtable中清理错误信息
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif    
/// <param name="e"></param>

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

InBlock.gif
InBlock.gif            
foreach (string key in list)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.m_htErr.Remove(key);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
Session操作的封装#region Session操作的封装
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 取得指定键值的Session
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="key">键值</param>
ExpandedSubBlockEnd.gif    
/// <returns>键内容值</returns>

InBlock.gif    public object GetSession(string key)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
object val = HttpContext.Current.Session[key];
InBlock.gif        
if (val == null)
InBlock.gif            
throw new Exception("页面超时,请重新登录。");
InBlock.gif
InBlock.gif        
return val;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 设置Session
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="key">键值</param>
ExpandedSubBlockEnd.gif    
/// <param name="val">键内容</param>

InBlock.gif    public void SetSession(string key, object val)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        HttpContext.Current.Session[key] 
= val;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值