1

I am developing some application at the moment which can run in two environments : development or production. I wish to have a different logging infrastructure for each.

The environment gets passed to my .exe at runtime. How can I enable/disable the respective appenders through code ? Is there something to tweak in the config below as well ?

I have other contents in this app.config file for application settings that give for each key a "prod" value and a "dev" value, based on this, so I do not want more than one config file.

<log4net>

    <root>
      <level value="debug" />
      <appender-ref ref="Dev" />
      <appender-ref ref="Prod" />
    </root>

    <appender name="Dev" type="log4net.Appender.FileAppender">
      <file type="log4net.Util.PatternString" value="blabla 1" />
      <layout type="log4net.Layout.PatternLayout">
        <Header value="[Application Start]&#13;&#10;" />
        <Footer value="[Application Exit]&#13;&#10;"  />
        <ConversionPattern value="%date (%property{ActiveStatus}) %-5level [%2thread] %-20.20logger{1} %message%newline" />
      </layout>
    </appender>

    <appender name="Prod" type="log4net.Appender.FileAppender">
      <file type="log4net.Util.PatternString" value="blabla 2" />
      <layout type="log4net.Layout.PatternLayout">
        <Header value="[Application Start]&#13;&#10;" />
        <Footer value="[Application Exit]&#13;&#10;"  />
        <ConversionPattern value="%date (%property{ActiveStatus}) %-5level [%2thread] %-20.20logger{1} %message%newline" />
      </layout>
    </appender>

</log4net>
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
BuZz
  • 16,318
  • 31
  • 86
  • 141
  • By the way, you can move just the stuff into a separate file. See here http://stackoverflow.com/a/445988/562906 – sinelaw Apr 26 '12 at 13:47
  • You can access log4net configuration at runtime and modify the appender collection – lnu Apr 26 '12 at 13:52

4 Answers4

6

You may want to look into doing a transform on your app.config file based on which environment you're publishing to. This is pretty common in Web Application, but there is an SO which explains how to extend it to other project types:

App.Config Transformation for projects which are not Web Projects in Visual Studio 2010?

Community
  • 1
  • 1
Khan
  • 17,904
  • 5
  • 47
  • 59
  • +1 you should definitely invest time in getting app.config transforms working - they are good not just for log4net config but for everything in your app.config that differs from dev to staging to production. – wal Apr 26 '12 at 13:55
4

I would suggest a separate configuration file per environment and load appropriate one in run time. In thsi way it would be easier to maintain and avoid occasional errors like development specific settings was applied to production appender and so on.

if (environment == "Production")
{
   log4net.Config.Configure(Path.Combine(productionFolder, "log4net.config"));
}
sll
  • 61,540
  • 22
  • 104
  • 156
  • This would still cause the same issue as the 'environment' variable/app setting may get incidentally overridden to dev in prod and so on. – Dienekes Sep 06 '17 at 19:47
1

I vote for sll's suggestion. However, if you insist on disabling one of the appenders:

ILoggerRepository repository = log4net.LogManager.GetRepository();
var appender = repository.GetAppenders()
    .Where(a => a.Name == "dev")
    .Single();
appender.Close();
Peter Zajic
  • 899
  • 8
  • 17
-2

Perhaps use the tag <threshold value="OFF"/> and modify alternatively ??

Comment out this way

<!-- <threshold value="OFF"/> -->

when you need the logging enabled

this-Me
  • 2,139
  • 6
  • 43
  • 70
  • 3
    even that i am not the downvoter ... this answer is clearly *stupid*, as it does not work for (nearly) automatic/automated deployment ... definitely no enterprise solution! if you are on your own machine and build closed setups, this might be ok ... but for every other scenario this is **not** an appropriate solution! –  Apr 26 '12 at 14:01
  • 3
    You shouldnt quote an answer as stupid. Just "wrong". – lost in binary Dec 22 '20 at 15:39