1

I am not able to access environment variables in C# console application's app.config file.

I want to access log-level's value from ENVIRONMENT Variable LOG_LEVEL in app.config :

  <configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=2.0.8.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a" />
    </configSections>
    <log4net>
        <root>
          <level value="INFO"/>
          <appender-ref ref="RollingFileAppender" />
          <appender-ref ref="ConsoleAppender" />
        </root>
        <!--log to console-->
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date %-5level %logger - %message%newline" />
          </layout>
        </appender>
     </log4net>
  </configuration>

I want to access it like <level value=%LOG_LEVEL%>.

But I am not able to access it on OSX. I have tried using it like $LOG_LEVEL, ${LOG_LEVEL} , %LOG_LEVEL

Utsav Chokshi
  • 1,357
  • 1
  • 13
  • 35
  • by showing your code people will have a much better chance of understanding what you are asking – Seabizkit Apr 13 '18 at 12:44
  • Have you tried `System.Configuration.ConfigurationManager.GetSection("log4net")`? – Ricardo Pontual Apr 13 '18 at 12:59
  • @RicardoPontual .. I know, I can always do this by accessing config section in code and access Environment variable by method `Environment.GetEnvironmentVariable("LOG_LEVEL")`. – Utsav Chokshi Apr 13 '18 at 14:53
  • @Seabizkit.. Right now, I am setting in value of level in code only and I am able to do this. But I want to do it in .config file. As I feel that setting such value via code is not best practice. – Utsav Chokshi Apr 13 '18 at 14:54
  • There is no environment variable substitution at the .NET config level. So this is really a log4net question. Based on https://stackoverflow.com/questions/2600147/is-it-possible-to-use-environment-variable-to-specify-the-desired-log-level-for the answer was “you can’t” 8 years ago. – Zastai Apr 16 '18 at 16:06
  • @Zastai .. Thanks but this answer suggests opposite : https://stackoverflow.com/questions/5303113/app-config-settings-environment-variable-as-partial-path – Utsav Chokshi Apr 17 '18 at 09:48
  • To me, that answer still says you have to do it in code, but if you use the `%VarName%` notation, you only need a single method call. If it’s log4net reading that setting, chances are it won’t do that. And doing it in code is what I thought you didn’t want. – Zastai Apr 17 '18 at 16:01

0 Answers0