Exception reporting from a WPF Application

后端 未结 4 1617
刺人心
刺人心 2021-02-05 13:09

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 dial

4条回答
  •  梦毁少年i
    2021-02-05 13:35

    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 threads

    The 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.

提交回复
热议问题