Asp.net: unexpected Error-Handling - send mail and redirect to error.aspx?
in my very big webpage (aroung 50 pages, 20.000 line code) I have only a very few situations in which unexpected errors are possible, which I don't catch correctly.
But for my safty: Is it possible, to send me an EMail (SMTP-Class is allready there, so I only must call a Method) and Redirect every unexpacted开发者_StackOverflow error to an specific site? (e.g.: Error.aspx)
In you Global.asax create an Application_Error method and handle errors there. You can get the last exception using Server.GetLastError()
Yes you can use a custom error page that catches any unhandled errors. You can write code on that page to send you an error containing the exception details.
Custom Error page
I believe a better way would be to write you own exception module and handler. Something in this fashion.
public sealed class ErrorCapture : IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
//nothing here
}
public void Init(HttpApplication context)
{
context.Error += new EventHandler(context_Error);
}
void context_Error(object sender, EventArgs e)
{
// Do your implementation like send mail
// Get Required details and use response.write for
// a good error msg display
HttpApplication Application = (HttpApplication)sender;
HttpServerUtility Server = Application.Server;
HttpResponse Response = Application.Response;
Exception error = Server.GetLastError();
if (error.GetType().Equals(typeof(HttpUnhandledException)))
error = error.InnerException;
//your stuff
Server.ClearError();//Do this
}
#endregion
}
Use customErrors for redirecting to the error page.
For sending a mail with a custom class, I've advise you to write a custom asp.net healthmonitoring provider. Have a look at this article: http://www.tomot.de/en-us/article/6/asp.net/how-to-create-a-custom-healthmonitoring-provider-that-sends-e-mails It shows you, how you can do that.
精彩评论