使用带有目标对象信息的 MEF 注入对象

Inject object using MEF with information about destination object

我正在寻找一种使用 MEF 将 log4net 记录器包装到各种对象中的日志对象注入方法。我目前遇到的问题是日志记录对象需要它所属的对象的类型。我可以通过在包含对象的构造函数中的日志记录对象上设置类型 属性 来解决这个问题,但是这让开发人员有责任设置类型,而且我认为没有编译时间限制的强制执行。

我有没有办法指定当日志记录对象由MEF生成并注入时,其构造函数参数设置为注入目标class的类型?

我的记录器实现了一个接口

public interface ILogger
{
    Type Type { get; }
}

具体实现的一个例子是

[Export(typeof(Ilogger))]
public class SimpleLogger : ILogger
{
    public SimpleLogger(Type typeOfObjectToLogFor)
    {
         this.Type = typeOfObjectToLogFor
    }

    public Type Type { get; }

    public void Info(string message)
    {
        //log the messsage including the type information
    }
}

并且当前不使用 MEF 使用它作为:

public class ExampleObject
{
    private readonly ILogger logger = new SimpleLogger(typeof(ExampleObject));

    public ExampleObject(){}

    public void MethodThatLogs()
    {
        logger.Info("MethodThatLogs called");
    }
}

我想做的是使用构造函数注入来注入它:

public class ExampleObject
{
    private readonly ILogger logger;

    [ImportingConstructor]
    public ExampleObject(Ilogger injectedLogger)
    {
        logger = injectedLogger;
    }

    public void MethodThatLogs()
    {
        logger?.Info("MethodThatLogs called");
    }
}

我可以用惰性评估反射来完成所有这些,但感觉应该可以从一个像样的 DI 容器中实现,希望这意味着 MEF 会支持它,有人帮忙吗?

默认情况下,指定 [Export] 属性,您将 PartCreationPolicy 设置为 Shared,这意味着容器将为您的导出创建一个单例 - 您的记录器。

但我建议您不导出 class 而是导出一个接受一个参数并为您创建记录器的工厂方法。

class LoggerFactory
{
    [Export("GetLogger")]
    public ILogger GetLogger(Type type)
    {
        return new SimpleLogger(type);
    }
}

class ExampleObject
{
    private readonly ILogger logger;

    [ImportingConstructor]
    public ExampleObject([Import(ContractName = "GetLogger", AllowDefault = true)]Func<Type, ILogger> loggerCreator)
    {
        logger = loggerCreator?.Invoke(this.GetType());
    }
}