Filetarget Archiving: Preserve original filename #1855

matthiasbonn opened this Issue Dec 15, 2016 · 8 comments


None yet

3 participants

matthiasbonn commented Dec 15, 2016 edited
  • Question
    NLog version: 4.0.30319

Platform: .Net 4.5

Current NLog config (xml or C#, if relevant)

  <target name="logfile" xsi:type="File" 
    maxArchiveFiles="30" /> 
  <target name="console" xsi:type="Console" />
  <target name="debugger" xsi:type="OutputDebugString" />

I use the nlog in powershells script in the following way in my scripts:

$nlogconfig = new-object NLog.Config.XmlLoggingConfiguration("$NlogPath\$NlogConfig")
[NLog.Targets.FileTarget]$fileTarget = [NLog.Targets.FileTarget]([NLog.LogManager]::Configuration.FindTargetByName('logfile'))
$fileTarget.FileName = $LogPathFile
$PSlogger = [NLog.LogManager]::GetLogger('PSLogger')  

Therefore the logfile name is the scriptname and i would like to preserver the name in the archive. It should be like ${shortdate}_ScriptName_{##}.log.
I have tried to set the archivefilename in the config to ${shortdate}_${file}_{##}.log but it does´t work.

304NotModified commented Dec 15, 2016 edited

What's _ScriptName_? PSLogger etc?

What about




The $PSlogger is a powershell variable that i use to represent the nlog instance in my script (example demoscript.ps1).

The code snippet works fine and i have a file target with the logfile name demoscript.log. I would like to use the archive funktion in that way that the archivefile becomes the name 20161215_demoscript_00.log.

demoscript is an example for the scriptname.


Is this still an issue?


Yes itis still an issue

304NotModified commented Jan 12, 2017 edited

Ah well then I'm a bit confused by this:

The code snippet works fine

to get 20161215_demoscript_00.log. then ${date:format=yyyyMMdd}_${logger}_{##}.log would be the solution? Or not?

update: not ${shortdate}, which has dashes


No it´s not the solution because ${logger} will be the content of the variable to access the nlog component, i my case PSLogger. I would like to have the actual log (or script) file name in that position.


I would like to have the actual log (or script) file name in that position.

isn't that fileName="E:\Logs\${logger}.log" ?


Maybe the solution is to include the scriptname as a subfolder-name, so each script has its own dedicated folder. Then you don't need the scriptname in the filename.

Another "solution" is to include the scriptname in the logger-name, and then have a filetarget for each scriptname where the filename and archive-filename has the script-name hardcoded in the file-path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment