Why is the ${basedir} NLog configuration not working?

18,220

This issue is related to file permission. Short story: the user that the web application is running as does not have permission to write the log files.

Depending on the version of .NET and the version of IIS, there are a variety of users that it could be.

For IIS 6, I would say that the web application is running as the NETWORK SERVICE account.

Therefore, you will need to grant this user permission to write/modify the logs folder (which I presume is a sub-folder of the web root). If you want the logs folder to be created automatically by NLog, you'll need to grant these permissions to the web root instead.

Share:
18,220
Willy
Author by

Willy

Updated on August 01, 2022

Comments

  • Willy
    Willy over 1 year

    Why I cannot use ${basedir} nlog.config in my production server? If I use fileName="${basedir}/logs/${shortdate}.log", nlog does not log the message info, but if I change to something like fileName="C:/logs/${shortdate}.log" it will log the message info

    my nlog.config:

    <?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">
    
      <targets>    
        <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
                layout="${longdate} ${uppercase:${level}} ${message}" />
      </targets>
    
      <rules>
        <logger name="*" minlevel="Trace" writeTo="f" />
      </rules>
    </nlog>
    

    Controller:

    public class HomeController : Controller
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
    
        public ActionResult Index()
        {
            logger.Debug("Test NLog");
            return View();
        }
    }