diff --git a/Teknik/Areas/Error/Controllers/ErrorController.cs b/Teknik/Areas/Error/Controllers/ErrorController.cs index 4d09c56..11c8187 100644 --- a/Teknik/Areas/Error/Controllers/ErrorController.cs +++ b/Teknik/Areas/Error/Controllers/ErrorController.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net.Mail; +using System.Text; using System.Web; using System.Web.Mvc; using Teknik.Areas.Error.ViewModels; @@ -24,6 +26,11 @@ namespace Teknik.Areas.Error.Controllers Response.TrySkipIisCustomErrors = true; } + if (exception != null) + { + SendErrorEmail(exception); + } + ErrorViewModel model = new ErrorViewModel(); model.Exception = exception; @@ -43,6 +50,11 @@ namespace Teknik.Areas.Error.Controllers Response.TrySkipIisCustomErrors = true; } + if (exception != null) + { + SendErrorEmail(exception); + } + ErrorViewModel model = new ErrorViewModel(); model.Description = exception.Message; model.Exception = exception; @@ -62,6 +74,11 @@ namespace Teknik.Areas.Error.Controllers Response.TrySkipIisCustomErrors = true; } + if (exception != null) + { + SendErrorEmail(exception); + } + ErrorViewModel model = new ErrorViewModel(); model.Exception = exception; @@ -99,10 +116,45 @@ namespace Teknik.Areas.Error.Controllers Response.TrySkipIisCustomErrors = true; } + if (exception != null) + { + SendErrorEmail(exception); + } + ErrorViewModel model = new ErrorViewModel(); model.Exception = exception; return View("/Areas/Error/Views/Error/Http500.cshtml", model); } + + private void SendErrorEmail(Exception ex) + { + try + { + // Let's also email the message to support + SmtpClient client = new SmtpClient(); + client.Host = Config.ContactConfig.Host; + client.Port = Config.ContactConfig.Port; + client.EnableSsl = Config.ContactConfig.SSL; + client.DeliveryMethod = SmtpDeliveryMethod.Network; + client.UseDefaultCredentials = true; + client.Credentials = new System.Net.NetworkCredential(Config.ContactConfig.Username, Config.ContactConfig.Password); + client.Timeout = 5000; + + MailMessage mail = new MailMessage(Config.SupportEmail, Config.SupportEmail); + mail.Subject = string.Format("Exception Occured on: {0}", Request.Url.AbsoluteUri); + mail.Body = string.Format(@" +Message: {0} + +Source: {1} + +Stack Trace: {2}", ex.Message, ex.Source, ex.StackTrace); + mail.BodyEncoding = UTF8Encoding.UTF8; + mail.DeliveryNotificationOptions = DeliveryNotificationOptions.Never; + + client.Send(mail); + } + catch (Exception) { /* don't handle something in the handler */ } + } } } \ No newline at end of file diff --git a/Teknik/Global.asax.cs b/Teknik/Global.asax.cs index 66622ee..0febdfd 100644 --- a/Teknik/Global.asax.cs +++ b/Teknik/Global.asax.cs @@ -108,8 +108,10 @@ namespace Teknik protected void Application_Error(object sender, EventArgs e) { + // Get the last exception Exception exception = Server.GetLastError(); - + + // Clear the response Response.Clear(); HttpException httpException = exception as HttpException;