将Nlog日志记录到另一个目录

4

默认情况下,Nlog日志记录到默认的应用程序文件夹。

        <target xsi:type="File" name="f" 
      fileName="${basedir}/logs/${shortdate}.log"
             layout="${longdate} ${uppercase:${level}} ${message} 
   ${exception:message=tostring}" />

目前我的应用程序在C目录中,我希望Nlog记录到D目录的特定文件夹中。我已经阅读了相关的文档,

            fileName="${tempdir:folder=myapptmp}/sample.log"

并且

       ${specialfolder:dir=String:file=String:folder=Enum}

特殊文件夹似乎登录到“我的文档”、“图片”中,所以没有太多用处。关于Tempdir我不确定。有人之前做过这个或者有什么想法吗?

我的Nlog配置

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

    <!-- 
     See https://github.com/nlog/nlog/wiki/Configuration-file 
        for information on customizing logging rules and outputs.
        -->
      <targets>
     <!-- add your targets here -->


       <target name="File" xsi:type="File"  
     fileName="D:\Sushil\DwebLogging\log-${date:format=yyyy-MM-dd}.log"
        layout="${longdate} ${uppercase:${level}} ${message} ${exception: 
     format=tostring}" />
        <rules>
         <logger name="*" minlevel="Trace" writeTo="File" />
      </rules>
      </nlog> 

1
上面的示例中没有闭合的</targets>标签。这是复制/粘贴问题还是准确的? - DiskJunky
2
啊..原来是这个问题。谢谢 :) - Sushil
2个回答

5

只需在 fileName 属性中指定完整路径,如下所示:

<target name="File" xsi:type="File"  fileName="D:\Logging\SomeFile-${date:format=yyyy-MM-dd}.log">

一个完整的可用样例是:
<?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"
      autoReload="true">

  <targets>
    <target name="File" xsi:type="File"  fileName="D:\Logging\Sample-${date:format=yyyy-MM-dd}.csv">
      <layout xsi:type="CsvLayout">
        <column name="Index" layout="${counter}" />
        <column name="ThreadID" layout="${threadid}" />
        <column name="Time" layout="${longdate}" />
        <column name="Severity" layout="${level:uppercase=true}" />
        <column name="Location" layout="${callsite:className=False:fileName=True:includeSourcePath=False:methodName=False}" />
        <column name="Detail" layout="${message}" />
        <column name="Exception" layout="${exception:format=ToString}" />
      </layout>
    </target>
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="File" />
  </rules>
</nlog>

如果您仍然无法看到您的文件,则可能是应用程序正在运行的进程对该文件没有写入访问权限(默认情况下,NLog将静默失败)。如果您的应用程序是在IIS中的Web应用程序,那么这种情况很常见,因为IIS进程需要对文件夹具有写入访问权限,而默认情况下它是没有的。


我尝试过以下方式,但它没有工作: <target name="File" xsi:type="File" fileName="D:\Sushil\DwebLogging\log-${date:format=yyyy-MM- dd}.log" layout="${longdate} ${uppercase:${level}} ${message} ${exception: format=tostring}" /> <rules> <logger name="*" minlevel="Trace" writeTo="File" /> </rules> - Sushil
你的完整 NLog.config 是什么?目标是否设置并匹配 <target name="File"> - DiskJunky
更新了带有Nlog.config的问题。 - Sushil
更新了带有可工作配置的答案,并提供了在IIS上可能遇到的问题。 - DiskJunky

1

我也研究了这个问题,并从相对路径中找到了解决方法,因为当它部署到服务器(例如Azure)时,我并不总是知道完整的路径。

我的配置文件位于项目根目录以便检测它。

在NLog.config中,您需要在目标中添加一个点以使其成为相对路径。

不起作用的版本:

  <target name="logfile" xsi:type="File" fileName="/App_Data/Logs/file.txt" /> 

工作版本:
  <target name="logfile" xsi:type="File" fileName="./App_Data/Logs/file.txt" /> 

我的规则长什么样子:

 <logger name="*" minlevel="Debug" writeTo="logfile" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接