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?
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.
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!
<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?
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"]);