本文介绍了在调用点在Unity拦截方法完整的堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我们正在研究使用Unity来处理日志记录服务的方法拦截。然而,值得关注的是,完整的堆栈跟踪不可用在调用点;其仅提供了一个拦截器调用中。

We're looking into using Unity to handle logging service methods with interception. However, one concern is that the full stack trace is not available at the call site; its only available within an interceptor call.

下面是一个例子设置:

public interface IExceptionService
{
    void ThrowEx();
}

public class ExceptionService : IExceptionService
{
    public void ThrowEx()
    {
        throw new NotImplementedException();
    }
}

public class DummyInterceptor : IInterceptionBehavior
{
    public IEnumerable<Type> GetRequiredInterfaces()
    {
        return Type.EmptyTypes;
    }

    public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
    {
        IMethodReturn ret = getNext()(input, getNext);
        if (ret.Exception != null)
            Console.WriteLine("Interceptor: " + ret.Exception.StackTrace + "\r\n");
        return ret;
    }

    public bool WillExecute
    {
        get { return true; }
    }
}

class Program
{
    static void Main(string[] args)
    {
        IUnityContainer container = new UnityContainer();
        container.AddNewExtension<Interception>();

        container.RegisterType<IExceptionService, ExceptionService>(
            new Interceptor<InterfaceInterceptor>(),
            new InterceptionBehavior<DummyInterceptor>());

        try
        {
            container.Resolve<IExceptionService>().ThrowEx();
        }
        catch (Exception e)
        {
            Console.WriteLine("Call Site: " + e.StackTrace);
        }

    }
}

下面的控制台输出运行该程序:

Here's the console output from running that program:

Interceptor:
at ConsoleDemo.ExceptionService.ThrowEx() in    C:\code\ServiceDemo\ConsoleDemo\Program.cs:line 25
at DynamicModule.ns.Wrapped_IExceptionService_248fe3264f81461f96d34670a0a7d45d.<ThrowEx_DelegateImplementation>__0(IMethodInvocation inputs, GetNextInterceptionBehaviorDelegate getNext)

Call Site:
at DynamicModule.ns.Wrapped_IExceptionService_248fe3264f81461f96d34670a0a7d45d.ThrowEx()
at ConsoleDemo.Program.Main(String[] args) in C:\code\ServiceDemo\ConsoleDemo\Program.cs:line 63

在拦截器堆栈跟踪优良,将足以满足服务级别的日志记录。但是,我们失去了一切都会过去的拦截代理呼叫在调用点;

The stack trace in the interceptor is fine and will suffice for logging at the service level. However, we lose everything past the interception proxy call at the call site;

我可以在一个ServiceException或一些这样的拦截器包装的情况例外,那将保持在内部异常调用堆栈,但导致尴尬的日志和调试检查方案(尽管不那么尴尬比完全失去了跟踪)

I can wrap the exception in the interceptor in a ServiceException or some such, and that will keep the call stack in the inner exception, but that leads to awkward logging and debug inspection scenarios (though less awkward than losing the trace entirely).

我也注意到我们得到或多或少我们想要的东西,当我们使用TransparentProxyInterceptor,但指出为比InterfaceInterception慢,并且触发警钟对某些群体。

I've also noticed we get more or less what we want when we use the TransparentProxyInterceptor, but that is noted as being slower than InterfaceInterception, and that triggers alarm bells for some groups.

有没有清洁的方式来获得与统一截取完整的堆栈跟踪的上一个代理?

Is there any cleaner way to get a full stack trace with Unity interception at the call site on a proxy?

推荐答案

在.NET 4.5会有 ExceptionDispatchInfo 用于这一目的。

In .NET 4.5 there will be ExceptionDispatchInfo for this purpose.

对于所有其他版本,你可以看到这样一个问题:
In C#中,我怎样才能重新抛出的InnerException又不失堆栈跟踪?

For all other versions you can see this question:
In C#, how can I rethrow InnerException without losing stack trace?

这篇关于在调用点在Unity拦截方法完整的堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:30