Thursday, February 17, 2011

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.

From stackoverflow
  • What information is it that you lose?

    Brian Adams : Happy to be corrected: If you don't handle all exceptions in each method and make sure all variables are defined outside the try-catch block then at the point the final exception is raised, you won't have access to all variables that were available in the execution context of the original exception.
  • 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.

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

0 comments:

Post a Comment