Вы можете отловить ошибку в вашем Global.asax и отправить электронное письмо с исключением.
В Global.asax.cs:
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception ex = Server.GetLastError();
ExceptionHandler.SendExceptionEmail(ex, "Unhandled", this.User.Identity.Name, this.Request.RawUrl);
Response.Redirect("~/ErrorPage.aspx"); // So the user does not see the ASP.net Error Message
}
Мой метод в классе My ExceptionHandler:
class ExceptionHandler
{
public static void SendExceptionEmail(Exception ex, string ErrorLocation, string UserName, string url)
{
SmtpClient mailclient = new SmtpClient();
try
{
string errorMessage = string.Format("User: {0}\r\nURL: {1}\r\n=====================\r\n{2}", UserName, url, AddExceptionText(ex));
mailclient.Send(ConfigurationManager.AppSettings["ErrorFromEmailAddress"],
ConfigurationManager.AppSettings["ErrorEmailAddress"],
ConfigurationManager.AppSettings["ErrorEmailSubject"] + " = " + ErrorLocation,
errorMessage);
}
catch { }
finally { mailclient.Dispose(); }
}
private static string AddExceptionText(Exception ex)
{
string innermessage = string.Empty;
if (ex.InnerException != null)
{
innermessage = string.Format("=======InnerException====== \r\n{0}", ExceptionHandler.AddExceptionText(ex.InnerException));
}
string message = string.Format("Message: {0}\r\nSource: {1}\r\nStack:\r\n{2}\r\n\r\n{3}", ex.Message, ex.Source, ex.StackTrace, innermessage);
return message;
}
}