code_junkie

ASP.NET custom error page - Server.GetLastError() is null

Nov 12th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. <customErrors mode="On" defaultRedirect="~/errors/GeneralError.aspx"
  2. />
  3.  
  4. Exception ex = Server.GetLastError();
  5. if (ex != null)
  6. {
  7. if (ex.GetBaseException() != null)
  8. ex = ex.GetBaseException();
  9. }
  10.  
  11. <customErrors mode="RemoteOnly" defaultRedirect="~/errors/GeneralError.aspx" redirectMode="ResponseRewrite" />
  12.  
  13. void Application_Error(object sender, EventArgs e)
  14. {
  15. // Code that runs when an unhandled error occurs
  16. Exception ex = Server.GetLastError();
  17. Application["TheException"] = ex; //store the error for later
  18. Server.ClearError(); //clear the error so we can continue onwards
  19. Response.Redirect("~/myErrorPage.aspx"); //direct user to error page
  20. }
  21.  
  22. <system.web>
  23. <customErrors mode="RemoteOnly" defaultRedirect="~/myErrorPage.aspx">
  24. </customErrors>
  25. </system.web>
  26.  
  27. protected void Page_Load(object sender, EventArgs e)
  28. {
  29.  
  30. // ... do stuff ...
  31. //we caught an exception in our Global.asax, do stuff with it.
  32. Exception caughtException = (Exception)Application["TheException"];
  33. //... do stuff ...
  34. }
  35.  
  36. //direct user to error page
  37. Server.Transfer("~/ErrorPages/Oops.aspx");
  38. }
  39.  
  40. protected void LoadError(Exception objError)
  41. {
  42. if (objError != null)
  43. {
  44. StringBuilder lasterror = new StringBuilder();
  45.  
  46. if (objError.Message != null)
  47. {
  48. lasterror.AppendLine("Message:");
  49. lasterror.AppendLine(objError.Message);
  50. lasterror.AppendLine();
  51. }
  52.  
  53. if (objError.InnerException != null)
  54. {
  55. lasterror.AppendLine("InnerException:");
  56. lasterror.AppendLine(objError.InnerException.ToString());
  57. lasterror.AppendLine();
  58. }
  59.  
  60. if (objError.Source != null)
  61. {
  62. lasterror.AppendLine("Source:");
  63. lasterror.AppendLine(objError.Source);
  64. lasterror.AppendLine();
  65. }
  66.  
  67. if (objError.StackTrace != null)
  68. {
  69. lasterror.AppendLine("StackTrace:");
  70. lasterror.AppendLine(objError.StackTrace);
  71. lasterror.AppendLine();
  72. }
  73.  
  74. ViewState.Add("LastError", lasterror.ToString());
  75. }
  76. }
  77.  
  78. protected void btnReportError_Click(object sender, EventArgs e)
  79. {
  80. SendEmail();
  81. }
  82.  
  83. public void SendEmail()
  84. {
  85. try
  86. {
  87. MailMessage msg = new MailMessage("webteam", "webteam");
  88. StringBuilder body = new StringBuilder();
  89.  
  90. body.AppendLine("An unexcepted error has occurred.");
  91. body.AppendLine();
  92.  
  93. body.AppendLine(ViewState["LastError"].ToString());
  94.  
  95. msg.Subject = "Error";
  96. msg.Body = body.ToString();
  97. msg.IsBodyHtml = false;
  98.  
  99. SmtpClient smtp = new SmtpClient("exchangeserver");
  100. smtp.Send(msg);
  101. }
  102.  
  103. catch (Exception ex)
  104. {
  105. lblException.Text = ex.Message;
  106. }
  107. }
Add Comment
Please, Sign In to add comment