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.
相关问题
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
- Bulk update SQL Server C#
- Should I use static function in c# where many call
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.
I was googling FirstChanceException, and I can't resist answering this more than two years later...
Now, in .net 4.0, you can catch the FirstChanceException event of the AppDomain. It is an event only, 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.