PPCUBAN

Hello

I'm new in Winforms. I come from ASP.NET. So, i'm trying to get used to the platforms differences.

My question is. I need to create a mechanism that whenever, wherever in my code an Exception occurs save that Exception to the database and automatcally send an email to myself.

I'm deploying a new app for my corporation and i want to be able to spot all error that might come up.

Thank you

PD: in ASP.NET there is a global method On_Error that is executed whenever a page has an exception. Then i used to grab the url, page, logged user and exception and it was quicker my response to the error.



Re: Windows Forms General Sending Email when Exception

nobugz

To trap only unhandled exceptions, implement an Application.ThreadException event handler. To trap any exception, look at the Enterprise Library Exception Handling Application Block.





Re: Windows Forms General Sending Email when Exception

Christopher Payne

Assuming C# 2.0, and that you have an SMTP server around. This snippet reads the mail server out of the App.Config file.

Code Snippet

using System.Configuration;

public void foo()

{

try

{

doStuff();

}

catch(Exception ex)

{

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);

message.Body = ex.Message;

System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(

ConfigurationManager.AppSettings["MailServer"]);

client.Send(message);

}

}

There isn't a single On_Error event, but you can use Application.ThreadException:

Code Snippet

[STAThread]

static void Main()

{

Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

Application.Run(new Form1());

}

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)

{

LogAndReportException(e.Exception);

}

I don't like to rely entirely on ThreadException, but it's a fallback. It won't catch exceptions that happen in other threads. I've also seen cases where the application hit an exception and exited without the ThreadException event being raised.





Re: Windows Forms General Sending Email when Exception

Andreas Johansson

I wouldn't recommend that you rely on that the application can send you an email when it fails in an unhandled way by handling the Application.ThreadException event. There might be a very good reason that your application fails and trying to send a mail might cause more trouble. This is probably not that likely but it is possible. Try to keep logging of critical situations to the minimum and use resources that are least likely to fail (like eventlog or files) to log unhandled exceptions.

You can then monitor the event log or file for failures and have monitoring software act on it rather than you having to implement it into your application..

A possible scenario is if you have an unhandled network failure, you will most likely not be able to fire of an email from inside your application if it also relies on a working network connection.