Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic email subjects with Elmah?

Tags:

asp.net

elmah

I'm using the errorMail functionality of Elmah to send an email when ASP.NET encounters an error. It allows you to configure the SMTP settings as well as hard-code a sender, recipient, and subject.

My question is: can I use a dynamic subject? Specifically, I would like to use the Exception.Message property as my subject, so that I can tell what the error is about just from the subject line of the email.

There is no documentation, and from a quick scan of the source code it looks impossible without modifying the code, but I thought I would ask the question anyways.

Relevant source code:

  • ErrorMailModule.cs
  • ErrorMailHtmlFormatter.cs
like image 336
Portman Avatar asked Aug 19 '09 14:08

Portman


2 Answers

Doh! Answer is on line 454 of ErrorMailModule.cs:

string subjectFormat = Mask.EmptyString(this.MailSubjectFormat, "Error ({1}): {0}");
mail.Subject = string.Format(subjectFormat, error.Message, error.Type)
                .Replace('\r', ' ')
                .Replace('\n', ' ');

You can use {0} for the message and {1} for the type.

like image 67
Portman Avatar answered Oct 10 '22 15:10

Portman


I've changed the email subject from the web.config file in this way:

<errorMail from="..." subject="Some subject: {0}">

where {0} will be the exception message.

You can check this article for more details http://weblogs.asp.net/jeffwids/format-the-email-subject-in-the-elmah-error-logging-module

like image 41
Alina Avatar answered Oct 10 '22 13:10

Alina