Search code examples
log4netapp-configwatchappender

log4net doesn't watch my app.config


I configured my log4net to watch on changes made to the app.config file.

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

When I run my app and change things in the config file, these changes only take effect when I restart my app. Why could this be?

Is there also a way to tell log4net to watch on changes in the app.config? Like:

<appender name="FileAppender" type="log4net.Appender.FileAppender" >
    <watch value="true" />
</appender>

------------- EDIT -------------

I tried now to use a separate config-file: log4net.config.
It looks like this:

<log4net>
  <appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file value="c:\log.txt" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%d [%t] %-5p %c (line %L) -- %m%n" />
    </layout>
  </appender>
  <root>
    <appender-ref ref="FileAppender" />
  </root>
</log4net>

In my assemblyInfo.cs I wrote the following:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

The class that logs to the file looks like this:

ILog myLogger = LogManager.GetLogger(typeof(Form1));
myLogger.Debug("test");

This works like the old version. logfile entries are made, but when I change my log4net.config during runtime, these changes are not applied.... "Watch=true" should enable that feature, right?


Solution

  • HA!, I was just encountering the same problem, running unit tests that require logging.
    Adding this line fixed it:

    log4net.Config.XmlConfigurator.Configure();
    

    My App.config:

    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <log4net>
        <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
            <file value="log.txt" />
            <appendToFile value="true" />
            <rollingStyle value="Size" />
            <maxSizeRollBackups value="10" />
            <maximumFileSize value="100KB" />
            <staticLogFileName value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
            </layout>
        </appender>
        <root>
            <level value="DEBUG" />
            <appender-ref ref="RollingFileAppender" />
        </root>
    </log4net>
    

    I also do have this:

    [assembly: log4net.Config.XmlConfigurator(Watch = true)]