3
votes

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>
2
In Service, why are you setting logger = NullLogger.Instance?wageoghe
May be this link can help youprasy
Thanks @prasy I've already seen that link.Simon Ryan

2 Answers

1
votes

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.

0
votes

You have to add the below code in Main() In order to generate a log file, this will cause the configSource to be used

log4net.Config.XmlConfigurator.Configure();

Edit: According to file Value in app.config. It will write the log files to ProgramFiles, not all users have a rights to write/edit files in programfiles. So, i recommend writing the log files in AppData.

<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="${LOCALAPPDATA}\logging\log.txt" />
....
</appender>