So lets say I happen to have an unhandled exception in my application or it crashes for some reason. Is there some way for me to capture the output and show an error report dialog when the application crashes.
What I'm thinking is having a small program running in the background, which only job is to listen for an abnormal exit of the main application and then show the 'report' dialog where the user could choose to email me the output of the error.
Not really sure how to implement this, or if this is the right way to do it.
Reporting the error message would be an easy task, but I have no idea how to capture the output of an unhandled exception or grab the exit codes (I'm assuming the program would give an exit code other then 0 when it crashes).
Would be great to have a nudge in the right direction.
You should handle the
Application.ThreadException
event.You can use NBug library for that (also you can use the nuget package here for easy installation). Simply install the NuGet package and configure it like below:
Now all unhandled exceptions will be nicely caught and giftwrapped for you and you'll get full details of the exception via email. Use the configurator tool to make sure that your configuration is correct.
Your best chance is inside the application. There are two hooks:
AppDomain.UnhandledException
is the ultimate 'catch-all'Application.ThreadException
is the UI specific catch-all for exceptions that occurred in Forms threadsThe proper place to 'catch-all' depends on your application semantics and is difficult to say where you should put it w/o knowing your application. Application need to also set the
Application.SetUnhandledExceptionMode
.Having an external watch dog is less useful because it cannot give any meaningful information why did the application crash. By the time it detect an 'unexpected' exit (how does it knows is 'unexpected'?) is way too late to collect any useful information. With an inside handler you can collect the exception and the stack and submit them to an analysis service like bugcollect.com and then you'll have a leg ahead in understanding now only what happened, but also how often it happens and which deployment are affected (where it happens). There are other similar services like exceptioneer.com or the Windows Error Reporting (this one requires your code to be signed by a trusted authority certificate like Verisign). Relying on a service for collection of incidents is far superior to sending mail, you don't want to wake up and find 2k incident emails in your inbox and start sifting through them to understand what happened.
And a final world: don't reinvent the wheel: there are already many frameworks to collect and log exceptions, like log4net and elmah.
The answers involving the
AppDomain.UnhandledException
event probably make an implicit assumption any unhandled exception is raised on WPF's UI thread. This means that, while they'll often work, they're not formally safe for use with operations on other threads. A more reliable option isApplication.DispatcherUnhandledException
, which WPF provides for applications to implement custom reporting of unhandled exceptions across the main UI thread, background UI threads, andBackgroundWorker
instances.A likely failure point with
AppDomain.UnhandledException
is the report dialog probably requires a single-threaded apartment (STA) since both WPF and Windows Forms are STA. As thread pool threads default to multithreaded apartments, an unhandled exception from an asynchronous operation underTask.Run()
,ThreadPool.QueueUserWorkItem()
,IProgress<T>.Report()
, or the many similar APIs will result upon instantiation of a report dialog failing with something like the exception below. This causes an application crash without a chance to prompt the user to report the underlying problem.My experience of
Application.DispatcherUnhandledException
is it's robust in combination with the TPL asTask
and its associated classes facilitate propagation of exceptions back to the caller. To summarize the TPL's exception handling,Wait()
andawait
rethrow automatically and callers using other synchronization methods should checkTask.Exception
.However, as
Application.DispatcherUnhandledException
's documentation points out, other cases require WPF's caller to implement exception propagation. Perhaps the most common of these is WPF'sProgress<T>
, which strangely lacks support for propagating exceptions from within its implementation ofIProgress<T>.Report()
even though its sole purpose is moving progress information from worker threads back to the UI. One workaround is to wrap the progress update handler using an approach similar to the example below. This is just a sketch; more frequent polling of theException
property may be valuable in halting on errors, the stronger semantics ofIDisposable
may be preferable toEnd()
, and it might be useful to handle cases where a backlog of updates fail concurrently differently.