所以,我遇到了这个问题,似乎没有人能够帮助我。因此,我不会继续猛烈抨击,而是将其抛出,寻找其他方法来剥这只特定的猫的皮。
我目前有以下内容:
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。我尝试过使用" input“IMethodInvocation参数来获取我想要的信息,但没有成功,问题是,input返回"ICustomerService”接口,而检查父对象的堆栈框架则返回父对象的已实现类(例如.CustomerService)意味着当我尝试使用namespace.class.methodname作为实体的ID来创建树结构时,ID和parentID不匹配。
把一个参数放到Log属性中也是行不通的,因为我能在里面放什么呢?如果我输入接口名称,我仍然会遇到与上面相同的问题,其中一个接口的ID是一个接口,而父接口是实现类。而且,我不能将实现类名放在接口的属性中,因为这违背了最初拥有接口的目的!
所以,这就是问题所在。有人有新点子吗?
发布于 2009-12-01 23:04:51
我最终使用PostSharp来实现日志记录,就像这样。http://www.postsharp.org
发布于 2010-10-09 02:10:14
我已经使用Unity和Interception进行了日志记录工作。由于我严重缺乏配置设置技能,我不得不通过编程来完成它。您需要设置至少一个拦截器以及一个或多个策略对象。哦,是的,UnityContainer.Configure<Interception>是至关重要的。
有点像这样:
// I'm using the TransparentProxyInterceptor because I want to trace EVERYTHING...
var intp = myUnityContainer.Configure<Interception>().
SetInterceptorFor(typeof(MyTypeToLog), new TransparentProxyInterceptor());
var policy = intp.AddPolicy("somePolicyName");
policy.AddMatchingRule<TypeMatchingRule>(
new InjectionConstructor(
new InjectionParameter(typeof(MyTypeToLog)))
.AddCallHandler(typeof(MyCallHandler),
new ContainerControlledLifetimeManager());当然,我还需要定义拦截调用处理程序:
public class MyCallHandler : ICallHandler, IDisposable
{
public IMethodReturn Invoke(IMethodInvocation input,
GetNextHandlerDelegate getNext)
{
var methodReturn = getNext().Invoke(input, getNext);
// log everything...
LogMethodCall(input, methodReturn);
// log exception if there is one...
if (methodReturn.Exception != null)
{
LogException(methodReturn);
}
return methodReturn;
}
}https://stackoverflow.com/questions/1826702
复制相似问题