Browse Source

Added email notification on exceptions.

master
Teknikode 6 years ago
parent
commit
6e8eb52966
  1. 52
      Teknik/Areas/Error/Controllers/ErrorController.cs
  2. 4
      Teknik/Global.asax.cs

52
Teknik/Areas/Error/Controllers/ErrorController.cs

@ -1,6 +1,8 @@ @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 */ }
}
}
}

4
Teknik/Global.asax.cs

@ -108,8 +108,10 @@ namespace Teknik @@ -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;

Loading…
Cancel
Save