darbid
darbid

Reputation: 2731

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.

Upvotes: 0

Views: 525

Answers (1)

Lee Willis
Lee Willis

Reputation: 1582

Have you tried to add

AppDomain.CurrentDomain.UnhandledException += AppDomain_UnhandledException;

As well as your Application_DispatcherUnhandledException?

Upvotes: 2

Related Questions