2014-08-27 27 views
0

我使用最新的Castle Windsor容器,并且我想创建一个记录器,它将写入文件以记录我想要的内容,同时也编写调用者程序集名称。所以这里有一个例子:确定哪个类或程序集实例化与Castle Windsor容器的对象

namespace Core.Datalayer 
{ 
    public class Foo 
    { 
     public Foo(ILogger logger) 
     { 
      logger.Write("test line"); 
     } 
    } 
} 

容器正确地给出了实例,但我想不通我怎么能确定呼叫者集名称或来电类的名字吗?我想,该记录器写入文件是这样的:

“组装Core.Datalayer-检测线”

是否有可能以确定其中实例化的ILogger接口?

我试过Assembly.GetCallingAssembly(),但始终是Castle.Core,因为堆栈已满Castle.Core调用。

+0

? log4net,nlog,别的? – 2014-08-27 19:16:49

+0

我不使用任何框架来记录它只是一个简单的记录器。我直接在文件中写入文本。 – 2014-08-27 19:18:15

+0

可能重复[如何找到调用当前方法的方法?](http://stackoverflow.com/questions/171970/how-can-i-find-the-method-that-c​​alled-the-current -method) – 2014-08-27 19:18:52

回答

2

您可以使用子依赖关系解析器。我认为像下面的代码应该适合你。

public class Logger 
{ 
    public Logger(string name) 
    { 
    } 
} 

public class LogResolver : ISubDependencyResolver 
{ 
    public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, 
          DependencyModel dependency) 
    { 
     return dependency.TargetType == typeof(Logger); 
    } 

    public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, 
          DependencyModel dependency) 
    { 
     return new Logger(model.Name); 
    } 
} 

安装记录仪的int容器

container.Kernel.Resolver.AddSubResolver(new LogResolver()); 
您正在使用哪个日志框架
+0

非常感谢!我差不多有一天在挣扎着。这就是我想要的! :) – 2014-08-28 07:02:29

相关问题