private MailMessage GetMailMessage(Exception exception, string to)
        {
            var error   = new Error(exception);
            var subject = error.Message.Replace("\r", string.Empty).Replace("\n", string.Empty);
            var html    = true;
            var body    = "An error occurred.";

            try
            {
                var writer    = new StringWriter();
                var formatter = new ErrorMailHtmlFormatter();
                formatter.Format(writer, error);
                body = writer.ToString();
            }
            catch (Exception)
            {
                // some exceptions may creep up before we have access to the html formatter
                html = false;
                var builder = new StringBuilder(body);
                builder.AppendLine();
                builder.AppendLine();
                builder.AppendLine(exception.Message);
                builder.AppendLine();
                builder.AppendLine(exception.StackTrace);
                body = builder.ToString();
            }
            return(new MailMessage(_from, to)
            {
                Subject = subject,
                IsBodyHtml = html,
                Body = body,
            });
        }
예제 #2
0
        public void LogException(Exception exception)
        {
            var error = new Error(exception);

            // first try to post it to the Elmah log
            try
            {
                var log = ErrorLog.GetDefault(null);
                if (log != null)
                {
                    log.Log(error);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(string.Format("Failed to add exception to Elmah error logger: {0}", ex.Message), "Error");
            }

            // second try to send it via Elmah mail
            try
            {
                var writer    = new StringWriter();
                var formatter = new ErrorMailHtmlFormatter();
                formatter.Format(writer, error);
                var message = new MailMessage(_config.EmailDefaultFromAddress,
                                              _config.EmailEmergencyAddresses)
                {
                    Subject    = error.Message.Replace("\r", string.Empty).Replace("\n", string.Empty),
                    IsBodyHtml = true,
                    Body       = writer.ToString(),
                };

                var client = new SmtpClient();
                client.Send(message);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(string.Format("Failed to send Elmah exception email: {0}", ex.Message), "Error");
            }
        }
예제 #3
0
        private MailMessage CreateMailMessage(Exception exception)
        {
            var error   = new Error(exception);
            var subject = error.Message.Replace("\r", string.Empty).Replace("\n", string.Empty);
            var isHtml  = true;
            var body    = "An error occurred.";

            try
            {
                var writer    = new StringWriter();
                var formatter = new ErrorMailHtmlFormatter();
                formatter.Format(writer, error);
                body = writer.ToString();
            }
            catch (Exception)
            {
                // some exceptions may creep up before we have access to the html formatter
                isHtml = false;
                var builder = new StringBuilder(body);
                builder.AppendLine();
                builder.AppendLine();
                builder.AppendLine(exception.Message);
                builder.AppendLine();
                builder.AppendLine(exception.StackTrace);
                body = builder.ToString();
            }
            var tos         = _appConfiguration.MailExceptionTo.ToArray();
            var mailMessage = new MailMessage(_appConfiguration.MailFromDefault, tos.First())
            {
                Subject    = subject,
                Body       = body,
                IsBodyHtml = isHtml,
            };

            foreach (var to in tos.Skip(1))
            {
                mailMessage.To.Add(to);
            }
            return(mailMessage);
        }