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] " />
<Footer value="[Application Exit] " />
<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] " />
<Footer value="[Application Exit] " />
<ConversionPattern value="%date (%property{ActiveStatus}) %-5level [%2thread] %-20.20logger{1} %message%newline" />
</layout>
</appender>
</log4net>
You may want to look into doing a transform on your
app.configfile 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?