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

时间:2016-02-05 12:30:51

标签: c# .net vb.net logging nlog

除非我完全错过它,否则我会认为NLog documentation在其示例中使用了${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>

据我所知,它可以正常工作,但我还没有找到任何记录的地方。

4 个答案:

答案 0 :(得分:22)

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

我认为,您会发现this manual page有帮助。

答案 1 :(得分:3)

这也取决于平台。对于常规.Net项目,它确实是bin或bin / debug等(取决于你的构建设置)

对于coreclr / aspnet5,它是dnx运行时的bin文件夹。那还在进行中。

答案 2 :(得分:3)

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

AppDomain.CurrentDomain.BaseDirectory

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

对于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>

答案 3 :(得分:3)

失败的另一种可能性是,如果您使用NLog.config,NLog无法找到配置文件。在您的构建中将文件设置为Copy Always,它将在bin目录中结束,因此NLog可以在运行时找到它。

如果将NLog配置信息复制到App.config,则不会出现此问题。

相关问题