Asp.net Error Handling Application_error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack asp.net application_error Overflow the company Business Learn more about hiring developers or posting ads with us application_error mvc Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community
Application_error Not Firing
of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up handling Application_Error in ASP.NET app's global.asax up vote 19 down vote favorite 2 I wish to https://msdn.microsoft.com/en-us/library/bb397417.aspx send mail to an administrator when the application crashes. So I simply do this in global.asax: void Application_error(object sender, EventArgs e) { SendMessageToAdministarator(Server.GetLastError().ToString()); } But actually many times Application_Error is called even though the application won't crash. And I wish to send mail to admin ONLY when the application crashed. Also, do I have a simple way to lift the application back on? I'm looking for the simplest solution. asp.net share|improve this http://stackoverflow.com/questions/434859/handling-application-error-in-asp-net-apps-global-asax question edited Nov 14 '11 at 14:57 Jason Plank 2,12242338 asked Jan 12 '09 at 9:06 Hanan 6973927 add a comment| 1 Answer 1 active oldest votes up vote 48 down vote accepted What kind of errors are send when the application is not crashed? You could check the type of exception and don't send emails on the exceptions that don't crash the app (for example a redirect can throw the ThreadAbortException which I manually filter in code): protected void Application_Error(Object sender, EventArgs e) { Exception ex = Server.GetLastError(); if (ex is ThreadAbortException) return; Logger.Error(LoggerType.Global, ex, "Exception"); Response.Redirect("unexpectederror.htm"); } You could add a redirect to an error page with a message for the user that an error has occured and some links to relevant pages in the site. This is for the 'lift the application back on' - I hope this is what you wanted. Also you might look into logging with log4net which can also log errors on the server and send emails on errors. share|improve this answer edited Oct 20 '11 at 15:43 Josh Stodola 54.5k33147204 answered Jan 12 '09 at 9:18 rslite 35.6k43444 Thank you very much. What do you mean by "manually filter in code" ? also, one error that I constanly getting is some S
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings http://stackoverflow.com/questions/1171035/asp-net-mvc-custom-error-handling-application-error-global-asax and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation http://www.wduffy.co.uk/blog/using-application_error-in-asp-net-mvcs-global-asax-to-handle-errors/ Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it error handling only takes a minute: Sign up ASP.NET MVC Custom Error Handling Application_Error Global.asax? up vote 82 down vote favorite 71 I have some basic code to determine errors in my MVC application. Currently in my project I have a controller called Error with action methods HTTPError404(), HTTPError500(), and General(). They all accept a string parameter error. Using or modifying the code application error handling below. What is the best/proper way to pass the data to the Error controller for processing? I would like to have a robust solution as possible. protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); Response.Clear(); HttpException httpException = exception as HttpException; if (httpException != null) { RouteData routeData = new RouteData(); routeData.Values.Add("controller", "Error"); switch (httpException.GetHttpCode()) { case 404: // page not found routeData.Values.Add("action", "HttpError404"); break; case 500: // server error routeData.Values.Add("action", "HttpError500"); break; default: routeData.Values.Add("action", "General"); break; } routeData.Values.Add("error", exception); // clear error on server Server.ClearError(); // at this point how to properly pass route data to error controller? } } c# asp.net asp.net-mvc error-handling user-experience share|improve this question edited Sep 10 '14 at 17:54 Oualid KTATA 52138 asked Jul 23 '09 at 11:07 aherrick 7,6411770115 add a comment| 9 Answers 9 active oldest votes up vote 80 down vote accepted Instead of creating a new route for that, you could just redirect to your controller/action and pass the information via querystring. For instance: protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); Response.C
1, 2011 • (7) ASP.NET MVC's controllers have great error handling capabilities and can be easily extended to cater for application specific requirements. However, many developer don't want to manage errors at a controller level and would rather manage 99 percent of errors from a single location. This allows for a single point of error logging, notification and handling. The Application_Error event within the global.asax is the perfect place for this, but there are a few issues to consider when used in an MVC project. Server.Transfer is not available to serve an error message as it requires a physical file to serve. Your MVC project more than likely doesn't have one of theses as it uses routes, controllers and views. We need to find a workaround for this in order to return suitable response headers. Reponse.Redirect is not suitable as '500: Internal Server Error' and '404: Not Found' pages should serve a suitable response header, not a 301 redirect. Response.Clear() should be called to ensure that any content written to the response stream before the error occurred is removed. Server.ClearError() must be called to stop ASP.NET from serving the yellow screen of death. With these points in mind the following steps can be coded into the Application_Error event for error handling and logging. Get the last error raised. Get the error code to respond with. Log the error (I'm ignoring 404′s). Clear the response stream. Clear the server error. Render the error handling controller without a redirect. Here is the Application_Error code… void Application_Error(object sender, EventArgs e) { var error = Server.GetLastError(); var code = (error is HttpException) ? (error as HttpException).GetHttpCode() : 500; if (code != 404) { // Generate email with error details and send to administrator // I'm using RazorMail which can be downloaded from the Nuget Gallery // I also have an extension method on type Exception that creates a string representation var email = new RazorMailMessage("Website Error"); email.To.Add("errors@wduffy.co.uk"); email.Templates.Add(error.GetAsHtml(new HttpRequestWrapper(Request))); Kernel.Get