AutoFac和Log4Net-注册和使用 [英] AutoFac and Log4Net - Registering and using

查看:313
本文介绍了AutoFac和Log4Net-注册和使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被指示要在log4net旁边使用AutoFac(而不是Castle Windsor),而对如何正确地一起使用这些技术一无所知.

I have been instructed on using AutoFac (instead of Castle Windsor) along side log4net and am lost on how to correctly use these technologies together.

使用autofac网站上的示例,我具有以下LoggerModule

Using the example on the autofac website I have the following LoggerModule

public class LoggingModule : Module
{
    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(ILog) && p.CanWrite && p.GetIndexParameters().Length == 0);

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

    private static 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(ILog), 
              (p, i) => LogManager.GetLogger(t))
          });
    }

    protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration)
    {
        // Handle constructor parameters.
        registration.Preparing += OnComponentPreparing;

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

以及以下注册它的代码

builder.RegisterModule(new LoggingModule());

这是整合它的正确方法吗?

Is this the correct method of integrating it?

我该如何在MVC控制器中使用它?

And how do I use it in, say, an MVC controller?

推荐答案

log4net模块与其他任何 Autofac模块-您将其(如图所示)连同所有其他注册一起注册在ContainerBuilder中.

The log4net module is just like any other Autofac module - you register it (as you show) in the ContainerBuilder along with all your other registrations.

// EITHER
builder.RegisterModule<LoggingModule>();
// OR
builder.RegisterModule(new LoggingModule());

log4net模块页面上的文档解释了如何做到这一点用于将ILog参数注入到对象的构造函数或属性中.

The documentation on the log4net module page explains how this can be used to inject ILog parameters into constructors or properties on your objects.

因此,就像其他任何IoC结构化类一样,要使用它,您可以添加构造函数或属性以允许其被注入.

So, just like any other IoC structured class, to consume it, you'd add either a constructor or property to allow it to be injected.

public class SomeClassThatNeedsLogging
{
  private ILog _logger;
  public SomeClassThatNeedsLogging(ILog logger)
  {
    this._logger = logger;
  }
}

同样,您可以使用构造函数或属性注入.

And, again, you can use constructor or property injection.

public class SomeClassThatNeedsLogging
{
  public ILog Logger { get; set; }
}

当您解决您的注册消费者时,Autofac会进行接线.

When you resolve your registered consumer, Autofac does the wireup.

var someClass = container.Resolve<SomeClassThatNeedsLogging>();
// someClass will have the ILog value populated.

对于控制器,您已经在Autofac中进行了注册,因此您所需要做的就是添加构造函数参数或public writable属性. MVC集成可解决所有问题,因此无需进行任何手动操作,也无需进行Resolve调用或其他任何操作.

For controllers, you're registering those with Autofac already, so all you need to do is add the constructor parameter or public writable property. The MVC integration does all the resolution, so there's nothing manual to do, no Resolve call or anything.

如果仍然不清楚,我建议您花一些时间与 the Autofac快速入门指南,并深入研究其余文档.

If it's still unclear, I'd recommend spending some time with the Autofac quick start guide and diving into the rest of the documentation.

这篇关于AutoFac和Log4Net-注册和使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆