$ {basedir}位于哪里,使用NLog?

Spi*_*kee 26 .net c# vb.net logging nlog

除非我完全失踪了,我的印象中NLOG文档使用${basedir}在它的例子,没有解释什么是它的位置应该是.

在哪里可以找到列出所有可能选项的信息?

我定义了这个配置:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">
  <targets>
    <target name="file" xsi:type="File"
                layout="${longdate} ${logger} ${message}"
                fileName="${basedir}/logs/${shortdate}.txt"
                keepFileOpen="false"
                encoding="iso-8859-2" />
  </targets>
  <rules>
    <logger name="*" minlevel="Debug" writeTo="file" />
  </rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)

它的工作范围我可以告诉,但我还没有得到任何记录在哪里的线索.

小智 25

${basedir} - 运行应用程序的目录.

我想,你会发现这个手册页很有帮助.

  • 那么`bin\debug`文件夹,如果你从VS运行它?在这种情况下,它不起作用,因为我没有看到任何文件.我没有收到任何错误. (3认同)

Ale*_*xei 6

根据已经提供的答案和评论,可以将答案总结为 .NET 应用程序:

AppDomain.CurrentDomain.BaseDirectory
Run Code Online (Sandbox Code Playgroud)

对于控制台或 Windows 窗体应用程序,此目录bin/debug位于 Visual Studio 中。如果部署了应用程序,路径很可能是可执行路径。

对于 Web 应用程序 (ASP.NET),这将是 Web 应用程序根目录。

看不到任何文件可能由多种原因触发,包括: NLog 配置错误和无法写入目标文件。要暴露这些错误,请确保 NLog.config(或嵌入在 web.config 或 app.config 中的 Nlog 配置)指定一个内部日志文件来输出此类错误:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      internalLogFile="C:\NLogError\NLog.log">

<!-- targets and rules come here -->

</nlog>
Run Code Online (Sandbox Code Playgroud)

  • 关于 internalLogFile 的好提示。 (2认同)