Nlog: FileLogger and InternalFileLogger relative path

Created on 13 Oct 2017  路  17Comments  路  Source: NLog/NLog

When i am logging with relative pathes in Nlog.Web.AspNetCore 4.5.0 Beta01 i get different locations for relative Pathes in internalLogFile and FileLogger.

InternalLog logs relative from project-directory.
FileLogger logs relative from project-directorybin\Debug\netcoreapp2.0.

With previous Versions of Nlog.Web.AspNetCore both relative pathes were based from project-directory.

This is the configuration i need to get the same log folder in my project root directory.

<?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"
      internalLogFile="..\..\logs\internal-NLog-web.txt">

  <variable name="formattedMessage" value="${message}${onexception:${newline}${exception:format=ToString}}" />
  <variable name="defaultLayout" value="${date:format=yyyy-MM-dd HH\:mm\:ss.fff} [${threadid}] ${level:uppercase=true} ${callsite} - ${formattedMessage}" />
  <variable name="logFolder" value="..\..\..\..\..\logs" />

  <targets async="true">
    <target xsi:type="File" name="rootFileTarget" fileName="${logFolder}\web-${shortdate}.log" layout="${defaultLayout}" keepFileOpen="true" />
    <target xsi:type="File" name="msFileTarget" fileName="${logFolder}\ms-${shortdate}.log" layout="${defaultLayout}" keepFileOpen="true" />
  </targets>

  <rules>
    <logger name="Microsoft.*" minlevel="Debug" writeTo="msFileTarget" />
    <logger name="Microsoft.*" final="true" />
    <logger name="*" minlevel="Debug" writeTo="rootFileTarget" />
  </rules>
</nlog>
bug file-target

Most helpful comment

@nimeshdhruve NLog 4.6 will support the following tokens for internalLogFile:

  • ${basedir}
  • ${currentdir}
  • ${tempdir}
  • Environment-Variables (Ex. %TEMP%)

See also https://github.com/NLog/NLog/pull/3031 and https://github.com/NLog/NLog/pull/3092

All 17 comments

Thanks for reporting! This is a bug.

Please post your config between triple backticks, thanks! (No encoding is needed then)

With previous Versions of Nlog.Web.AspNetCore

Which version and more import, which version of NLog? 5. beta? 4.4?

I can reproduce this issue, but please provide the version when it did worked.

Cannot explain why it isn't working

@snakefoot any idea?

No idea, but if you can reproduce then you should be able to fix it

My previous version was NLog.Web.AspNetCore 4.3.1 + NLog 5.0.0-beta04.
Cant get the complete config today, cause its on my working pc.

But it was this relative pathes to get the same parent folder:
-nlog with internalLogFile=".....\logs\internal-NLog-web.txt"
-target xsi:type="File" with fileName="..........\web-${shortdate}.log"

nlog internal 2 folders up
nlog target 5 folders up

it seems nlog internal log is based on project dir / working dir
and nlog target is based on the location of the binaries which is bin\Debug\netcoreapp2.0

I think I have found the issue. NLog 4.5 auto reading the config is working better then 5 ;)

The relative path of the file target is relative from nlog.config. For the internal logger it's fixed.

Solution is to not copy nlog.config to the bin directoy, so:

image

not sure how this works when publishing.

I already have Nlog.config as "Do not copy".
But nlog target seems to use C:\Git\myproject\Sources\Webbin\Debug\netcoreapp2.0 as root for the relative path, even if there is no config file there.

It`s not a big issue for me, cause i can use different relative pathes.

And when publishing to production i dont't mind cause i am running in docker container which wil always end in C:\logs then even with 5x "..".

But it would still be nice if the relative pathes would be handled equal, to avoid confusing :)

` Datentr鈥瀏er in Laufwerk C: ist Windows
Volumeseriennummer: 460F-9B5A

Verzeichnis von C:\Git\myproject\Sources\Webbin\Debug\netcoreapp2.0

[.]
[..]
Web.deps.json
Web.dll
Web.pdb
Web.runtimeconfig.dev.json
Web.runtimeconfig.json
32 Datei(en), 575.971 Bytes
2 Verzeichnis(se), 335.654.088.704 Bytes frei
`

but where is the nlog.config located in your folders?

`
Datentr鈥瀏er in Laufwerk C: ist Windows
Volumeseriennummer: 460F-9B5A

Verzeichnis von C:\Git\myproject\Sources\Web

[.]
Api
appsettings.development.json
appsettings.json
appsettings.local.json
appsettings.production.json
appsettings.staging.json
appsettings.user.json
bin
cert
NLog.config
obj
project.lock.json
Properties
tools
web.config
Web.csproj
Web.csproj.user
Web.xproj.user
wwwroot
`

Just wanting to report something that may well be related to this issue as well. We currently are deploying a new web service written in Asp.NET Core 2.0 and the deployment is via TeamCity and Octopus. During deployment Octopus adds the relevant paths for the targets to write the log files to. In our case on UAT it adds a path that contains braces ie. ......\UAT (Test)....... and this seems to cause NLog to write log files as UAT (Test) rather than the actual target. Just removing the UAT (Test) part from the path allows the log files to be written as expected. So question is, are braces seen as invalid characters in the target filename?

Just wanting to report something that may well be related to this issue as well. We currently are deploying a new web service written in Asp.NET Core 2.0 and the deployment is via TeamCity and Octopus. During deployment Octopus adds the relevant paths for the targets to write the log files to. In our case on UAT it adds a path that contains braces ie. ......\UAT (Test)....... and this seems to cause NLog to write log files as UAT (Test) rather than the actual target. Just removing the UAT (Test) part from the path allows the log files to be written as expected. So question is, are braces seen as invalid characters in the target filename?

@oggythorpe1610 it this the same question as https://github.com/NLog/NLog.Web/issues/285?

I encountered the same problem, a quick solution is to set InternalLogger.LogFile in your Main method.

This is how I am doing it:

```c#
public static void Main(string[] args)
{
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
try
{
InternalLogger.LogFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs", "nlog-internals.txt");
BuildWebHost(args).Run();
}
catch (Exception ex)
{
logger.Error(ex, "Stopped program because of exception");
throw;
}
finally
{
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
NLog.LogManager.Shutdown();
}
}

And below is my config file:

```xml
    <?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="Trace">

      <!-- enable asp.net core layout renderers -->
      <extensions>
        <add assembly="NLog.Web.AspNetCore"/>
      </extensions>

      <!-- the targets to write to -->
      <targets>
        <!-- write logs to file  -->
        <target xsi:type="File" name="allfile" fileName="logs/nlog-all-${shortdate}.log"
                layout="${longdate}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />

        <!-- another file log, only own logs. Uses some ASP.NET core renderers -->
        <target xsi:type="File" name="ownFile-web" fileName="logs/nlog-own-${shortdate}.log"
                layout="${longdate}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
      </targets>

      <!-- rules to map from logger name to target -->
      <rules>
        <!--All logs, including from Microsoft-->
        <logger name="*" minlevel="Trace" writeTo="allfile" />

        <!--Skip non-critical Microsoft logs and so log only own logs-->
        <logger name="Microsoft.*" maxLevel="Warn" final="true" />
        <!-- BlackHole without writeTo -->
        <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
      </rules>
    </nlog>

Final output:
output

One drawback of this approach is that you need to change path in two places (Program.cs and nlog.config) if it ever changes.

@nimeshdhruve NLog 4.6 will support the following tokens for internalLogFile:

  • ${basedir}
  • ${currentdir}
  • ${tempdir}
  • Environment-Variables (Ex. %TEMP%)

See also https://github.com/NLog/NLog/pull/3031 and https://github.com/NLog/NLog/pull/3092

@nimeshdhruve NLog 4.6 will support the following tokens for internalLogFile:

  • ${basedir}
  • ${currentdir}
  • ${tempdir}
  • Environment-Variables (Ex. %TEMP%)

See also NLog/NLog#3031 and NLog/NLog#3092

Think we can close this one then?

Think we can close this one then?

Yes I think this can closed as resolved with NLog 4.6

Was this page helpful?
0 / 5 - 0 ratings