Lempkin
Lempkin

Reputation: 1578

log4net change conf file path dynamicaly

I need to change the conf file path depending on the environment. In my Gobal.asaw.cs i have :

log4net.Config.XmlConfigurator.Configure();

In my web.config :

 <add key="log4net.Config" value="conf/Log4netDev.config"/>
 <add key="log4net.Config.Watch" value="True"/>

In my web.Debug.config :

<add key="log4net.Config" value="conf/Log4netDev.config" xdt:Locator="Match(key)" xdt:Transform="Replace" />
<add key="log4net.Config.Watch" value="True" xdt:Locator="Match(key)" xdt:Transform="Replace"/>

In my web.Release.config :

<add key="log4net.Config" value="conf/Log4netRelease.config" xdt:Locator="Match(key)" xdt:Transform="Replace" />
<add key="log4net.Config.Watch" value="True" xdt:Locator="Match(key)" xdt:Transform="Replace"/>

So in my web.config, the log4net.Config value is same as in web.debug.config With visual studio, if i run in debug, i can find my logFile where it's supposed to be, everything is ok. If i run in release, no log file...

I've tried to replace the log4net.Config value in web.config with the value i have in web.release.config, and now : when i run project in Release i have my log file and not in debug anymore. What i understand is this only work when the value is in the web.config

Upvotes: 1

Views: 466

Answers (1)

Fab
Fab

Reputation: 14813

Why not change it programmatically ?

    var loggingConfigurationFile = HttpContext.Current.IsDebuggingEnabled ? "conf/Log4netDev.config" : "conf/Log4netRelease.config";
    log4net.Config.XmlConfigurator.Configure(new FileInfo(loggingConfigurationFile));

Upvotes: 1

Related Questions