2

我正在研究这个(http://docs.castleproject.org/Windsor.Windsor-Tutorial-Part-Five-Adding-logging-support.ashx)Castle Windsor的IoC教程,我已经通过了上述所有步骤,并且如上面的教程中所述,我试图让log4net记录仪通过属性注入到我的控制器,如下列:Castle Windsor IoC不会将log4net注入到我的控制器中

public class HomeController : Controller 
{ 
    // this is Castle.Core.Logging.ILogger, not log4net.Core.ILogger 
    public ILogger Logger { get; set; } 

    public ActionResult Index() 
    { 
     Logger.Debug("Hello world"); 

     ViewBag.Message = "Hello world"; 

     return View(); 
    } 

} 

但不幸的是,在Logger.Debug记录仪的执行点为空,使之与空引用异常的结果。虽然当我试图呼叫

var logger = container.Resolve<ILogger>(); 

logger.Debug("Container bootstrapped"); 

内部Global.asax记录器是完全解决。

为什么温莎不想解决控制器内部的依赖关系?


EDIT

控制器经由温莎

Global.asax中创建

using System; 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.Linq; 
using System.Web; 
using System.Web.Http; 
using System.Web.Mvc; 
using System.Web.Optimization; 
using System.Web.Routing; 
using Castle.Core.Logging; 
using Castle.Windsor; 
using Castle.Windsor.Installer; 
using FlightManagementSystem.WebPlatform.Configuration.IoC.Windsor.Factories; 

namespace FlightManagementSystem.WebPlatform 
{ 
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801 

    public class MvcApplication : System.Web.HttpApplication 
    { 
     private static IWindsorContainer container; 

     protected void Application_Start() 
     { 
      Debugger.Break(); 

      AreaRegistration.RegisterAllAreas(); 

      WebApiConfig.Register(GlobalConfiguration.Configuration); 
      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
      RouteConfig.RegisterRoutes(RouteTable.Routes); 
      BundleConfig.RegisterBundles(BundleTable.Bundles); 
      AuthConfig.RegisterAuth(); 

      container = new WindsorContainer(); 

      container.Install(FromAssembly.This()); 

      var controllerFactory = new ControllerFactory(container.Kernel); 

      ControllerBuilder.Current.SetControllerFactory(controllerFactory); 

     } 

     protected void Application_End() 
     { 
      container.Dispose(); 
     } 
    } 
} 

ControllerFactory.cs

using System; 
using System.Web; 
using System.Web.Mvc; 
using Castle.MicroKernel; 

namespace FlightManagementSystem.WebPlatform.Configuration.IoC.Windsor.Factories 
{ 
    public class ControllerFactory : DefaultControllerFactory 
    { 
     private readonly IKernel kernel; 

     public ControllerFactory(IKernel kernel) 
     { 
      this.kernel = kernel; 
     } 

     public override void ReleaseController(IController controller) 
     { 
      kernel.ReleaseComponent(controller); 
     } 

     protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) 
     { 
      if (controllerType == null) 
      { 
       throw new HttpException(404, String.Format(Resources.THE_CONTROLLER_FOR_PATH_0_COULD_NOT_BE_FOUND, requestContext.HttpContext.Request.Path)); 
      } 

      return (IController) kernel.Resolve(controllerType); 
     } 
    } 
} 

ControllerInstaller.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using Castle.MicroKernel.Registration; 
using Castle.MicroKernel.SubSystems.Configuration; 
using Castle.Windsor; 

namespace FlightManagementSystem.WebPlatform.Configuration.IoC.Windsor.Installers 
{ 
    public class ControllerInstaller : IWindsorInstaller 
    { 
     public void Install(IWindsorContainer container, IConfigurationStore store) 
     { 
      container.Register 
      (
       Classes.FromThisAssembly() 
       .BasedOn<IController>() 
       .LifestyleTransient() 
      ); 
     } 
    } 
} 

LoggerInstaller.cs

using Castle.Facilities.Logging; 
using Castle.MicroKernel.Registration; 
using Castle.MicroKernel.SubSystems.Configuration; 
using Castle.Windsor; 

namespace FlightManagementSystem.WebPlatform.Configuration.Logger.log4net 
{ 
    public class LoggerInstaller : IWindsorInstaller 
    { 
     public void Install(IWindsorContainer container, IConfigurationStore store) 
     { 
      container.AddFacility<LoggingFacility>(f => f.UseLog4Net()); 
     } 
    } 
} 
+0

你如何创建控制器? – 2013-05-12 21:24:09

+0

控制器通过Windsor创建,我编辑了主要问题,插入了控制器创建逻辑 – Lu4 2013-05-12 21:29:31

+0

您是否添加并配置了“LoggingFacility”? – 2013-05-12 21:34:59

回答

3

它没有真正回答你的问题,但有几个建议:

1)使用空对象行话来登录时防止NRE

ILogger logger = NullLogger.Instance; 
public ILogger Log 
{ 
    get { return logger; } 
    set { logger = value ?? NullLogger.Instance; } 
} 

2)记录设施应在大多数情况下,先添加 - 任何安装程序在注册前:

container = new WindsorContainer(); 
container.AddFacility<LoggingFacility>(f => f.UseLog4Net()); 
container.Install(FromAssembly.This()); 

如果添加日志记录功能后,你可以在安装过程中丢失了一些有价值的信息。

相关问题