我正在尝试记录从 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
to
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 层的调用的问题。
我正在寻找我的前两个问题的方法,以及我做错了什么TypeInterceptor