Search code examples
c#log4netcastle-windsorcastle

Castle Log4Net facility not logging


I'm trying to use the Castle log4Net facility in a windows service. I could not get it to write a log so I thought I'd create a console app to attempt to get it working first. I am still not seeing the log file written. What have I done wrong or missed pleased?

namespace CastleLoggingFacilityWithLog4Net
{
    class Program
    {
        private static IWindsorContainer _container;

        static void Main(string[] args)
        {

            _container = new WindsorContainer();
            _container.Register(Component.For<IService>().ImplementedBy<Service>().LifestyleTransient());
            _container.AddFacility<LoggingFacility>(f => f.LogUsing(LoggerImplementation.Log4net));
            //_container.Install(FromAssembly.This());

            var service = _container.Resolve<IService>();
            service.TestLogging();
        }
    }

    public interface IService
    {
        void TestLogging();
    }

    public class Service : IService
    {
        private ILogger logger = NullLogger.Instance;

        public void TestLogging()
        {
            Logger.Info("this is a test");
        }

        public ILogger Logger
        {
            get { return logger; }
            set { logger = value; }
        }

    }
}

Add my log4net config in log4net.config is as follows...

<configuration>
  <configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
  <file value="logging\log.txt" />
  <appendToFile value="true" />
  <datePattern value="dd-MM-yyyy'.log'" />
  <staticLogFileName value="false" />
  <rollingStyle value="Date"/>
  <maximumFileSize value="10MB" />
  <maxSizeRollBackups value="5" />
  <dateTimeStrategy type="log4net.Appender.RollingFileAppender+UniversalDateTime" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%utcdate %-5level %logger - %message%newline%exception" />
  </layout>
</appender>
<root>
  <level value="DEBUG" />
  <appender-ref ref="RollingFile" />
</root>
</log4net>
</configuration>

Solution

  • Turns out that there was nothing wrong with my code or configuration. The problem was that log4net config was not set to copy to output directory! Shame neither Castle or log4net were able to tell me that the logfile couldn't be found.