ricksmt
ricksmt

Reputation: 899

NLog appsetting default to relative path

I want to use <appsetting> in NLog (the NLog.Extended NuGet package to be specific) to do something like this:

<variable name="logDir" value="${appsetting:name=RemoteLogDir:default=.\Log}" />

Where I set up RemoteLogDir when I publish my app, but fallback to a relative directory for local development. I've been able to get an absolute path to work, but that doesn't seem collaborator-friendly.

How can I get a relative path to work? Or do I need to submit a feature request?

Notes:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true" throwExceptions="false">

  <variable xsi:type="NLogVariable" name="appLogName" value="MyApp"/>

  <variable xsi:type="NLogVariable" name="logDir" value="${appsetting:name=RemoteLogDir:default=\.\\Log}" />
  <variable xsi:type="NLogVariable" name="layout" value="${level:uppercase=true} | ${date:format=MM/dd/yyyy HH\:mm\:ss} | ${machinename} | ${windows-identity:domain=true} | ${callsite} ${newline}    ${message}${onexception:${newline}[EXCEPTION]${newline}${exception:format=tostring}}" />

  <variable xsi:type="NLogVariable" name="logFileName" value="${var:logDir}/${var:appLogName}_${date:format=yyyy-MM-dd}.txt" />
  <variable xsi:type="NLogVariable" name="archiveLogFileName" value="${var:logDir}/Archive/${var:appLogName}_Archived_{#}.txt" />

  <targets>
    <target name="singleFile" xsi:type="File"
              layout="${var:layout}"
              fileName="${var:logFileName}"
              concurrentWrites="true"
              keepFileOpen="true"
              archiveFileName="${var:archiveLogFileName}"
              archiveEvery="Day"
              archiveNumbering="Date"
              archiveDateFormat="yyyy-MM-dd_HH"
              maxArchiveFiles="30" />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="singleFile" />
  </rules>
</nlog>

Upvotes: 1

Views: 1092

Answers (1)

ricksmt
ricksmt

Reputation: 899

TL;DR

<variable xsi:type="NLogVariable" name="logDir" value="${whenEmpty:whenEmpty=${basedir}/Log:inner=${appsetting:name=RemoteLogDir}}" />

Explanation

Indeed, the appsetting only accepts a string for the default value. However, you can emulate the desired functionality by supplying no default and using whenEmpty. When the app setting isn't set, the inner value will be counted as empty, so the whenEmpty value is rendered.

Upvotes: 2

Related Questions