asp.net 4.6.1 无法使用 NLog 创建日志文件

asp.net 4.6.1 can't create log file using NLog

我正在尝试使用 NLog 4.4.12 记录一些应用程序信息,但我似乎无法在我的 C drive 中创建日志文件。

我在 .net 4.6.1

上有一个 class 图书馆 运行

我在该项目的根目录中创建了一个 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 name="logfile" xsi:type="File" fileName="c:\ApiLogfile.txt" />
  </targets>

  <rules>
    <logger name="*" minlevel="Info" writeTo="logfile" />
  </rules>
</nlog>

当我调用 logger.Info("NLog to the rescue") 时,输出文件未在 C 盘中创建。我在这里遗漏了什么吗?

我无法使用

让它工作
<target name="logfile" xsi:type="File" fileName="c:\ApiLogfile.txt" />

这可能是一个权限问题,如果您以管理员身份执行该应用程序,它确实有效。

这两个地点对我有用:

<target name="logfile" xsi:type="File" fileName="${basedir}/ApiLogfile.txt" />
<target name="logfile" xsi:type="File" fileName="C:\Users\{username}\Documents\ApiLogfile.txt" />

同时确保 复制到输出目录 设置为 始终复制 以确保 NLog.config 文件被复制到输出目录,这是 NLog 程序集期望的位置。