Search code examples
c#.netapp-confignlog

Nlog config escape colon


I have a NLog.config file with the following variable

<variables>
    <variable name="LogsFilePath" value="${appsetting:item=LogsFolderPath:default=C:\Company\Logs}" >
</variables>
<targets>
    
    <target name="File"
        xsi:type="File"
        fileName="${LogsFilePath}\${event-properties:FileName}.log" />
    
</targets>

<rules>
    <rule logger="DynamicLogger_*" minLevel="Trace" writeTo="File" />
</rules>

When there is no value in app.config file the default value taken is only C (because of the colon).

My target is something like this

Also if I use the variable in the config file like ${var:LogsFilePath} I have problems because of the colon. I have to use it with the static form ${LogsFilePath}

How can I solve this and use a path as default value in the default path of the appsetting variable?


Solution

  • You need to escape it. Replace ":" with "\:", also you need to escape backslash.

    I got it working like this

    <variables>
        <variable name="LogsFolderPath" value="${appsetting:item=LogsFolderPath:default=C\:\\Company\\Logs}" />
    </variables>