5

Hi and thanks for taking the time to answer my question.

I have an asp.net webforms application that I built for internal purposes of a medium sized company.

First off let me say that I handle all exceptions that might happen during CRUD operations and show the appropriate message to the user. However, is there a way that I can, in a centralised manner, first email the innerException message to myself along with the stack trace, and then show the message to the user?

In other words, I dont want to create a method like EmailException() and call it in every catch(Exception ex) block but I'd like to call it once somewhere where all exceptions are first caught.

If my question is not clear enough please tell me.

Thanks again!

Dragan
  • 3,713
  • 12
  • 42
  • 59
  • 2
    This is older than dirt at this point, but probably still makes a passable starting point: http://www.developer.com/net/asp/article.php/961301/Global-Exception-Handling-with-ASPNET.htm – Donnie Dec 12 '12 at 17:51

2 Answers2

4

Elmah is great, but Log4Net will also give you that capability, along with other logging capabilities. You just need to add an SmtpAppender definition to your log4net config, along with whatever other appenders you want/need/desire.

Simple, easy to do. And the easiest way to catch any unhandled exceptions is by adding an Application_Error() method to your global.asax. It is invoked by the Asp.Net runtime and will catch

all unhandled ASP.NET errors while processing a request — in other words, all the errors that are not caught with a Try/Catch block or in a page-level error handler.

It should be noted that Elmah, log4net and the global.asax Application_Error() method all coexist happily. Elmah just sits there, sotto voce, and listens, logging any exceptions that pass by.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
  • +1. Log4Net is awesome. (Don't avoid it because of its low activity in recent years.) – Michael Haren Dec 12 '12 at 18:04
  • So basically I integrate elmah or log4net, set up the config files, and then in application_error() I basically redirect a user to an error page and tell him that this error was logged and that the admin has been notified? – Dragan Dec 12 '12 at 18:06
  • @Dragan yep, that's a common way to do it – Michael Haren Dec 12 '12 at 23:34
3

Elmah is perfect for this:

  • Logging of nearly all unhandled exceptions.
  • A web page to remotely view the entire log of recoded exceptions.
  • A web page to remotely view the full details of any one logged exception, including colored stack traces. *In many cases, you can review the original yellow screen of death that ASP.NET generated for a given exception, even with customErrors mode turned off.
  • An e-mail notification of each error at the time it occurs.
  • An RSS feed of the last 15 errors from the log.

Here's a config example.

Community
  • 1
  • 1
Michael Haren
  • 105,752
  • 40
  • 168
  • 205
  • yes i'm looking the the elmah page. However, this only handles unhandled exceptions. I handle the exceptions that occur from CRUD operations and print in a label msgs such as "Problem occurred while adding a new Project. The admin will be notified of this problem." This means that I handle these exceptions. What will the users see if I left the exception unhandled and elmah has logged it? Yellow screen of death? – Dragan Dec 12 '12 at 17:55
  • No. Definitely not a YSOD if you do it correctly. Let the unhandled error bubble up to your global error handler. This presents a friendly face on the error, and Elmah still gets a piece of the action. – Michael Haren Dec 12 '12 at 17:59
  • Okay I will investigate further on how to handle the exception after elmah has logged it. If you have a link that shows that that would be great. Am accepting your answer. Thank you very much, greatly appreaciated. Never heard of ELMAH before it was exactly what I was looking for! – Dragan Dec 12 '12 at 18:02