8

我正在更新一个使用 AutoFac 的遗留项目,并且我想将 NLog 与简单日志记录外观 (SLF) 一起使用

我过去曾在 Ninject 中使用过它,而且它很容易设置,我只需要执行以下操作:

kernel.Bind<ILogger>().ToMethod(x => LoggerFactory.GetLogger(x.Request.Target.Member.ReflectedType));

输出将类似于:

NLogNinjectSlf.Services.MyService 2013-12-30 15:21:10.5782 来自注入记录器的调试日志

小菜一碟

但是现在我必须使用 AutoFac 并且我不知道如何获取需要记录器的Target 类型

例如,如果我有以下接口/类:

public interface IMyService
{
    void DoSomething();
}

public class MyService : IMyService
{
    private readonly ILogger _logger;

    public MyService(ILogger logger)
    {
        _logger = logger;
    }

    public void DoSomething()
    {
        _logger.Debug("Log from injected Logger");
    }
}

我希望能够获取类的类型MyService以将其用作我的记录器的名称

在 AutoFac 中,这是我迄今为止尝试过的:

var containerBuilder = new ContainerBuilder();

containerBuilder.RegisterType<MyService>().As<IMyService>();

containerBuilder.Register(x =>
{
    // TODO: Get the correct type
    return LoggerFactory.GetLogger(x.GetType());
}).As<ILogger>();

顺便说一句:我在 SLF4Net 后面使用 NLog 并不是真正需要解决主要问题......

4

2 回答 2

10

感谢nemesv对我帮助很大

这是我最终使用的代码

顺便提一句。如果您愿意,您可以删除注入属性的代码,然后在所有类中使用 DI 来注入ILogger可以提高性能的代码

public class LoggingModule : Module
{
    protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry,
        IComponentRegistration registration)
    {
        registration.Preparing += OnComponentPreparing;

        registration.Activated += (sender, e) => InjectLoggerProperties(e.Instance);
    }

    private static void InjectLoggerProperties(object instance)
    {
        var instanceType = instance.GetType();

        // Get all the injectable properties to set.
        // If you wanted to ensure the properties were only UNSET properties,
        // here's where you'd do it.
        var properties = instanceType
          .GetProperties(BindingFlags.Public | BindingFlags.Instance)
          .Where(p => p.PropertyType == typeof(ILogger) && p.CanWrite && p.GetIndexParameters().Length == 0);

        // Set the properties located.
        foreach (var propToSet in properties)
        {
            propToSet.SetValue(instance, LoggerFactory.GetLogger(instanceType), null);
        }
    }

    private void OnComponentPreparing(object sender, PreparingEventArgs e)
    {
        var t = e.Component.Activator.LimitType;

        e.Parameters = e.Parameters.Union(
            new[]
            {
                new ResolvedParameter((p, i) => p.ParameterType == typeof (ILogger),
                    (p, i) => LoggerFactory.GetLogger(t))
            });
    }
}

然后注册模块:

containerBuilder.RegisterModule<LoggingModule>();
于 2014-01-04T07:25:17.840 回答
3

似乎使用 Autofac 4.3.0 你可以简单地使用 OnComponentPreparing() 回调而不使用特殊的属性注入技术。ctor() 和属性的值都将被注入:

public class LoggingModule : Module
{
 protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry,
        IComponentRegistration registration)
    {
        registration.Preparing += Registration_Preparing;
    }

private static void Registration_Preparing(object sender, PreparingEventArgs e)
    {
        var t = e.Component.Activator.LimitType;

        e.Parameters = e.Parameters.Union(
            new[]
            {
                new ResolvedParameter(
                    (p, i) => p.ParameterType == typeof (ILog),
                    (p, i) => LogFactory.CreateWithType(t))
            });
    }
}

我还发现了一些不错的技巧,仅在您需要控制记录器生命周期时才可用于属性注入。

public class LoggingModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        const string propertyNameKey = "Autofac.AutowiringPropertyInjector.InstanceType";

        builder.RegisterType<NLogLogger>()
            .As<ILog>()
            .OnPreparing(x =>
            {
                var firstParam = x.Parameters?
                    .OfType<NamedParameter>()
                    .FirstOrDefault(p => p.Name == propertyNameKey);

                if (null == firstParam)
                {
                    return;
                }

                var valueType = firstParam.Value;
                x.Parameters = x.Parameters.Union(
                    new[]
                    {
                        new ResolvedParameter(
                            (p, i) => p.Name == "type",
                            (p, i) => valueType)
                    });
            })
            .InstancePerDependency();

NLogLogger 本身在哪里

public class NLogLogger : ILog
{
    private readonly Logger _log;

    public NLogLogger()
    {
        _log = LogManager.GetCurrentClassLogger();
    }

    public NLogLogger(Type type)
    {
        _log = LogManager.GetLogger(type.FullName);
    }
}
于 2017-03-09T09:24:52.587 回答