使用Autofac注入log4net的到控制器 [英] Using Autofac to inject log4net into controller

查看:1887
本文介绍了使用Autofac注入log4net的到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用Autofac注入一个log4net的类到我的控制器,但我得到了以下异常:

Trying to use Autofac to inject a log4net class into my controller, but I get the following exception:

在构造函数中没有发现与公共绑定标志
   类型'MvcApplication6.Controllers.HomeController'可以与提供的服务和参数调用:无法解析构造'太虚.ctor(log4net.ILog)'参数'log4net.ILog记录器

None of the constructors found with 'Public binding flags' on type 'MvcApplication6.Controllers.HomeController' can be invoked with the available services and parameters: Cannot resolve parameter 'log4net.ILog logger' of constructor 'Void .ctor(log4net.ILog)'.

我创建了一个模块,使用正确类型注入登录类:

I have created a module to inject the Log class using the correct type:

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

    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)) 
        });
    }
}

我那么我的ASP.NET MVC中注册模块的Application_Start 方法:

protected void Application_Start()
{
     ContainerBuilder builder = new ContainerBuilder();
     builder.RegisterControllers(typeof (MvcApplication).Assembly) ;

     var container = builder.Build() ;
     DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); 

     builder.RegisterModule(new LogInjectionModule());

     AreaRegistration.RegisterAllAreas();

     RegisterGlobalFilters(GlobalFilters.Filters);
     RegisterRoutes(RouteTable.Routes);
}

我添加了一个constuctor到需要一个的ILog 作为参数控制器:

namespace MvcApplication6.Controllers
{
   public class HomeController : Controller
   {
      ILog _log;

      public HomeController(ILog logger) 
      {
         _log = logger;
      }

      public ActionResult Index()
      {
         ViewBag.Message = "Welcome to ASP.NET MVC!";

         _log.Info("Log message from Index()");

         return View();
      }

      public ActionResult About()
      {
         _log.Info("Log message from About()");

         return View();
      }
   }
}

我相信,我已经错过了一步,所以任何帮助将是AP preciated。

I am sure I have missed a step, so any help would be appreciated.

推荐答案

我不知道这是造成你的问题,但你应该尝试的模块添加到ContainerBuilder 调用builder.Build前();

I'm not sure this is causing your problem but you should try to add the module to the ContainerBuilder before calling builder.Build();

事情是这样的:

ContainerBuilder builder = new ContainerBuilder();
builder.RegisterControllers(typeof (MvcApplication).Assembly) ;
builder.RegisterModule(new LogInjectionModule());

var container = builder.Build() ;
DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); 

另一个建议是为未注入记录即可。通常,当我设计一个类,用构造函数依赖我尝试前preSS组件的逻辑业务依赖即时造型。测井是大多的实施细节,正交于该应用程序。至少在log4net的你可以在你需要与LogManager.GetLogger(类型)中创建记录任何类有一个静态成员。为了方便添加您可以使用Visual Studio的片段记录器。

Another suggestion is to not inject the logger. Usually when i design a class, with the constructor dependencies i try to express the logical business dependencies of the component i'm modelling. Logging is mostly an implementation detail that is orthogonal to the application. At least with log4net you can have a static member in any class where you need logging that is created with LogManager.GetLogger(type). To facilitate adding the logger you can use a Visual Studio snippet.

这篇关于使用Autofac注入log4net的到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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