Nlog: when .net core app published as single file - ${basedir} is wrong

Created on 14 Feb 2020  路  3Comments  路  Source: NLog/NLog

Sorry for my english.
I've created project to teach my team how to migrate simple console app to .core 3.1. All is ok with nlog, all is working as expected. BUT. When i publish as "produce single file" - ${basedir} is not respond to real situation. As i understand - when i run application as single file - dotnet "unpacks" file to temp folder and run from it. In my case log files appeared in C:\TEMP.net\gOldCleaner\lcieddmk.fr0\ when application launched from D:_TEMP\gOld31\

e.g.
simple example project

NLog version: 4.6.8

Platform: .NET Core 3.1

Current NLog config

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true">
  <variable name="logDirectory" value="${basedir}/logs" />
  <targets>
    <target name="console" xsi:type="ColoredConsole" layout="${message}" />
    <target name="LogFileInOneFile" xsi:type="File" fileName="${logDirectory}/${date:format=yyyy-MM-dd}.log" layout="${longdate}|${pad:padding=5:inner=${level:uppercase=true}}|${message}|${exception:format=ToString,StackTrace}" />
  </targets>
  <rules>
    <logger name="*" minLevel="Info" maxLevel="Info" writeTo="console" />
    <logger name="*" minLevel="Debug" writeTo="LogFileInOneFile" />
  </rules>
</nlog>

  • What is the current result?
    Application run from
    D:_TEMP\gOld31\gOldCleaner.exe but log appears in C:\TEMP.net\gOldCleaner\lcieddmk.fr0\logs

  • What is the expected result?
    Logs appeared in D:_TEMP\gOld31\logs if

<variable name="logDirectory" value="${basedir}/logs" />
  • Did you checked the Internal log?
    yes, and found path C:\TEMP.net\gOldCleaner\lcieddmk.fr0\ :)

  • Are there any workarounds? yes/no
    yes

  • don't use publish to single file
  • use absolute path like "c:\logs" instead of ${basedir}/logs

  • Is there a version in which it did work?
    Don't think so, single file publish is enough new feature

  • Can you help us by writing an unit test?
    yes :)

external file-target question

Most helpful comment

Duplicate of #3517 + #3738

Sadly enough Microsoft decided not to fix AppDomain.BaseDirectory when doing Single File Publish in NetCore 3.1

https://github.com/dotnet/aspnetcore/issues/12621
https://github.com/dotnet/core-setup/issues/7491

The work-around is that you have specify fixTempDir=true explicit so it becomes ${basedir:fixTempDir=true}:

<variable name="logDirectory" value="${basedir:fixtempdir=true}/logs" />

All 3 comments

Hi! Thanks for opening your first issue here! Please make sure to follow the issue template - so we could help you better!

Duplicate of #3517 + #3738

Sadly enough Microsoft decided not to fix AppDomain.BaseDirectory when doing Single File Publish in NetCore 3.1

https://github.com/dotnet/aspnetcore/issues/12621
https://github.com/dotnet/core-setup/issues/7491

The work-around is that you have specify fixTempDir=true explicit so it becomes ${basedir:fixTempDir=true}:

<variable name="logDirectory" value="${basedir:fixtempdir=true}/logs" />

Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vasumsit picture vasumsit  路  3Comments

FaMouZx3 picture FaMouZx3  路  3Comments

linmasaki picture linmasaki  路  3Comments

ericnewton76 picture ericnewton76  路  3Comments

carkov1990 picture carkov1990  路  3Comments