How do I log an exception when I don't know where it might throw?
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开发者_Python百科 of doing it?
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.
精彩评论