/*------------------------------------------------ ----------------
* Copyright (C)
* all rights reserved.
*
* File name: ErrorManager.cs
* Function description: Unified error repair in asp.net. To match this category, an error information display page needs to be added, such as error.aspx
*
* Instructions for use: 1. Start the timer in Application_Start() (clear error information regularly): ErrorManager.Instance.Start(),
* By default, it runs every 12 hours, or is set using ErrorManager.Instance.SetTimerInterval().
* 2. In Application_Error(), when an error occurs, save the error information and go to error.aspx to display the error.
* string key = ErrorManager.Instance.AddError();
* Response.Redirect("error.aspx?key=" + key);
* 3. Obtain and display the error message from the key passed through the URL in error.aspx:
* string err = ErrorManager.Instance.GetError(key)
* The first 19 characters in err are the time when the error occurred, followed by the error message.
* 4. In order to catch Session timeout errors instead of returning the error message that Session[key] is null, this class adds GetSession()
* and SetSession function to manage Session uniformly. In the future, Session cannot be read directly in aspx, but must be read through this class.
*
*
*Create logo:
*
*Modify logo:
*Modify description:
*
*Modify logo:
*Modify description:
*------------------------------------------------ ---------------*/
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>
/// Summary description for Error
/// </summary>
public class ErrorManager
{
private System.Timers.Timer m_timer;
private Hashtable m_htErr;
/**//// <summary>
/// Private constructor
/// </summary>
privateErrorManager()
{
this.m_timer = new System.Timers.Timer();
this.m_timer.Enabled = false;
this.m_timer.Interval = 12 * 60 * 60 * 1000; //Executed once every 12 hours by default
this.m_timer.Elapsed += new System.Timers.ElapsedEventHandler(m_timer_Elapsed);
this.m_htErr = new Hashtable();
}
/**//// <summary>
/// Singleton mode interface
/// </summary>
public static readonly ErrorManager Instance = new ErrorManager();
/**//// <summary>
/// Set the frequency of the timer in milliseconds
/// </summary>
/// <param name="Interval">Milliseconds</param>
public void SetTimerInterval(int Interval)
{
this.m_timer.Interval = Interval;
}
/**//// <summary>
/// Timer starts
/// </summary>
public void TimerStart()
{
this.m_timer.Enabled = true;
}
/**//// <summary>
/// Timer ends
/// </summary>
public void TimerStop()
{
this.m_timer.Enabled = false;
}
/**//// <summary>
/// An error occurred. Save the error information and return the error ID for easy reading on the page.
/// </summary>
/// <returns>Return the wrong id</returns>
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;
}
/**//// <summary>
/// Returns the error message of the specified Key. The first 19 characters are the time when the error occurred.
/// </summary>
/// <param name="key">key is a guid</param>
/// <returns>Return error message</returns>
public string GetError(string key)
{
return this.m_htErr[key].ToString();
}
/**//// <summary>
/// Regularly clear error information in the Hashtable
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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)
{
//The first 19 characters are the date when the error occurred, 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) //Clear the error information 20 minutes ago from the hashtable
list.Add(key);
}
foreach (string key in list)
{
this.m_htErr.Remove(key);
}
}
}
Encapsulation of Session operations#region Encapsulation of Session operations
/**//// <summary>
/// Get the Session of the specified key value
/// </summary>
/// <param name="key">Key value</param>
/// <returns>Key content value</returns>
public object GetSession(string key)
{
object val = HttpContext.Current.Session[key];
if (val == null)
throw new Exception("Page timed out, please log in again.");
return val;
}
/**//// <summary>
/// Set up Session
/// </summary>
/// <param name="key">Key value</param>
/// <param name="val">Key content</param>
public void SetSession(string key, object val)
{
HttpContext.Current.Session[key] = val;
}
#endregion
}
http://www.cnblogs.com/81/archive/2006/08/16/478175.html