rendered paste body public static string log_unhandled_exception()
{
string code = random_code();
try {
HttpContext c = HttpContext.Current;
string fileName = (System.IO.Path.GetFileName(HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"]));
// get last exception
Exception e = c.Server.GetLastError().GetBaseException();
// save to database
SQLEngineEx eng = new SQLEngineEx("INSERT INTO [Core]..[unhandled_exception_log] (timestamp, err_id, url, source, message, trace, form_data, session_data, cookie_data, remotehost, username, user_id, served_by, page_name) VALUES (GETDATE(), @err_id, @url, @source, @message, @trace, @form_data, @session_data, @cookie_data, @remotehost, @username, @user_id, @served_by, @page_name)", CommandType.Text);
eng.AddParameter("@err_id", code);
eng.AddParameter("@url", c.Request.Url.ToString());
eng.AddParameter("@source", e.Source);
eng.AddParameter("@message", e.Message);
eng.AddParameter("@trace", e.StackTrace);
eng.AddParameter("@form_data", combine_nvc(c.Request.Form));
eng.AddParameter("@session_data", combine_session(c.Session));
eng.AddParameter("@cookie_data", combine_cookies(c.Request.Cookies));
if (c.Request.Headers["X-Forwarded-For"] != null)
eng.AddParameter("@remotehost", c.Request.Headers["X-Forwarded-For"]);
else
eng.AddParameter("@remotehost", c.Request.UserHostAddress);
eng.AddParameter("@username", "not_resolved");
if (c.Session != null && c.Session["userpid"] != null)
eng.AddParameter("@user_id", c.Session["userpid"]);
else
eng.AddParameter("@user_id", 0);
eng.AddParameter("@served_by", c.Server.MachineName);
eng.AddParameter("@page_name", fileName);
eng.ExecuteNonQuery();
} catch {
}
return code;
}
// combine all fields of a name value collection
public static string combine_nvc(NameValueCollection in_nvc)
{
if (in_nvc == null || in_nvc.AllKeys == null) return "";
StringBuilder rstr = new StringBuilder();
foreach (string key in in_nvc.AllKeys)
{
rstr.AppendLine(key.PadRight(20) + in_nvc[key]);
}
return rstr.ToString();
}
// combine all session variables
public static string combine_session(System.Web.SessionState.HttpSessionState session)
{
if (session == null || session.Keys == null) return "";
StringBuilder rstr = new StringBuilder();
foreach (string key in session.Keys)
{
if (session[key] != null)
rstr.AppendLine(key.PadRight(20) + session[key].ToString());
}
return rstr.ToString();
}
// combine all cookie variables
public static string combine_cookies(HttpCookieCollection cookies)
{
if (cookies == null || cookies.AllKeys == null) return "";
StringBuilder rstr = new StringBuilder();
foreach (string key in cookies.AllKeys)
{
rstr.AppendLine(key.PadRight(20) + cookies[key].Value.ToString());
}
return rstr.ToString();
}