5

I have been investigating the best way to handle all the exceptions of an application without messing much with the code. The main objective here is to send information about the exceptions to an external platform such as Application Insights. So far I've found the following methods:

  • Castle Interceptor:
    This is the best approach so far, the thing is that, for the methods to be intercepted either the methods are virtual, or the class must be interfaced. Since I'm working on a really big application these changes are not desired.

  • Events:
    Using AppDomain UnhandledException is also to be considered, but since I have several app domains that would require a lot of changes a messing with classes only for the exceptions, which is not optimal since classes should not be messed just because of exception handling. Besides the number of AppDomains I also have several threads running from which exceptions are not caught by this kind of handlers.

  • PostSharp:
    PostSharp works similarly to Castle, and the problem here if I understood correctly, is that I would have to add attributes/decorators to all the methods I want intercepted, also not a very good approach.


If anyone has any suggestions on the best approach here I would be very appreciated.

Jorge Lima
  • 157
  • 15
  • You don't necessarily _want_ to handle all exceptions of an application. Some exceptions are good... they tell you when your program is doing something it shouldn't be doing. – Abion47 May 25 '17 at 09:19
  • @Abion47: Capturing all exceptions is good. Sometimes you want to throw them out again and not resume program flow but capturing them so you can log them properly at the very least is good. – Chris May 25 '17 at 09:20
  • The point here is to send information about the exceptions to Application Insights, not necessarily to eat the exception, I could rethrow it. Just as @Chris reffered :) – Jorge Lima May 25 '17 at 09:20
  • This quest of mine might be helpful https://stackoverflow.com/q/30326673/495455 – Jeremy Thompson May 25 '17 at 09:20
  • If the change is simple enough (e.g. call a static method in each catch), maybe you can use Mono.Cecil https://github.com/jbevain/cecil to modify your assemblies .. or even use it to add the attributes requires by PostSharp .. not sure if either is a feasible solution, just a thought – KMoussa May 25 '17 at 12:33
  • Another option might be fody.anotator, you might face the same problem as with PostSharp, but since you are free to modify the plugin it should be possible to do something like this with a configuration file. – Marwijn May 25 '17 at 20:43

1 Answers1

-1

There is a fine article located at https://dncmagazine.blob.core.windows.net/edition30/DNCMag-Issue30.pdf that discusses error handling in large projects. Probably the least intrusive approach would be to use the global exception handler. I would also suggest looking at using a library such as log4net as this can record exception details using multiple stores (local file, SQL, .....) and can be reconfigured from config files, thus avoiding code changes, recompiling and application distribution/installation.

For those not familiar with DNCMag - it is a FREE magazine for coders with many excellent articles and can be viewed at http://www.dotnetcurry.com/magazine/

Ian
  • 114
  • 4