14

I have a problem with NLog for logging its internal logs with this configuration

<?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"
  throwExceptions="true"
  internalLogFile="${basedir}/App_Data/NLog.log"
  internalLogLevel="Trace">

   <targets>
      <target name="debug"
              xsi:type="File" 
              fileName="${basedir}/App_Data/Site.log" />
   </targets>

   <rules>
      <logger name="*"
              writeTo="debug" />
   </rules>
</nlog>

The target "debug" is working well, but the internalLogFile is only working if I set it for exemple to "D:/NLog.log".

Any idea why this happening?

dove
  • 20,469
  • 14
  • 82
  • 108
JuChom
  • 5,717
  • 5
  • 45
  • 78
  • Note; the default path is where ever IISExpress is installed. If you specify: InternalLogFile="nLogError.txt". Then (at least on my PC) it will try and write to "C:\Program Files (x86)\IIS Express\nlogExceptions.txt". Which can have permission problems. – OzBob Apr 14 '15 at 04:24

4 Answers4

12

You can't use layout renderers ${...} in the internalLogFile property. They are for a target's layout only:

<target layout="${...}" />

Try to use relative path like "..\App_Data\NLog.log"

Update NLog 4.6 enables some simple layouts.

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
kolbasov
  • 1,548
  • 11
  • 15
  • 1
    Thanks for the answer. Relative path are not working, it seems that it needs absolute path. – JuChom Feb 24 '12 at 11:14
  • you are wrong..i am using ${} in internal logging and it works fine!! @Swell u can use ${..} in internal log file – Neel Mar 12 '14 at 09:44
  • @Neel Well, it might have changed after 2 years :) Could you show your NLog.config? – kolbasov Mar 13 '14 at 17:39
  • 2
    internalLogFile="${specialfolder:folder=CommonApplicationData}/Logs/Nlog/${shortdate}/Nlog-Internal.log" – Neel Mar 14 '14 at 11:04
  • yes @justips that is possible may be this has been expanded in 2 years – Neel Mar 14 '14 at 11:05
  • 3
    Very good comment from a NLog collaborator on why this is not a good idea [internalLogFile: NLog variables](https://github.com/NLog/NLog/issues/581#issuecomment-74923718) – Gonzalo Contento Feb 18 '15 at 19:01
  • 1
    for the record the comment mentioned was " 304NotModified commented on 18 Feb 2015 | As answered at Stackoverflow: this isn't possible and also unwanted. The internal logfile should be as stable as possible (a so no fancy features). Also I think thank the internal log should only be used in rare cases." – Tim Abell Oct 28 '16 at 15:08
5

The internalLogFile attribute needs to be set to an absolute path and the executing assembly needs to have permission to write to that absolute path.

The following worked for me.

  1. Create a folder somewhere - e.g. the route of your c: drive, e.g. c:\logs
  2. Edit the permissions of this folder and give full control to everyone
  3. Set your nlog config: internalLogFile="C:\logs\nlog.txt"

Remember to clean up after yourself and not leave a directory with those sorts of permissions on

Ed Spencer
  • 462
  • 1
  • 8
  • 21
  • 2
    Thx for the tip. That worked for me. It appears that .NET Framework requires an absolute path, but ASP.NET Core allows for relative paths. – Tod Birdsall Oct 10 '19 at 17:39
1

NLog ver. 4.6 add support for environment-variables like %appdata% or %HOME%, and using these basic layouts in internalLogFile=:

  • ${currentdir}
  • ${basedir}
  • ${tempdir}

NLog ver. 4.7 also adds this:

  • ${processdir}

See also: https://github.com/NLog/NLog/wiki/Internal-Logging

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
0

from this link I think the path is absolute

Willy
  • 1,689
  • 7
  • 36
  • 79