Windsor + log4net = NullReference

时间:2012-10-29 07:09:41

标签: castle-windsor log4net

以下是我的代码。使用Logger对象时出现NullReference异常。我错过了什么?

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

namespace log4netTest
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            new WindsorContainer()
                .Install(FromAssembly.This());

            var test = new testClass();
            test.Do();
        }
    }

    public class testClass
    {
        public ILogger Logger { get; set; }

        public void Do()
        {
            Logger.Info("start");
            Logger.Error("error end");
        }
    }

    public class MyWindorInstaller : IWindsorInstaller
    {
        #region IWindsorInstaller Members

        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.AddFacility<LoggingFacility>(facility => facility.UseLog4Net("log4net.config"));
        }

        #endregion
    }
}

1 个答案:

答案 0 :(得分:0)

我应该使用

var test = container.Resolve<testClass>();