Controlling log file locations from build time configuration

82 views
Skip to first unread message

Mark Ohlson

unread,
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.

MarkO

Glenn Lanier, II

unread,
May 21, 2019, 1:02:58 PM5/21/19
to nlog-...@googlegroups.com
Mark,

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

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

    <targets>
        <target name="dailyFile"
xsi:type="File"
fileName="${RootLogFolder}/${date:format=yyyy.MM.dd}.${var:AppName}.${machinename}.log"
layout="${longdate}|${var:CurrentUser}|${level}|${var:AppName}:${logger}|${message}" />
        <target name="OnScreenLog"
xsi:type="File"
fileName="${RootLogFolder}/OnScreen.${date:format=yyyy.MM.dd}.${var:AppName}.${machinename}.log"
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.

--G


--
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 nlog-users+...@googlegroups.com.
To post to this group, send email to nlog-...@googlegroups.com.
Visit this group at https://groups.google.com/group/nlog-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/nlog-users/aa968701-89d4-4b83-adff-257281517662%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark Ohlson

unread,
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 nlog-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages