views:

62

answers:

3

Hi all,

I've been working on a .Net Windows Service and i was wondering how to implement sending out an email on an error from within the service. I entertained myself with the idea of sending the email from within a catch block, but then i remembered that there could be an error within sending an email as well and an unhandled exception would be thrown. Have you guys ever working on something like this. If so, what path did you take?

Thanks for any help

+2  A: 

Look into log4net (it can do email, file log, db log) and see if that provides you what you're looking for. This is the first thing that comes to my mind.

Chris Missal
+1  A: 

Place the email sending login in global.asax and Application_Error method. You can get the most recent exception by using Server.GetLastError().

So I presume you could send the email and then re-throw the exception, or throw a new exception with that as inner exception.

Skrim
+1  A: 

I don't recommend the email approach. I would recommend putting it directly into your bug-tracking database if at all possible. You still have to catch exceptions you encounter while logging and do something with them as well (hopefully it doesn't recurse to where you have no options).

What I have done in the past:
... Log to bug-tracking database.
... Log to event log.
... Log to network share.
... Log to local disk.

Edit: I should clarify why I don't recommend email. The primary reason is that the error should go to the database anyway and if you implement the "email on error" poorly, your email becomes the go-to for exceptions that at some point in the future you may no longer want. Even if you go so far as to create a generic email that points to the appropriate mailbox, you still end up avoiding what you should be doing in the first place -- logging it to your bug-tracking database.

Austin Salonen