Asp.net Global Application Error Handling
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
Application_error Mvc
Learn more about Stack Overflow the company Business Learn more about hiring developers
Page Level Error Handling In Asp.net Example
or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack asp.net custom error Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Catching errors in Global.asax up vote 8 down https://msdn.microsoft.com/en-us/library/bb397417.aspx vote favorite 2 I have the following in my Global.aspx which is meant for handling errors: void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); if (exception != null) { //Log if (HttpContext.Current.Server != null) { HttpContext.Current.Server.Transfer("/siteerror.aspx"); } } } This works for the most part, but sometimes does not get into Server.Transfer. For some reason HttpContext.Current.Server is null. I figured out where this happens: when http://stackoverflow.com/questions/10061837/catching-errors-in-global-asax errors occur in a user control and in my business logic classes. Am I missing something here? Thanks c# asp.net error-handling share|improve this question edited Apr 8 '12 at 11:43 svick 123k23194304 asked Apr 8 '12 at 9:36 Peuge 69311330 2 Any reason not using Response.Redirect rather? –leppie Apr 8 '12 at 9:41 1 Where in global.asax is this? –Marc Gravell♦ Apr 8 '12 at 9:42 @leppie I did not add this so not entirely sure why the previous person chose Server.Transfer, it shouldn't make a difference though should it? –Peuge Apr 8 '12 at 9:53 @MarcGravell I have updated the question to reflect this. –Peuge Apr 8 '12 at 9:54 Earlier you use Server.blah - does Server.Transfer(...) not work? (no HttpContext.Current) –Marc Gravell♦ Apr 8 '12 at 9:56 add a comment| 2 Answers 2 active oldest votes up vote 2 down vote Application_Error block can catch exception anytime between application life cycle. Application life cycle is parent of Session life cycle as you can understand there can be many sessions within a single application. Thus you may have HttpContext.Current null at certain errors occured before creating session or after ex
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 http://stackoverflow.com/questions/434859/handling-application-error-in-asp-net-apps-global-asax Learn more about Stack Overflow the company Business Learn more about hiring developers http://www.codeproject.com/Articles/600466/ASP-NETplusApplicationplusErrorplusHandling or posting 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 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 error handling 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) { 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 handling in asp 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 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
Latest Articles Latest Tips/Tricks Top Articles Beginner Articles Technical Blogs Posting/Update Guidelines Article Help Forum Article Competition Submit an article or tip Post your Blog quick answersQ&A Ask a Question about this article Ask a Question View Unanswered Questions View All Questions... C# questions Linux questions ASP.NET questions SQL questions VB.NET questions discussionsforums All Message Boards... Application Lifecycle> Running a Business Sales / Marketing Collaboration / Beta Testing Work Issues Design and Architecture ASP.NET JavaScript C / C++ / MFC> ATL / WTL / STL Managed C++/CLI C# Free Tools Objective-C and Swift Database Hardware & Devices> System Admin Hosting and Servers Java .NET Framework Android iOS Mobile SharePoint Silverlight / WPF Visual Basic Web Development Site Bugs / Suggestions Spam and Abuse Watch features Competitions News The Insider Newsletter The Daily Build Newsletter Newsletter archive Surveys Product Showcase Research Library CodeProject Stuff communitylounge Who's Who Most Valuable Professionals The Lounge The Insider News The Weird & The Wonderful The Soapbox Press Releases Non-English Language > General Indian Topics General Chinese Topics help What is 'CodeProject'? General FAQ Ask a Question Bugs and Suggestions Article Help Forum Site Map Advertise with us About our Advertising Employment Opportunities About Us Articles » Web Development » ASP.NET » General ArticleBrowse CodeStatsRevisions (9)Alternatives Comments (37) Add your ownalternative version Tagged as HTMLC#VS2013ASP.NET Stats 111.5K views2.6K downloads90 bookmarked Posted 30 May 2013 ASP.NET Application Error Handling Daniel Miller, 26 Jan 2015 CPOL 4.95 (37 votes) 1 2 3 4 5 4.95/5 - 37 votesμ 4.95, σa 1.05 [?] Rate this: Please Sign up or sign in to vote. Application error handling in ASP.NE