I'd like to simply get notified for site errors, and I've this straight forward code that does work everywhere in the application except in global.asax!!
void Application_Error(object sender, EventArgs e)
{
UIHelper.SendErrorMessage(Server.GetLastError());
}
code in another project:
public static void SendErrorMessage(Exception ex)
{
MailMessage errorMessage = new MailMessage();
errorMessage.From = new MailAddress("xxxxxx@gmail.com", "Site Errors");
errorMessage.Subject = "Error in sending overseas user";
errorMessage.CC.Add(new MailAddress("xxxxxxx@gmail.com", "xxxxx")); //test only
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Source:{0}\n", ex.Source);
sb.AppendFormat("Message:{0}\n", ex.Message);
sb.AppendFormat("Stack Trace:{0}\n", ex.StackTrace);
sb.AppendFormat("Inner Exception:{0}\n", ex.InnerException == null ? string.Empty : ex.InnerException.Message);
errorMessage.Body = sb.ToString();
errorMessage.IsBodyHtml = false;
var smtp = new SmtpClient();
try
{
smtp.Send(errorMessage);
}
catch
{
}
}
Update: I've wrote logic to write the exception to a file in a folder in my website, and I tried that logic in my dev box and it worked, but it never works in the Host!! this is drives me crazy! Email logic works in my box, file logging works in my box but none of them works in production?! this is really strange... any thoughts?