Asp.net| Errors | error handling | encapsulation | tips
/*----------------------------------------------------------------
* Copyright (C)
* All rights reserved.
*
* FileName: ErrorManager.cs
* Function Description: asp.net in the common error repair, with this class matching need to add an error message display page, such as Error.aspx
*
* Use instructions: 1. Start the timer (timed emptying error message) in Application_Start (): ErrorManager.Instance.Start (),
* The default 12-hour run time, or with ErrorManager.Instance.SetTimerInterval () set.
* 2. In Application_Error (), when an error occurs, save the error message and go to error.aspx to display the error
* String key = ErrorManager.Instance.AddError ();
* Response.Redirect ("error.aspx?key=" + key);
* 3. In Error.aspx, the key is passed through the URL to obtain and display an error message:
* 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 a session timeout error, instead of returning Session[key] is a null error message, this class adds GetSession ()
* and the Setsession function to unify the management session, in the future ASPX cannot read the session directly, but must read through this class.
*
*
* Create identity:
*
* Modify the identity:
* Modify Description:
*
* Modify the identity:
* 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 constructors
</summary>
Private ErrorManager ()
{
This.m_timer = new System.Timers.Timer ();
This.m_timer. Enabled = false;
This.m_timer. Interval = 12 * 60 * 60 * 1000; Default 12-Hour execution time
This.m_timer. Elapsed + = new System.Timers.ElapsedEventHandler (m_timer_elapsed);
This.m_hterr = new Hashtable ();
}
/**////<summary>
Interface for single case mode
</summary>
public static readonly ErrorManager Instance = new ErrorManager ();
/**////<summary>
Set the frequency of the timer, in milliseconds
</summary>
<param name= "Interval" > Ms </param>
public void Settimerinterval (int Interval)
{
This.m_timer. Interval = Interval;
}
/**////<summary>
Timer started
</summary>
public void Timerstart ()
{
This.m_timer. Enabled = true;
}
/**////<summary>
///timer end
///</summary
public void Timerstop ()
{
This.m_timer. Enabled = false;
}
/**////<summary>
An error occurred, save the error message, and return the wrong ID to facilitate reading in 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 for the specified key, with the first 19 characters being the time 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>
Timed to clean up error messages in 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 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//Remove 20-minute error message from Hashtable
List. ADD (key);
}
foreach (string key in list)
{
This.m_htErr.Remove (key);
}
}
}
Encapsulation of the session operation #region the session operation
/**////<summary>
Gets the session for 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 timeout, please login again.") ");
return Val;
}
/**////<summary>
Set 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
}