Asp.net Error Handler Global.asax
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Retired content Samples We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. MSDN Library MSDN Library MSDN Library MSDN Library Design Tools Development Tools and Languages Mobile and Embedded Development .NET Development Office development Online Services Open Specifications patterns & practices Servers and Enterprise Development Speech Technologies Web Development Windows Desktop App Development TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. How to: Handle Application-Level Errors Other Versions Visual Studio 2010 .NET Framework 4 Visual Studio 2008 .NET Framework 3.0 Visual Studio 2005 This code example shows how to create an error handler in the Global.asax file that will catch all unhandled ASP.NET errors while processing a request — in other words, all the errors that are not caught with a Try/Catch block or in a page-level error handler. In the example, the handler transfers control to a generic error page named GenericErrorPage.aspx, which interprets the error and displays an appropriate message. Example The following example is from a complete code sample in Complete Example for Error Handlers. Security Note Never set customErrors to Off in your Web.config file if you do not have an Application_Error handler in your Global.asax file. Potentially compromising information about your Web site can be exposed to anyone who can cause an error to occur on your site. C#VB Copy void Application_Error(object sender, EventArgs e) { // Code that runs when an unhandled error occurs // Get the exception object. Exception exc = Server.GetLastError(); // Handle HTTP errors if (exc.GetType() == typeof(HttpException)) { // The Complete Error Handling Example generates // som
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 Overflow the company Business Learn more about hiring developers 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 Catching errors in Global.asax up vote 8 down vote favorite 2 I have the following in https://msdn.microsoft.com/en-us/library/24395wz3.aspx 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 errors occur in a user control and in my business logic classes. http://stackoverflow.com/questions/10061837/catching-errors-in-global-asax 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 expiring of sessions. If you want to write session specific error redirects you should check for Null of current HttpContext always. You can also use Server.GetL
here for a quick overview of the site Help Center Detailed answers to any questions you might have http://stackoverflow.com/questions/14612888/best-practice-error-handling-in-global-asax Meta Discuss the workings and policies of this site About Us Learn http://stackoverflow.com/questions/28768715/application-error-in-global-asax-not-catching-errors-in-webapi more about Stack Overflow the company Business Learn more about hiring developers 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 asp.net error you, helping each other. Join them; it only takes a minute: Sign up Best practice error handling in Global asax up vote 0 down vote favorite I have code below in global asax now, I want to store exception log in database, is this good practice? because if sql error happens there, I want to log asp.net error handler it too. So I am thinking changing the code below to write text log instead email, then on sql error, write text log. void Application_Error(object sender, EventArgs e) { // Code that runs when an unhandled error occurs string testEnvironment = ConfigurationSettings.AppSettings["isTestEnvironment"]; if (testEnvironment == "0") { Exception ex = Server.GetLastError(); if (ex is HttpException && ex.InnerException is ViewStateException) { Response.Redirect(Request.Url.AbsoluteUri) return } StringBuilder theBody = new StringBuilder(); theBody.Append("URL: " + Request.Url + "\n"); theBody.Append("Referer: " + Request.ServerVariables["HTTP_REFERER"] + "\n"); theBody.Append("IP: " + Request.ServerVariables["REMOTE_HOST"] + "\n"); theBody.Append("Error Message: " + ex.ToString() + "\n"); if (User.Identity.IsAuthenticated) theBody.Append("User: " + User.Identity.Name + "\n"); else theBody.Append("User is not logged in." + "\n"); theBody.Append("Form Values: " + "\n"); foreach (string s in Request.Form.AllKeys) { if (s != "__VIEWSTATE") theBody.Append(s + ":" + Request.Form[s] + "\n"); } theBody.Append("Session Values: " + "\n"); foreach (string s in Session.Keys) theBody.Append(s + ":" + Session[s] + "\n"); System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage(); email.IsBodyHtml = false; email.From = new System.Net.Mail.MailAddress("errors@karpach.com", "ErrorManager"); email.To.Add(new System.Net.Mail.MailAddress("errornotification@karpach.com", "Developer")); email.Subject = Request.Url.
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 Overflow the company Business Learn more about hiring developers 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 Application_Error in global.asax not catching errors in WebAPI up vote 4 down vote favorite 1 For a project I am working on, one of the things we're implementing is something that we have code for in some of my teams older ASP.NET and MVC projects - an Application_Error exception catcher that dispatches an email to the development team with the exception experience and most relevant details. Here's how it looks: Global.asax: protected void Application_Error(object sender, EventArgs e) { Exception ex = Server.GetLastError(); string path = "N/A"; if (sender is HttpApplication) path = ((HttpApplication) sender).Request.Url.PathAndQuery; string args = string.Format("Path: {0}", path); // Custom code that generates an HTML-formatted exception dump string message = Email.GenerateExceptionMessage(ex, args); // Custom code that sends an email to the dev team. Email.SendUnexpectedErrorMessage("Some App", message); } One "minor" problem, though - when I intentionally have a part of the code throw an exception in order to test this mechanism... public static void GetMuffinsByTopping(string topping) { throw new Exception("Test Exception!", new Exception("Test Inner Exception!!!")); // Actual repository code is unreachable while this test code is there } The front-end JavaScript is imm