简体   繁体   中英

How to catch all Exceptions in a WPF program so that TextWriterTraceListener writes it to the log before crashing

I have a pretty normal WPF program (in VB.net but same should be no different to C#) and have implemented the TextWriterTraceListener which writes everything to a log file. The issue I have is that if there is an exception that is uncaught then my app crashes and the log file seems to be "behind" so it does not show the very last entry before crashing.

  1. Can I somehow make the TextWriterTraceListener write to the log file faster?

  2. I have implemented the Application_DispatcherUnhandledException but this does not seem to catch my unhandled errors.

I am aware that on a proper close down I have to call flush and close on the TextWriterTraceListener object, the issue is that if my app crashes I don't get the opportunity to do this.

Have you tried to add

AppDomain.CurrentDomain.UnhandledException += AppDomain_UnhandledException;

As well as your Application_DispatcherUnhandledException ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM