log4net doesn't watch my app.config
Asked Answered
A

3

13

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?

Await answered 19/10, 2011 at 12:37 Comment(0)
E
15

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)]
Evenings answered 19/10, 2011 at 18:58 Comment(5)
It's not like it didn't watch my whole config file. it's just that changes made to the config file during runtime, dont take effect.Await
even after calling log4net.Config.XmlConfigurator.Configure(); after making those changes?Evenings
unfortunately yes. can't explain this.... can you edit your config file during runtime ? for example the conversion pattern, and these changes immediately take effect?..Await
I don't think it watches that section continuously, see: #437200 If you do the refresh section that it talks about and call .Configure() it may refresh settings.Evenings
After researching so much, your answer saved me :)Groundage
S
10

According to log4net documentation, the Watch feature does not work for application configuration files (app.config, web.config):

Because the System.Configuration API does not support reloading of the config file the configuration settings cannot be watched using the log4net.Config.XmlConfigurator.ConfigureAndWatch methods.

So if you need the log4net configuration to be re-configurable, you will need to place it in a separate XML file and your application needs to have sufficient permissions to read the file:

The file to read the configuration from can be specified using any of the log4net.Config.XmlConfigurator methods that accept a System.IO.FileInfo object. Because the file system can be monitored for file change notifications the ConfigureAndWatch methods can be used to monitor the configuration file for modifications and automatically reconfigure log4net.

Sleeper answered 26/3, 2014 at 12:18 Comment(0)
M
0

Even though I'm terribly late to the party - here's, what helped me: a simple call to log4net.LogManager.GetLogger("DUMMY"); at the very beginning of my program. I put it in the very first line of program.cs's Main() method. No need to assign the logger to any object, merely a polite request to log4net to read the assembly's attributes as stated here.

Using attributes can be a clearer method for defining where the application's configuration will be loaded from. However it is worth noting that attributes are purely passive. They are information only. Therefore if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked.

Misbegotten answered 23/1, 2018 at 13:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.