MethodBoundaryAspect.Fody icon indicating copy to clipboard operation
MethodBoundaryAspect.Fody copied to clipboard

Exception logging only on highest call stack level?

Open jowliwon opened this issue 3 years ago • 1 comments

While using OnException to catch exceptions. I sort of want to be able to inform the user of things going wrong with a dialog. How ever, if the exception that gets thrown belongs to a sub in the call stack, the OnException will get triggered all the way to its origin place. Is there any proper way to keep track if this is the first triggered 'OnException' in the call stack chain? As in my case, there only needs to be one dialog triggered by this, and not lets say five, reporting on the same issue, just on different levels of the call stack.

public override void OnException(MethodExecutionArgs args) { //Some kind of if-statement //Show exception in dialog window. }

jowliwon avatar Jun 28 '22 10:06 jowliwon

I don't understand how there can be multiple exceptions in one user action. Do you mean when using concurrent/parallel method calls?

All in all I would prefer using the unhandled exception handler of your UI/message pump framework to intercept exceptions as last resort and then show the corresponding dialog. For WPF check this out: WPF unhandled exception handler

If you really want to track the call stack chain longer than the lifetime of the aspect then you have to use a static variable or enforce a policy that the instance of the current method call have an interface which provides a property to store this information. The stack trace can be retrieved via reflection and stack trace But this solution would be a big code smell for me :D

Ralf1108 avatar Jun 28 '22 12:06 Ralf1108