为什么${basedir} NLog配置不起作用?

本文关键字:配置 不起作用 NLog basedir 为什么 | 更新日期: 2023-09-27 18:04:03

为什么我不能使用${basedir} nlog ?在我的生产服务器中配置?如果我使用fileName="${basedir}/logs/${shortdate}.log", nlog不记录消息信息,但如果我更改为fileName="C:/logs/${shortdate}.log",它将记录消息信息

我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>

控制器:

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

为什么${basedir} NLog配置不起作用?

文件权限问题。简而言之:运行web应用程序的用户没有写日志文件的权限。

根据。net的版本和IIS的版本,它可能是各种各样的用户。

对于IIS 6,我会说web应用程序以NETWORK SERVICE帐户运行。

因此,您需要授予该用户权限写入/修改 logs文件夹(我假设它是web根目录的子文件夹)。如果你想让NLog自动创建logs文件夹,你需要将这些权限授予web根用户。