Brian Adams
Brian Adams

Reputation: 996

Is there a way to log or intercept First Chance Exceptions

Short of using a profiler, is there any way inside a running program to detect first chance exceptions? Ideally, I would like to record more detailed state information that is not available once the catch block has taken over the final exception.

Upvotes: 9

Views: 1978

Answers (3)

jlnorsworthy
jlnorsworthy

Reputation: 3974

Now, in .NET Framework 4.0, you can subscribe to the FirstChanceException event of the AppDomain. It is only an event, so you can't handle the error, but it seems to be a good, central way to get information on exceptions whether they are handled or not. The FirstChanceException event is thrown before a catch block is allowed to handle it. I haven't found a lot of information on it, but aside from the microsoft documentation, one of the better sources is Mitch Sellers Blog.

Upvotes: 20

Garo Yeriazarian
Garo Yeriazarian

Reputation: 2533

I think the only way you can get that information in .NET is using a Debugger.

Otherwise, you'll have to develop a solution yourself for saving the state of a stackframe and having a special way to log exceptions. You'd basically be doing the same things that a memory profiler does, keep track of the instances that are created. This would be a huge performance hit though unless you limit the amount of information you are logging.

A better solution would be to use the Trace and Assert capabilities in the System.Diagnostics namespace to selectively trace the program state, or to use a logging facility (log4net, EnterpriseLibrary, NLog, roll your own simple one) to dump thread / stack / variable information as you go.

In any case, adding all this extra information is a big overhead.

EDIT: I got news of this project in my feed: NTrace. It looks like it will fit a little more of what you're trying to do.

Upvotes: 2

Tal
Tal

Reputation: 1769

Use Adplus. It will attach a debugger to the proccess, and generate (by default) a small minidump when first chance excpetion are raised. Adplus generated log file will also contain exception info. Just make sure you have PDB's aviable to see full calstack info.

Upvotes: 1

Related Questions