I'm using NLog with a PowerShell script, where messages are logged to a file in a directory that is relative to the script.
I'd like to be able to specify a relative path in the Xml configuration file, but this syntax doesn't work:
<target name="logfile" xsi:type="File" fileName="./Logs/${logger}/${logger}.${date:format=yyyyMMdd}.${date:format=HHmmss}.log" createDirs="true" layout="${machinename}|${environment-user}|${logger}|${date:format=yyyy-MM-dd HH\:mm\:ss}|${level:uppercase=true}|${message}"/>
Currently, I'm doing this:
$ScriptName = (Get-Item $PSCommandPath).Basename
$Here = Split-Path -Parent $MyInvocation.MyCommand.Path
# create logger
$Logger = [NLog.LogManager]::GetLogger($ScriptName)
# load Xml configuration file (in the same directory as script)
$Configuration = [NLog.Config.XmlLoggingConfiguration]::new("$Here/$ScriptName.NLog.config")
[NLog.LogManager]::Configuration = $Configuration
# reconfigure the file target
$target = $Configuration.FindTargetByName("logfile")
$target.FileName = "$Here/Logs/$ScriptName/$ScriptName.$( (get-date).tostring('yyyyMMdd.HHmmss') ).log"
[NLog.LogManager]::ReconfigExistingLoggers()
Is there a way to use a relative path in the Xml configuration file?