【发布时间】:2011-06-22 18:40:26
【问题描述】:
我正在尝试记录从 UI(DNN 模块)到它使用的一些各种服务的调用,以分析人们与网站的交互方式。我正在使用 StructureMap 2.5.3.0 和 Log4Net
我在单个类/实例对上运行良好,但我必须这样配置:
ObjectFactory.Configure(ce =>
ce.ForRequestedType<IRegService>()
.TheDefaultIsConcreteType<RegService>()
.EnrichWith(LoggingEnrichment.InterfaceLogger<IRegService>));
IRegService 两次感觉有点乱,但我可以忍受。
日志记录是这样实现的:
public class LoggingEnrichment
{
public static object InterfaceLogger<TInterface>(object concrete)
{
return InterfaceLogger(typeof(TInterface), concrete);
}
public static object InterfaceLogger(Type iinterface, object concrete)
{
var dynamicProxy = new ProxyGenerator();
return dynamicProxy.CreateInterfaceProxyWithTarget(iinterface, concrete, new LogInterceptor());
}
}
public class LogInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
var watch = new Stopwatch();
watch.Start();
invocation.Proceed();
watch.Stop();
ILog logger = LogManager.GetLogger(typeof(LogInterceptor));
var sb = new StringBuilder();
sb.AppendFormat("Calling: {0}.{1}\n", invocation.InvocationTarget.GetType(), invocation.MethodInvocationTarget.Name);
var param = invocation.Method.GetParameters();
if (param.Length > 0) sb.Append("With:\n");
for (int i = 0; i < param.Length; i++)
{
sb.AppendFormat("\t{0}\n\t\t{1}", param[i].Name, invocation.GetArgumentValue(i));
}
if(invocation.Method.ReturnType != typeof(void))
{
sb.AppendFormat("Returning: {0}\n", invocation.ReturnValue ?? "null");
}
sb.AppendFormat("In: {0}ms\n", watch.ElapsedMilliseconds);
logger.Debug(sb.ToString());
}
}
这可行,但有几个问题:
- 我必须手动配置每个服务接口对
- 我只想在从 UI 调用服务时连接日志记录
我试图通过为 StructureMap 实现 TypeInterceptor 来解决这个问题:
public class ApplicationRegistry : Registry
{
public ApplicationRegistry()
{
RegisterInterceptor(new LoggingInterceptor());
Scan(scanner =>
{
scanner.TheCallingAssembly();
var codeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", String.Empty);
codeBase = codeBase.Substring(0, codeBase.LastIndexOf("/"));
scanner.AssembliesFromPath(codeBase);
scanner.WithDefaultConventions();
scanner.LookForRegistries();
});
}
}
public class LoggingInterceptor :TypeInterceptor
{
public object Process(object target, IContext context)
{
var newTarget = target;
if (context.BuildStack.Current != null && context.BuildStack.Current.RequestedType != null)
{
newTarget = LoggingEnrichment.InterfaceLogger(context.BuildStack.Current.RequestedType, target);
}
return newTarget;
}
public bool MatchesType(Type type)
{
return type.Name.EndsWith("Service", StringComparison.OrdinalIgnoreCase);
}
}
但是我遇到了一个问题,即对Process 的调用给了我一个没有实现由构建上下文定义的接口的类。这导致不得不将InterfaceLogger 的实现更改为
public static object InterfaceLogger(Type iinterface, object concrete)
{
if(!iinterface.IsAssignableFrom(concrete.GetType())) return concrete;
var dynamicProxy = new ProxyGenerator();
var interfaceProxy = dynamicProxy.CreateInterfaceProxyWithTarget(iinterface, concrete, new LogInterceptor());
return interfaceProxy;
}
永远不会到达return interfaceProxy; 上的断点,这表明context.BuildStack.Current.RequestedType 没有返回正确的接口。奇怪的是,我所有的类似乎都被正确注入了。
此外,即使这有效,我仍然会遇到只想拦截来自 UI 层的调用的问题。
我正在寻找我的前 2 个问题的方法,以及我在 TypeInterceptor 上做错了什么
【问题讨论】:
-
嗨,遇到了同样的问题。你解决了吗? Tnx。
-
您好,我下面的回答解决了您的问题吗? Tnx。
标签: logging structuremap castle-dynamicproxy