No emails when I perform a manual log

246 views
Skip to first unread message

dege

unread,
Aug 2, 2007, 4:08:59 PM8/2/07
to ELMAH
Hi there,

I constructed a simple test because I noticed that I was not receiving
emails for exceptions where I called ErrorLog.Default.Log rather than
letting the exception reach the user.

protected void btnThrow_Click(object sender, EventArgs e) {
throw new ApplicationException("Exception Test");
}

protected void btnCatch_Click(object sender, EventArgs e) {
try {
throw new ApplicationException("Exception Test");
} catch (Exception ex) {
GotDotNet.Elmah.ErrorLog.Default.Log(new
GotDotNet.Elmah.Error(ex, HttpContext.Current));
}
lbOutput.Text += "Exception thrown and caught.<br />\n";
}

When I click btnThrow, it works fine and I get the email (and see the
application exception on the screen). btnCatch does not send me any
emails. Is there a separate method I need to call to get the emails to
be produced?

Atif Aziz

unread,
Aug 2, 2007, 5:52:42 PM8/2/07
to el...@googlegroups.com
The mailing and logging pieces of ELMAH are completely unrelated so
that's why going over ErrorLog does not send any e-mail. While there
is an explicit way to log an exception there is no single-method way
to send an exception via e-mail. It's not very difficult either. The
bulk of the formatting is done in ErrorMailHtmlFormatter so you could
do something like the following (the code shown here is in IronPython
that you can try out interactively with a few replacements):

IronPython 1.0.60816 on .NET 2.0.50727.832
Copyright (c) Microsoft Corporation. All rights reserved.
>>> import clr
>>> clr.AddReferenceToFileAndPath(r"C:\ELMAH\bin\net-2.0\Debug\Elmah.dll")
>>> from Elmah import *
>>> from System.Net.Mail import SmtpClient, MailMessage
>>> from System.IO import StringWriter
>>> from System import ApplicationException
>>> e = Error(ApplicationException())
>>> sw = StringWriter()
>>> formatter = ErrorMailHtmlFormatter()
>>> formatter.Format(sw, e)
>>> mail = MailMessage("el...@example.com", "m...@example.com")
>>> mail.Subject = e.Message.Replace('\r', ' ').Replace('\n', ' ')
>>> mail.IsBodyHtml = True
>>> mail.Body = sw.ToString()
>>> smtp = SmtpClient("smtp.example.com")
>>> smtp.Send(mail)

Here's quick breakdown...

Add reference to ELMAH assembly:

>>> import clr
>>> clr.AddReferenceToFileAndPath(r"C:\ELMAH\bin\net-2.0\Debug\Elmah.dll")

Import stuff we'll be using (this is like using statements in C#):

>>> from Elmah import *
>>> from System.Net.Mail import SmtpClient, MailMessage
>>> from System.IO import StringWriter
>>> from System import ApplicationException

Create an Error object from an ApplicationException (without throwing):

>>> e = Error(ApplicationException())

Format the Error object into HTML:

>>> sw = StringWriter()
>>> formatter = ErrorMailHtmlFormatter()
>>> formatter.Format(sw, e)

Create the MailMessage object (change the sender and recipient):

>>> mail = MailMessage("el...@example.com", "m...@example.com")
>>> mail.Subject = e.Message.Replace('\r', ' ').Replace('\n', ' ')
>>> mail.IsBodyHtml = True
>>> mail.Body = sw.ToString()

Finally send it (use your SMTP settings):

>>> smtp = SmtpClient("smtp.example.com")
>>> smtp.Send(mail)

I would also recommend opening this up as an issue over at
http://code.google.com/p/elmah/issues/list if you'd like to see this
simplified and implemented some time in the future.

Hope this helps.

dege

unread,
Aug 2, 2007, 6:27:19 PM8/2/07
to ELMAH
Thanks for the tip Atif! Here is what I ended up doing:

using ...default list..
using GotDotNet.Elmah;
using System.IO;
using System.Net.Mail;

namespace MyProject {
public static class ErrorLog {
public static void Log(Exception ex) {
Error error = new Error(ex, HttpContext.Current);
GotDotNet.Elmah.ErrorLog.Default.Log(error);
StringWriter sw = new StringWriter();
ErrorMailHtmlFormatter formatter = new
ErrorMailHtmlFormatter();
formatter.Format(sw, error);
MailMessage mail = new MailMessage("el...@example.com",
"m...@example.com");
mail.Subject = error.Message.Replace('\r', '
').Replace('\n', ' ');
mail.IsBodyHtml = true;
mail.Body = sw.ToString();
SmtpClient smtp = new SmtpClient();
smtp.Send(mail);
}
}
}

------------ Then over in my test page ---------------

protected void btnCatch_Click(object sender, EventArgs e) {
try {
throw new ApplicationException("Exception Test");
} catch (Exception ex) {

ErrorLog.Log(ex);


}
lbOutput.Text += "Exception thrown and caught.<br />\n";
}


Works perfectly!

dege

unread,
Aug 2, 2007, 6:32:49 PM8/2/07
to ELMAH
One more question and I'll let you get back to work... is there an
easy way to get the values from web.config:

<gotdotnet.elmah><errorMail from="el...@example.com"
to="m...@example.com" subject="Error Log" async="true"/></
gotdotnet.elmah>

Is "from" and "to" publicly available properties of some object in
Elmah? If not, is there a way to load them in code?

Atif Aziz

unread,
Aug 2, 2007, 6:46:16 PM8/2/07
to el...@googlegroups.com
Somehow or the other, I had the feeling that this question was going
to pop sooner or later. :) What you could do is directly grab the
settings by going to
ConfigurationManager.GetSection("elmah/errorMail"). This returns an
IDictionary, which you can index into using the attribute names
appearing on the errorMail element in your web.config. It's a bit
dirty to go this way, but it'll serve your itch for now. :) This
method and assumption that you'll get an IDictionary object from that
section will remain safe and compatible for v1.0 but probably not
beyond that.

dege

unread,
Aug 2, 2007, 7:05:30 PM8/2/07
to ELMAH
Very cool! Here's what my implementation ended up looking like:

using ...default list..
using GotDotNet.Elmah;

using System.Collections;
using System.IO;
using System.Net.Mail;

namespace MyProject {
public static class ErrorLog {
public static void Log(Exception ex) {

Hashtable errorMail =
ConfigurationManager.GetSection("gotdotnet.elmah/errorMail") as
Hashtable;


Error error = new Error(ex, HttpContext.Current);
GotDotNet.Elmah.ErrorLog.Default.Log(error);
StringWriter sw = new StringWriter();
ErrorMailHtmlFormatter formatter = new
ErrorMailHtmlFormatter();
formatter.Format(sw, error);
MailMessage mail = new

MailMessage((string)errorMail["from"], (string)errorMail["to"]);

Reply all
Reply to author
Forward
0 new messages