开发者

How to catch all un-handled exceptions in a .net assembly (library type, not application)

I have an assembly containing a number of classes. This is a class library type assembly, not a windows f开发者_高级运维orms application. It's also single threaded.

Is there a way to catch all un-handled exceptions so that I can log them?


In my opinion, putting such logic inside library is not a good idea. I think it should be application responsibility to decide how to deal with exceptions (both handled and unhandled). However you might look at AppDomain.UnhandledException. You can install such handler for CurrentDomain and do something there. However doing this way you are restricting usages of you library (for example implying that library would be used only in one domain). Also you will receive notifications for all unhandled exceptions even totally unrelated to your assembly.

I think that better idea is to allow developers that use your library to do their job dealing with all unhandled exceptions (possibly with UnhandledException installed by the application).


You can use AppDomain.UnhandledException Event to catch unhandled exceptions globally:

http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx

Or use try/catch for every assembly call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜