gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:29:05 26 4
gpt4 key购买 nike

为什么我不能在我的生产服务器上使用 ${basedir} nlog.config?如果我使用 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>

Controller :

public class HomeController : Controller
{
private static Logger logger = LogManager.GetCurrentClassLogger();

public ActionResult Index()
{
logger.Debug("Test NLog");
return View();
}
}

最佳答案

此问题与文件权限有关。简短的故事:运行 Web 应用程序的用户没有写入日志文件的权限。

根据 .NET 的版本和 IIS 的版本,可能有多种用户。

对于 IIS 6,我会说 Web 应用程序作为 NETWORK SERVICE 帐户运行。

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

关于c# - 为什么 ${basedir} NLog 配置不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23729629/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com