0

I have an application (written in C# / ClickOnce) which, for the most part, works fine; it has no memory leaks and runs reliably and is stable for days at a time.

However, it also utilises MEF (so plugins/extensions can be dynamically added to the core assembly). Again, this 'works' currently, but if an exception/fatal error occurs in an externally linked assembly/plugin, it will crash the entire application.

After some recent testing, I found that the application had crashed after around 14 hours of [successful] operation.

With that in mind, my question is really two-fold:

a) is it possible to catch any unhanded exception a plugin (or the main application) may throw, so it can at least output info for debugging assistance?

b) I can't be sure if it was the plugin or the main application which failed. Therefore, I cannot think where to start debugging/tracing the issue. How does one go about finding a bug which only occurs after such a long period of time?

Thanks for any input.

Alfie
  • 2,341
  • 2
  • 28
  • 45
  • 1
    It will depend how the plugin is integrated in your main app, mostly. – Crono Apr 07 '14 at 23:42
  • 4
    First advice, use log4net or logging library alike. Secondly, attach a handler to `AppDomain.CurrentDomain.UnhandledException`. It will be fired any time an unhandled exception occurs. In the exception you always have a stack trace. It will give you precise details about the location of the error. – Oybek Apr 07 '14 at 23:43
  • @HansPassant e.ExceptionObject.ToString() is not enough. The details like stack trace are essential. Moreover, the actual problem can be described in inner exceptions. – Oybek Apr 07 '14 at 23:44
  • If you want to support external plugins it's important you sandbox them so that they can never hurt the normal processing of your application. – Crono Apr 08 '14 at 00:01

1 Answers1

1

As noted in the comments (I guess I should have read those before I started typing up an answer...)

When an application throws an exception that is unhandled, it fires the UnhandledException event just before death and you can subscribe to that in order to log any details that will help you figure out what happened.

See this SO post for an example on how to use it (includes ThreadException).

Community
  • 1
  • 1
David
  • 4,665
  • 4
  • 34
  • 60