开发者

Exception notification in .NET

开发者 https://www.devze.com 2023-02-24 19:25 出处:网络
I have a WPF program that I\'m developing in which I want to catch exceptions and send notification to a central (off-site) authority, returning control back to the user. A system commonly seen in sof

I have a WPF program that I'm developing in which I want to catch exceptions and send notification to a central (off-site) authority, returning control back to the user. A system commonly seen in software like Firefox, Eclipse, etc.

To wit:

User clicks a button in my WPF program that is broken. Exception is caught and user is presented with a dialog box "Shit, we screwed开发者_如何学运维 something up. We've been notified and will fix it ASAP. In the meantime, you might be careful about <some smart summary of events>"

After that, control is returned in a usable state. On the back end, a copy of the exception (traceback, etc.) is sent either by email or some pub/sub interface to the development team.

Basically, I want something like Hoptoad or Exceptional (Rails).

  1. Does such a library already exist?
  2. If not, what are the best email and/or pub/sub libraries to use to build it.


You might want to take a look at the Enterprise Library (EntLib) - The logging application block.

It does not automagically log exceptions but allows you to use (and create custom) sinks such as the event log and a database to store messages.


Redgates smartassembly has an exception reporter which may do the trick. If not rolling your own isn't too difficult.


We have used EntLib's logging in my previous team and it was quite useful, whenever exception was thrown it would log to DB and send email to us.

In my current team, we use simple Email exception feature, where we display the error and user can choose to send us the error (it also takes a screenshot of the screen along with complete log file, not jsut the exception which is sometimes useful)


// WPF might have another event, this one works for winforms
Application.ThreadException += OnUnhandledThreadException;

//console apps
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

// example method
static void OnUnhandledException(object sender, UnhandledExceptionEventArgs args) 
{
    Exception e = (Exception) args.ExceptionObject;

    SmtpClient client = new SmtpClient("my.smtp.server");
    var message = new MailMessage("support@mycompany.se", "mycoolapp@somewhere")
        { 
            Subject = "App failed",
            Body = e.ToString()
        };
    client.Send(message);

    MessageBox.Show("Whoops. App failed. Sorry. Goodbye!");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消