Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用NLog,${basedir}位于哪里?_C#_.net_Vb.net_Logging_Nlog - Fatal编程技术网

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

C# 使用NLog,${basedir}位于哪里?,c#,.net,vb.net,logging,nlog,C#,.net,Vb.net,Logging,Nlog,除非我完全忽略了它,否则我的印象是在示例中使用了${basedir},而没有解释它的位置应该是什么 我在哪里可以找到列出所有可能选项的信息以及有意义的描述 我已定义此配置: <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true"> <targets>

除非我完全忽略了它,否则我的印象是在示例中使用了
${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>


据我所知,它工作正常,但我不知道它在哪里记录任何东西。

${basedir}
-应用程序运行的目录,又名


我想,你会发现这很有帮助。

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


对于coreclr/aspnet5,它是dnx运行时的bin文件夹。这项工作仍在进行中

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

AppDomain.CurrentDomain.BaseDirectory
对于控制台或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>

另一种失败的可能性是,如果您使用的是NLog.config,则NLog无法找到配置文件。将文件设置为在构建中始终复制,它将最终位于bin目录中,以便NLog可以在运行时找到它


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

因此,如果您是从VS运行
bin\debug
文件夹,则?在这种情况下,它不起作用,因为我看不到任何文件。但我没有收到任何错误。@Spike您可以通过AppDomain.CurrentDomain.BaseDirectory在应用程序中检查它,它确实是
bin\debug
,但我看不到
日志
子文件夹,也看不到任何文件。@Spike我现在就试过了,它对我有效。您确定捕获并记录了异常吗?您确定要调用LogManager.Flush()吗;关闭应用程序之前?请注意,对于asp.net项目${basedir}(以及AppDomain.CurrentDomain.BaseDirectory),请指向网站的根目录,该目录(在使用visual studio时)是包含asp.net网站的入门级asp.net项目源代码的目录(也称为“bin/[Debug Release]”在visual studio中进行调试时,ASP.NET网站不存在该部分)。我找不到我的日志文件,因为我在使用VS时在错误的文件夹中查找它们。有趣吧?请记住在Visual Studio中更改文件属性,以便NLog配置文件复制到输出目录=>Always。另一种方法是将nlog配置注入app.config。在测试期间,我还遇到了一个类似的问题,我需要在Visual Studio项目中打开“复制到输出”选项以获得“nlog.xsd”(在nlog配置中引用)的副本,然后才能在“bin/debug”文件夹中写入任何日志。切勿将任何设置为“始终复制”。使用“如果更新,则复制”否则您的构建将始终被视为过时(即使没有任何更改,它也将编译项目)internalLogFile的好提示。我们知道
${basedir}
是否可以在
internalLogFile
中使用?这仍然是真的吗?