Interception is changing stack traces

Jul 1, 2009 at 9:40 PM

 

If the implementation class that I’m wrapping in an interceptor (InterfaceInterceptor) throws an exception, then my unit test prints an exception that has a stack trace rooted in the dynamically generated wrapper class rather than the actual stack trace. I found that if I wrap the exception in another exception in my call handler, then I am able to see the correct stack trace. Is this a known issue? Something I’m doing wrong? I’m new to this stuff.

         public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)

        {
                var result = getNext()(input, getNext);
                var exception = result.Exception;
                if (exception != null)
                {
                    //Wrap the exception so the stack trace is preserved
                    result.Exception = new Exception(exception.Message, exception);
                }
                return result;
        }

 

Oct 28, 2009 at 7:51 PM

The problem is that the stack trace gets regenerated at each throw. The only way to completely avoid the issue is to use private reflection to set the stack trace object, which has lots of problems with security and is very slow.