We have a WPF application that we've deployed to the customer. However, the application is randomly fatally crashing with an "unexpected error" dialog box on the customer's computer. We need to be able to see the exception message and preferably stack trace as well, but because it's crashing randomly, we don't know where we should be putting the try/catch checks so we can log it. We are using NLog for logging. My question is, is there any way for us to either configure NLog to catch all exceptions not handled by our code, or some other method of doing it?
+1
A:
Why not putting a global try/catch block, covering the whole code?
Since it is a WPF application, some changes may be required. Instead of letting WPF open the main window automatically, remove App.xaml file, and open the window through code from the application start point (inside try/catch).
Note that this is not a good practice (like it's generally bad to catch all exceptions), but may be a temporary workaround to find where the crash occurs.
Edit: I also invite you to read an answer to a related question about global try/catch blocks in WPF. There is some useful stuff I didn't know when writing my answer.
MainMa
2010-09-04 01:06:36
Thanks, hooking into the event worked great!
Daniel T.
2010-09-04 03:08:56
Actually, it's **good** to catch all exceptions, so long as you don't just ignore them. You want a top-level try/catch block that logs the fatal exception before shutting down.
Steven Sudit
2010-09-09 09:30:02