【发布时间】:2010-10-09 02:10:13
【问题描述】:
所以,我遇到了这个问题,似乎没有人能够提供帮助。因此,与其继续抨击,我将把它扔出去,寻找其他方法给这只猫剥皮。
我目前有以下:
public interface ICustomerService
{
Customer GetCustomer(int id);
}
public class CustomerService : ICustomerService
{
public Customer GetCustomer(int id)
{
...
}
}
...在 Unity 中,我设置了 IOC,同时配置拦截,例如:
IUnityContainer ioc = new UnityContainer();
ioc.RegisterType<ICustomerService, CustomerService>()
.Configure<Interception>()
.SetInterceptorFor<ICustomerService>(new InterfaceInterceptor());
我想要实现的是能够像这样在界面中放置属性:
public interface ICustomerService
{
[Log]
Customer GetCustomer(int id);
}
... 定义如下:
public class LogAttribute: HandlerAttribute
{
public override ICallHandler CreateHandler(IUnityContainer container)
{
return new LogHandler();
}
}
...然后在 LogHandler 类中执行我想要的所有日志记录:
public class LogHandler : ICallHandler
{
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
... log stuff
}
}
我想要实现的是一个跟踪/日志记录系统,其中处理程序记录正在调用的 namespace.class.methodname,以及调用它的父 namespace.class.methodname。我尝试使用“输入”IMethodInvocation 参数来获取我想要的信息但没有成功,问题是,输入返回“ICustomerService”接口,同时检查父级的堆栈帧返回父级的实现类(例如.CustomerService) 意味着当我尝试使用 namespace.class.methodname 作为实体 ID 创建树结构时,ID 和 parentID 不匹配。
将参数放入 [Log] 属性也不会真正起作用,因为我可以在其中放入什么?如果我输入接口名称,我仍然遇到与上面相同的问题,其中一个的 ID 是一个接口,而父级是实现类。而且,我不能将实现类名放在接口的属性中,因为这违背了首先拥有接口的目的!
所以,这就是两难境地。有人有新想法吗?
【问题讨论】:
标签: c# logging unity-container aop unity-interception