Crate an unhandled exception handler like below:
static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args)
{
Exception ex = (Exception)args.ExceptionObject;
UtilGui.LogException(ex);
}
static void ApplicationThreadUnhandledExceptionHandler(object sender, System.Threading.ThreadExceptionEventArgs args)
{
Exception ex = (Exception)args.Exception;
UtilGui.LogException(ex);
}
and register it in your Main
method like this:
// Add the event handler for handling UI thread exceptions to the event.
Application.ThreadException += new ThreadExceptionEventHandler(ApplicationThreadUnhandledExceptionHandler);
// Set the unhandled exception mode to force all Windows Forms
// errors to go through our handler.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// Add the event handler for handling non-UI thread exceptions to the event.
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomainUnhandledExceptionHandler);