Search code examples
c#.netapp-confignlog

Reference a NLog variable from App.Config Setting


<envSettings>
    <environment name="Local">      
      <add key="ElasticSearch.Url" value="http://localhost:9200/" />
    </environment>
    <environment name="Dev">     
      <add key="ElasticSearch.Url" value="http://XXXXXXX:9200/" /> 
    </environment>
 </envSettings>

 <nlog autoReload="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      throwExceptions="false">
    <variable name="elastic.url" value="${envSettings:name=ElasticSearch.Url}"/> </nlog>

Is it Possible for NLog to read the value from EnvSettings? I have used NLog.Extended nuget package.


Solution

  • As Rolf noticed, you need probably a custom layout renderer, as the closest thing reads the System.Environment (e.g. ${environment:variable=PATH})

    So code then:

    // register ${ElasticSearch-Url}
    LayoutRenderer.Register("ElasticSearch-Url", (logEvent) => retrieve ElasticSearch.url here);
    

    Do this a soon as possible e.g. in main(), app_start etc.