How to log all thrown exceptions?

Poma picture Poma · Aug 16, 2011 · Viewed 15.2k times · Source

How to log any exceptions that were thrown and catched? Something like Visual Studio's IntelliTrace do. Or is there a way to integrate InteliTrace into debug version of application and then view its logs?

Update: I'll clarify that a bit. I want standard .txt (or any custom) logs, the format doesn't matter. The main point is I want to log all exceptions that occurred in all third-party libraries without adding code to them.

Answer

yas4891 picture yas4891 · Aug 16, 2011

I guess the feature you are searching for is called FirstChanceException and can be accessed via the AppDomain.FirstChanceException Event

Essentially this event provides a hook to get information about any (managed) exception getting thrown in the AppDomain. You can not handle the Exception this way! It is only a sort of notification


Update: regarding your comment about a "swallowed exception" on another answer - and just a shot into the dark:
On x64 systems exceptions that get thrown in a windows' onLoad method can not be caught in your Main() method.
see this SO article for reference


Update 2: As for Threads I think that you would have to implement it yourself. This would involve some sort of polling and would harm performance, but I guess for debugging it is OK in most cases. This could be done using

var threads = Process.GetCurrentProcess().Threads;