Nic*_*ick 5 .net unity-container unity-interception
我们正在研究使用Unity来处理带有拦截的日志服务方法.但是,一个问题是呼叫站点没有完整的堆栈跟踪; 它只在拦截器调用中可用.
这是一个示例设置:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是运行该程序的控制台输出:
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
Run Code Online (Sandbox Code Playgroud)
拦截器中的堆栈跟踪很好,足以在服务级别进行日志记录.但是,我们在呼叫站点的拦截代理呼叫之后丢失了所有内容;
我可以在ServiceException或类似的东西中将异常包装在拦截器中,这将使调用堆栈保持在内部异常中,但这会导致笨拙的日志记录和调试检查场景(尽管不如完全丢失跟踪那么尴尬).
我还注意到,当我们使用TransparentProxyInterceptor时,我们或多或少会得到我们想要的东西,但是这被认为比InterfaceInterception慢,并且会触发某些组的警钟.
是否有更简洁的方法在代理的呼叫站点上使用Unity拦截获得完整的堆栈跟踪?
在 .NET 4.5 中,将有ExceptionDispatchInfo用于此目的。
对于所有其他版本,您可以看到这个问题:
In C#, how can I re throw InnerException without Losing stack track?
| 归档时间: |
|
| 查看次数: |
2135 次 |
| 最近记录: |