my error reporting model looks like this
using System;
using System.Diagnostics;
using System.Web;
using MailKit.Net.Smtp;
using MimeKit;
namespace Commonwealth.Models
{
public class Errors
{
public static void ErrorOccured(Exception ex)
{
var st = new StackTrace(ex, true);
// Get the top stack frame
var frame = st.GetFrame(0);
// Get the line number from the stack frame
var line = frame.GetFileLineNumber();
string page = frame.GetFileName();
var col = frame.GetFileColumnNumber();
string domain = HttpContext.Current.Request.Url.Host;
string MailBody = ex.Message.ToString() + "\r\n
string MessageToSend = ex.StackTrace.ToString();
string MailSender = "website email";
string MailRecipient = "my email";
string MailSubject = "Error on " + domain;
MimeMessage msg = new MimeMessage();
msg.From.Add(new MailboxAddress("", MailSender));
msg.To.Add(new MailboxAddress("",MailRecipient));
msg.Subject = MailSubject;
msg.Body = new TextPart(MimeKit.Text.TextFormat.Html) { Text = MailBody + "\r\n
using (var smtp = new SmtpClient())
{
smtp.Connect("emailserver", 587, false);
smtp.Authenticate("emailuser", "password");
smtp.Send(msg);
smtp.Disconnect(true);
}
}
}
}
and then the controller has this
public ActionResult About()
{
try
{
return View();
}
catch (Exception ex)
{
Errors.ErrorOccured(ex);
}
return RedirectToAction("Index", "Error", null);
}