2

I have .Net Framework 4.7.2 Console app running on Windows 10 (64bit), which is called thru Task Scheduler periodically.

When the task is run the log file gets created inside C:\Windows\SysWOW64, if I switch to ${basedir} instead of ${currentdir}, it creates file where the exe is located.

I am puzzled is this correct behaviour, that ${currentdir} creates files in C:\Windows\SysWOW64? Before I log a issue with NLog, I thought I should check on SO first.

Their documentation on ${basedir} or ${currentdir} doesn't state the difference between two or explains which option to use when.

NLog version: 4.6.3

Below is my nlog.config file:

<?xml version="1.0" encoding="utf-8" ?>
<!-- XSD manual extracted from package NLog.Schema: https://www.nuget.org/packages/NLog.Schema-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogFile="nlog-internal.log"
      internalLogLevel="Info">

  <variable name="format" value="${threadid}|${longdate}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
  <variable name="path" value="${currentdir}" /> <!-- Using ${basedir} works -->
  <!-- the targets to write to -->
  <targets>
    <!-- write logs to file -->
    <target xsi:type="Console" name="console" layout="${var:format}" />
    <target xsi:type="File" name="mslog" fileName="${var:path}\mslog-${shortdate}.log" layout="${var:format}" />
    <target xsi:type="File" name="datadaptorlogs" fileName="${var:path}\datadaptor-${shortdate}.log" layout="${var:format}" />
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <logger name="*" minlevel="Trace" writeTo="datadaptorlogs,console" />
    <logger name="Microsoft.*" minlevel="Error" writeTo="mslog" />
  </rules>
</nlog>
Julian
  • 26,655
  • 14
  • 92
  • 132
ndd
  • 2,829
  • 2
  • 22
  • 37
  • Strange you have provided wiki-links to basedir and currentdir, and yet you have not read them. Let me try for you. ${basedir} is the appdomain-base-directory for the exe-file. ${currendir} is the random working-folder where the application was started. See also https://en.wikipedia.org/wiki/Working_directory – Rolf Kristensen Jun 10 '19 at 18:27
  • It is possible to specify working folder when running as scheduled task. See https://stackoverflow.com/a/27401164/193178 – Rolf Kristensen Jun 10 '19 at 18:28

1 Answers1

3

With ${basedir}, NLog is searching for the root of our application.

${currentdir} is the working directory, set on the process running your application.

You could set the working directory - and so the ${currentdir} - in the task schedular:

enter image description here

Julian
  • 26,655
  • 14
  • 92
  • 132