During an unhandled exception 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).
Your best chance is inside the application. There are two hooks:
AppDomain.UnhandledExceptionis the ultimate ‘catch-all’Application.ThreadExceptionis 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.