Global.asax file not giving Exception Message
Here's my problem I have some code and I'm using it to send an e-mail with the last error details but all I want is the (Inner)Exception Message to be displayed in the email with the URL
Here's my code
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Get current exception
Dim err As System.Exception = Server.GetLastError
Dim ErrorDetails As String = err.Exception.Message
Dim ErrorURL As String = Request.Url.ToString()
' Send notification e-mail
Dim Email As MailMessage = _
New MailMessag开发者_如何学Goe("email@email.co.uk", email@email.co.uk")
Email.IsBodyHtml = False
Email.Subject = "WEB SITE ERROR"
Email.Body = ErrorDetails & vbcrlf & vbcrlf & ErrorURL
Email.Priority = MailPriority.High
Dim sc As SmtpClient = New SmtpClient("localhost")
sc.Send(Email)
End Sub
Any help would be much appreciated
Thanks
Jamie
Use err.ToString()
- this gives you the full strack trace and inner exceptions.
If you really only want the inner exception error message, use err.InnerException.Message
protected void Application_Error(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
HttpContext ctx = HttpContext.Current;
msg.To.Add(new MailAddress("me@me.com"));
msg.From = new MailAddress("from@me.com");
msg.Subject = "My app had an issue...";
msg.Priority = MailPriority.High;
StringBuilder sb = new StringBuilder();
sb.Append(ctx.Request.Url.ToString() + System.Environment.NewLine);
sb.Append("Source:" + System.Environment.NewLine + ctx.Server.GetLastError().Source.ToString());
sb.Append("Message:" + System.Environment.NewLine + ctx.Server.GetLastError().Message.ToString());
sb.Append("Stack Trace:" + System.Environment.NewLine + ctx.Server.GetLastError().StackTrace.ToString());
msg.Body = sb.ToString();
//CONFIGURE SMTP OBJECT
SmtpClient smtp = new SmtpClient("myhost");
//SEND EMAIL
smtp.Send(msg);
//REDIRECT USER TO ERROR PAGE
Server.Transfer("~/ErrorPage.aspx");
}
精彩评论