Error Handling In Application_error
Contents |
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students
here for a quick overview of the site Help Center Detailed answers to any questions you might
Page Level Error Handling In Asp.net Example
have Meta Discuss the workings and policies of this site application_error web api About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting
Application Error Event In Global Asax
ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 https://msdn.microsoft.com/en-us/library/24395wz3.aspx 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 send mail to an administrator when the application crashes. So I simply do this in global.asax: void Application_error(object sender, EventArgs e) { http://stackoverflow.com/questions/434859/handling-application-error-in-asp-net-apps-global-asax 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 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 f
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 http://www.wduffy.co.uk/blog/using-application_error-in-asp-net-mvcs-global-asax-to-handle-errors/ 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. https://channel9.msdn.com/Forums/TechOff/131498-HTTP-Error-VS-ApplicationError-ASPNET-Error-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 error handling 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, error handling in 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
information from MSDN Visual Studio Achievements Latest Achievement: Loading Visual Studio Achievements Something went wrong getting the Visual Studio Achievements Follow us @ch9 Subscribe to Channel 9 Sign In Channel9 Browse Tags Shows Series Blogs Authors Events Topics Coding4Fun Windows Azure Visual Studio DevOps Microsoft Mechanics MVPs Forums Coffeehouse Site Feedback Tech Off Build Events WinHEC Visual Studio TechEd Build Microsoft Ignite Azure Speakers ForumsTechOff HTTP Error VS Application_Error (ASP.NET Error Handling) Subscribe Oops, something didn't work. Try again? Sign In to subscribe to this conversation What does this mean? Subscriptions allow us send you email notifications when new content is added. You need to be signed in to Channel 9 to use this feature. Getting subscription Unsubscribe to this conversation Subscribe to this conversation What does this mean? Subscriptions allow us send you email notifications when new content is added. Unsubscribing Subscribing Did you know you cansign up for email notifications? RSS reddit Tweet zhuo Nov 09, 2005 at7:46PM Hi all, I've implemented the Application_Error handler in Global.asax.cs which handles all unhandled errors thrown by the ASP.NET application. This error handler is fine for most situations but it seems to handle HTTP errors as well, e.g. 404 errors. This didn't really come as a surprise to mebut my question is how do I selectively handle certain errors in Application_Error and leave the rest rethrown again. E.g. I don't want to handle HTTP 404 errors, I want to redirect it to a error page for HTTP 404 based on web.config. But for other errors I want Application_Error to handle it. The alternatively solution I am looking for is a way of detecting that an HTTP error has occured and redirect it programmatically along with its status code. Bonus for me if you have solution to both alternatives. Thank you James Charles