You can try the global try / catch method, except that if there is an exception in the background thread, it will not be detected. You can use AppDomain.UnhandledExceptionit if you want to be notified of any unhandled exception in appdomain ( msdn ). You should register mainly before the rest of your program runs like this:
static void Main(string[] args)
{
AppDomain.UnhandledException += WriteUnhandledExceptionToFile;
}
static void WriteUnhandledExceptionToFile(object sender, UnhandledExceptionEventArgs args)
{
string path = Path.Combine(Environment.CurrentDirectory, "UnhandledException.txt");
File.WriteAllText(path, args.ExceptionObject.ToString());
}
Edit
Note that by default, Windows Forms and WPF catch all exceptions that occur in the user interface thread. You will need to subscribe to the Application.ThreadException event (forms) or the Application.DispatcherUnhandledException event (wpf) in order to receive notification of exceptions from these threads. The code will be very similar to the code above for the AppDomain event.