Is there a way to log or intercept First Chance Ex

2020-02-13 12:22发布

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.

3条回答
我只想做你的唯一
2楼-- · 2020-02-13 12:40

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.

查看更多
Juvenile、少年°
3楼-- · 2020-02-13 12:41

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.

查看更多
干净又极端
4楼-- · 2020-02-13 12:47

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.

查看更多
登录 后发表回答