Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 475
Fabian Avatar asked Oct 19 '11 12:10

Fabian


People also ask

Where is log4net config?

You can configure the log4net. config file to create log files. The file is located in the webroot\App_data directory of the installation.

How do I enable log4net logging?

If you are using a separate configuration file for log4net, do this: after following all the other setup instructions, make sure that u right click on the file in the visual studio solution explorer, select properties, expand the "Advanced" option group, set the "Copy To Output Directory" value as "Copy always".


2 Answers

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)]
like image 120
maxfridbe Avatar answered Oct 01 '22 06:10

maxfridbe


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.

like image 27
Igor Brejc Avatar answered Oct 01 '22 05:10

Igor Brejc