Controlling log file locations from build time configuration

Skip to first unread message

Mark Ohlson

May 21, 2019, 11:17:50 AM5/21/19
to NLog-Users
I am working with an application (set) using Nlog - and need to make an adjustment to log file base location.  Up to this point they are based on ${basedir} which ends up in C:/Program Files/ for release/install builds and inside the visual studio project directory for debugging (C:\CustomDevelopment\Company\Code\Code - Trunk - MarkDev - Apr16\DebugOutput\logs ) ...

This location is problematic for 2 reasons: (1) security - install directory is wrong place for writable files, and (2) space - C drive is often relatively small and sensitive to file space exhaustion.

I am relying on static definition of Nlog.Config and proper placement of this file during the build process (Visual Studio, .Net...).  A current snippet from this file (as example) is one of targets:

<target xsi:type="File" name="RobotMovement" fileName="${basedir}/logs/RobotMovement/${logger} ${appdomain:format={1\}} ${shortdate}.log"
                  layout="${longdate} | ${logger} | ${uppercase:${level}} | ${message} | ${exception:format=toString}"  concurrentWrites="true" keepFileOpen="true" />

I have searched (somewhat unsuccessfully) for ways to influence this location, and my intended choice (unless advised otherwise) is the following:

(1) add a custom variable to the Nlog.Config file as in:

<!-- optional, add some variables
  <variable name="logBasedir" value="TO_BE_SUBSTITUTED"/>

(2) then change targets to be of form

<target xsi:type="File" name="RobotMovement" fileName="${logBasedir}/logs/RobotMovement/${logger} ${appdomain:format={1\}} ${shortdate}.log"
                  layout="${longdate} | ${logger} | ${uppercase:${level}} | ${message} | ${exception:format=toString}"  concurrentWrites="true" keepFileOpen="true" />

(3) at build time (pre-build event), run a custom build tool on Nlog.Config file to find&replace  "TO_BE_SUBSTITUTED" with an appropriate (configured ) location based on build type 
          typically something like    find&replace  "TO_BE_SUBSTITUTED" with  "D:/Company/Product/Nlogs"

Am I missing some alternative best practice approach(s) ?  This approach seems heavy-handed and crude, though it works.  I read about moving the Config into the application - creating a configuration in code dynamically.  This seems equally heavy-handed to accomplish such a small change.  Thanks in advance.


Glenn Lanier, II

May 21, 2019, 1:02:58 PM5/21/19

We are controlling this using TFS deployment variable replacement in the AppSettings section of the web.config file.
    <add key="RootLogFolder" value="Fully-qualified-path-to-log-folder" />

NLog.config looks like:
    <variable name="RootLogFolder" value="${appsetting:name=RootLogFolder}" />

        <target name="dailyFile"
layout="${longdate}|${var:CurrentUser}|${level}|${var:AppName}:${logger}|${message}" />
        <target name="OnScreenLog"
layout="${time} ${level:padding=-5} ${logger:shortName=true:padding=-30:fixedLength=true} ${message} ${var:CurrentUser} " />

I set the CurrentUser variable programatically, but that's a different scenario from what you described.

Hopefully this helps and you can use/adapt/springboard to a working solution.


You received this message because you are subscribed to the Google Groups "NLog-Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

Mark Ohlson

May 21, 2019, 2:40:53 PM5/21/19
to NLog-Users
Thanks - the key was learning how to access the app.config file (in my case not a web app, so not the web.config file).  This also simplifies common access of same info by Nlog and by runtime components of the application.  Also lead me to full list of NLog Layout Renderers for more opportunities. Thanks, this should get me to a better place.  MarkO
To unsubscribe from this group and stop receiving emails from it, send an email to
Reply all
Reply to author
0 new messages