13

我想注册一个组件以根据它可能正在解析的类使用参数解析。(这听起来有点令人困惑,所以我将展示一个示例)。

这是一个使用记录器的对象:

class MyObject : IMyObject
{
    public ILogger Logger;
    public MyObject(ILogger logger)
    {
        Logger = logger;
    }
}

现在传入的记录器可能因类而异。因此,对于如何在下面执行此操作,我有一个相当完善的想法:

class MyLogger : ILogger
{
    public string Name{get; protected set;}

    public static ILogger GetLogger(string className)
    {
        Name = className;
        MyLogger logger;
        // Do something to choose a logger for that specific class
        return logger;
    }
}

因此,当我注册 Logger 时,我希望能够告诉它类名。我希望有一种方法可以做到这一点:

ContainerBuilder builder = new ContainerBuilder();

builder.Register<MyLogger>(ctx =>
    {
        string className = //Get resolving class name somehow;
        return MyLogger.GetLogger(className);
    }).As<ILogger>();

builder.Register<MyObject>().As<IMyObject>();

var container = builder.Build();

IMyObject myObj = container.Resolve<IMyObject>();

//myObject.Logger.Name should now == "MyObject"

我想这样做的原因是避免在代码中使用带有 autofac 的记录器注册我实现的每个类。我希望能够在 xml 中注册所有对象,并且只需有一个 LoggerModule 即可添加此注册。

提前致谢!

4

1 回答 1

16

Here's what I do (ILog is just my own wrapper around log4net):

public class LoggingModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.Register((c, p) => GetLogger(p.TypedAs<Type>()));
    }

    protected override void AttachToComponentRegistration(
        IComponentRegistry registry, IComponentRegistration registration)
    {
        registration.Preparing +=
            (sender, args) =>
            {
                var forType = args.Component.Activator.LimitType;

                var logParameter = new ResolvedParameter(
                    (p, c) => p.ParameterType == typeof (ILog),
                    (p, c) => c.Resolve<ILog>(TypedParameter.From(forType)));

                args.Parameters = args.Parameters.Union(new[] {logParameter});
            };
    }

    public static ILog GetLogger(Type type)
    {
        return new Log4NetLogger(type);
    }
}

public interface ILog
{
    void Debug(string format, params object[] args);
    void Info(string format, params object[] args);
    void Warn(string format, params object[] args);

    void Error(string format, params object[] args);
    void Error(Exception ex);
    void Error(Exception ex, string format, params object[] args);

    void Fatal(Exception ex, string format, params object[] args);
}

public class Log4NetLogger : ILog
{
    private readonly log4net.ILog _log;

    static Log4NetLogger()
    {
        XmlConfigurator.Configure();
    }

    public Log4NetLogger(Type type)
    {
        _log = LogManager.GetLogger(type);
    }

    public void Debug(string format, params object[] args)
    {
        _log.DebugFormat(format, args);
    }

    public void Info(string format, params object[] args)
    {
        _log.InfoFormat(format, args);
    }

    public void Warn(string format, params object[] args)
    {
        _log.WarnFormat(format, args);
    }

    public void Error(string format, params object[] args)
    {
        _log.ErrorFormat(format, args);
    }

    public void Error(Exception ex)
    {
        _log.Error("", ex);
    }

    public void Error(Exception ex, string format, params object[] args)
    {
        _log.Error(string.Format(format, args), ex);
    }

    public void Fatal(Exception ex, string format, params object[] args)
    {
        _log.Fatal(string.Format(format, args), ex);
    }
}
于 2012-07-27T16:19:21.070 回答