Global.asax Error Handling Email
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
Asp.net Error Handling
Learn more about Stack Overflow the company Business Learn more about hiring developers or exception handling in asp.net c# posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow
Asp.net Custom Error
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 Send email with Global.asax file up vote 3 asp.net application_error down vote favorite In the event of an unhandled error I would like to send an email to the admin that contains the information from the error that occurred. Below is what I have in my web.config and Global.asax.cs file, the redirect works but the email does not:
"; mail.Body += error.ToString(); mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; smtp.Credentials = new System.Net.NetworkCredential("username", "password"); smtp.Port = 587; smtp.Send(mail); Server.ClearError(); } c# asp.net unhandled-exception share|improve this question asked Nov 20 '13 at 20:56 brandozz 299617 2 In what way does "the email not work"? Please be more specific. –John Källén Nov 20 '13 at 20:57 1 Don't you get an exception on smtp.Send(mail); ? –bump Nov 20 '13 at 20:58 The email is not sending and no error messages are displayed besides the default message on the error.aspx page. It almost seems as if the application redirects to the error page and does nothing with Global.asax. –brandozz Nov 20 '13 at 21:03 Could you check for null on your var error right after the Server.GetLastError(), this would avoid the null exception on error.ToString(). Also try to remove the defaultRedirect. –Swell Nov 20 '13 at 21:29 I found this and it solved my problem, at least for now
Websites Community Support ASP.NET Community Standup ForumsHelp Web Forms:Guidance Videos Samples Forum Books Open Source Getting Started Getting StartedGetting Started with ASP.NET 4.5 Web Forms and Visual Studio 20131. Getting Started
Asp.net Custom Error Page
with Web Forms and Visual Studio2. Create the Project3. Create the Data Access Layer4. asp.net error logging UI and Navigation5. Display Data Items and Details6. Shopping Cart7. Checkout and Payment with PayPal8. Membership and Administration9. URL Routing10.
Asp.net Error Handling Best Practices
ASP.NET Error HandlingIntroduction to ASP.NET Web FormsCreating a Basic Web Forms Page in Visual Studio 2013Creating ASP.NET Web Projects in Visual Studio 2013Code Editing ASP.NET Web Forms in Visual Studio 2013ASP.NET Scaffolding in http://stackoverflow.com/questions/20106842/send-email-with-global-asax-file Visual Studio 2013ASP.NET Web Forms (dotnetConf 2014)Using Page Inspector for Visual Studio 2012 in ASP.NET Web FormsVisual Studio 2012 Hands On LabsWhat's New in ASP.NET and Web Development in Visual Studio 2012What's New in Web Forms in ASP.NET 4.5Using Page Inspector in Visual Studio 2012Monitoring and TelemetryRoutingASP.NET 4 - RoutingASP.NET 4 - Defining RoutesASP.NET 4 - Constructing URLs from RoutesASP.NET 4 - Accessing URL Parameters in http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/aspnet-error-handling a PageJavaScript and Client FrameworksASP.NET 4 - Microsoft Ajax OverviewASP.NET AJAX Control Toolkit (maintained by DevExpress)Working with Data Getting Started with ASP.NET 4.5 Web FormsModel Binding and Web Forms in Visual Studio 20131. Retrieving and Displaying Data2. Updating, Deleting, and Creating Data3. Sorting, Paging, and Filtering Data4. Integrating JQuery UI Datepicker5. Using Query String Values to Filter Data6. Adding Business Logic LayerASP.NET 4 Web Forms - Validating User Input in a PageASP.NET 4 Web Forms - State ManagementASP.NET Data Access - Recommended ResourcesServer Data ControlsASP.NET 4 Data-Bound ControlsASP.NET 4 Data Source Controls OverviewASP.NET 4.5 Chart ControlRecommended Resources for ASP.NET Data AccessSecurity, Authentication, and Authorization Getting Started with ASP.NET 4.5 Web FormsASP.NET IdentityCreate a secure ASP.NET Web Forms app with user registration, email confirmation and password reset (C#)Create an ASP.NET Web Forms app with SMS Two-Factor Authentication (C#)OWIN and KatanaPerformanceUsing Asynchronous Methods in ASP.NET 4.5[Build 2014] Deep Dive: Improving Performance in Your ASP.NET App (Levi Broderick)ASP.NET 4 - Web Forms Performance OverviewASP.NET 4 - Web Forms Caching OverviewASP.NET 4 - Caching Web Forms PagesASP.NET 4 - Caching Portions of a Web Forms PageDeploymentGet Started with ASP.NET and Azure App ServicesDeploy a Web App in Azure App ServiceWeb Deployment Overview
Effectively in ASP.NET MVC 10 April 2014Handling Errors Effectively in ASP.NET MVCASP.NET MVC gives you more options in the way that you handle exceptions. Error handling https://www.simple-talk.com/dotnet/asp-net/handling-errors-effectively-in-asp-net-mvc/ isn't intrinsically exciting, but there are many ways of avoiding the classic yellow page http://www.wduffy.co.uk/blog/using-application_error-in-asp-net-mvcs-global-asax-to-handle-errors/ of death, even getting ELMAH to manage error handling for you. 79 3 Dino Esposito Years ago, ASP.NET's error handling was one of the major things that made me wonder if ASP.NET MVC could give me something that ASP.NET Web Forms couldn't. Web Forms is based on pages; so if something goes wrong, all error handling that you can do is to redirect the user to another page and explain what the error was or just be generically sorry. ASP.NET Web Forms allow you to map an error page for each possible HTTP status code. You control the mapping through the
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