asp.Netエラー処理クラス+SESSIONクラス
6639 ワード
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>
/// Summary description for Error
/// </summary>
public class ErrorManager
{
private System.Timers.Timer m_timer;
private Hashtable m_htErr;
/**//// <summary>
///
/// </summary>
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();
}
/**//// <summary>
///
/// </summary>
public static readonly ErrorManager Instance = new ErrorManager();
/**//// <summary>
/// ,
/// </summary>
/// <param name="Interval"> </param>
public void SetTimerInterval(int Interval)
{
this.m_timer.Interval = Interval;
}
/**//// <summary>
///
/// </summary>
public void TimerStart()
{
this.m_timer.Enabled = true;
}
/**//// <summary>
///
/// </summary>
public void TimerStop()
{
this.m_timer.Enabled = false;
}
/**//// <summary>
/// , , id,
/// </summary>
/// <returns> 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>
/// Key , 19
/// </summary>
/// <param name="key">key, guid</param>
/// <returns> </returns>
public string GetError(string key)
{
return this.m_htErr[key].ToString();
}
/**//// <summary>
/// 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)
{
// 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
/**//// <summary>
/// Session
/// </summary>
/// <param name="key"> </param>
/// <returns> </returns>
public object GetSession(string key)
{
object val = HttpContext.Current.Session[key];
if (val == null)
throw new Exception(" , 。");
return val;
}
/**//// <summary>
/// Session
/// </summary>
/// <param name="key"> </param>
/// <param name="val"> </param>
public void SetSession(string key, object val)
{
HttpContext.Current.Session[key] = val;
}
#endregion
}