NLog: Incorrect log path in single file publish

NLog version: (e.g. 4.6.6)

Platform: .NET Core 3 Preview6

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

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="Warn" internalLogToConsole="true" internalLogToConsoleError="true">
  <targets>
    <target xsi:type="File" name="info" fileName="log\info-${shortdate}.log"
            layout="${longdate} [${level}]: ${logger}${newline} Message: ${message}${newline}" />
    <target xsi:type="File" name="war" fileName="log\war-${shortdate}.log"
            layout="${longdate} [${level}]: ${logger}${newline} Message: ${message}${newline}Exception: ${exception:format=tostring}${newline}" />
    <target xsi:type="Null" name="blackhole" />
  </targets>
  <rules>
    <logger name="Microsoft.AspNetCore.Hosting.Internal.WebHost*" minlevel="Info" maxLevel="Info" writeTo="info" />
    <logger name="*" minlevel="Info" maxLevel="Info" writeTo="info" />
    <!--Skip non-critical Microsoft logs and so log only own logs-->
    <!-- BlackHole without writeTo -->
    <logger name="*" minlevel="Warn" writeTo="war" />
    <logger name="Microsoft.*" minlevel="Trace" maxLevel="Info" writeTo="blackhole" final="true" />
  </rules>
</nlog>

.NetCore 3 Preview6 Console App published in single file model.

  1. Publish dotnet publish -p:PublishSinlgefile=true -r win-x64

  2. Copy the exe file to another folder(include nlog.config)

     /app-sandbox
         ConsoleApp.exe
         nlog.config            
    
  3. Run ‘ConsoleApp.exe’

  • What is the current result? Log folder in %Temp%/.net/ConsoleApp/xxxxxxx.xxx/

  • What is the expected result?

      /app-sandbox
        ConsoleApp.exe
        nlog.config
        log/
          infor-date.log
    
  • Are there any workarounds? yes Write full path in the nlog.config

  • Can you help us by writing an unit test?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 31 (19 by maintainers)

Most upvoted comments

Hi!

Could you please test with ${basedir}, ${basedir:processDir=true} and ${currentdir}?

Thanks in advance!

@snakefoot Yes, ${basedir:fixtempdir=true} is better than {currentdir}.

@ttaylor29 Strange that you only get 2 lines of log-output from calling LogManager.LogConfiguration. Guess you have some other issues going on. Never mind then

@tai-yi NLog ver. 4.6.8 has been released:

https://www.nuget.org/packages/NLog

You can now use ${basedir:fixtempdir=true}.

Also if you place a config-file with the naming MyConsoleApp.exe.nlog side-by-side your MyConsoleApp.exe then it should take priority (Will not take priority over NLog.config, that will be resolved with NLog 5.0)

NLog 5 will prioritize FileName.exe.nlog as first priority (instead of AppDomain.BaseDirectory\nlog.config).

Curious what path do you get for this call when application is published:

  • System.Reflection.Assembly.GetEntryAssembly()?.CodPath

Tested myself, and it also resolves into temp-directory. So we need to re-fix the auto-loading of nlog.config to ignore when entry-assembly has been unpacked to temp-directory. Had to implement special fix to handle that process-name is often the launcher dotnet.exe (Now it is the other way around).

Below steps use the code above

  1. Publish single file.
  2. Copy single file to C:\tmp\sandbox\
  3. Run it
  4. Modify C:\tmp\sandbox\nlog.config log path ${currentdir}\log\${shortdate}.log
  5. Check the log file path

The log still write into ‘%Temp%/.net/ConsoleApp/xxxxxxx.xxx/’.

If remove or make same change on the file %Temp%/.net/ConsoleApp/xxxxxxx.xxx/nlog.config, it works fine.

The big exe file includes all output resources( include nlog.config). The big exe will extract all resources into %Temp%/.net/ConsoleApp/xxxxxxx.xxx/ on the first run.

You know, hiding ‘nlog.config’ and log file in %Temp%/.net/ConsoleApp/xxxxxxx.xxx/ is not good idea for the user, so I copy it to the working dir if not exists.