【问题标题】:Nlog log to another directoryNlog 日志到另一个目录
【发布时间】:2017-09-22 10:53:15
【问题描述】:

默认情况下,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}

特殊文件夹似乎登录到 MyDocuments、Pictures。所以用处不大。 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> 

【问题讨论】:

  • 以上示例中没有结束 &lt;/targets&gt; 标记。复制/粘贴问题还是准确?
  • 啊..这就是问题所在。谢谢:)

标签: c# .net asp.net-mvc logging nlog


【解决方案1】:

只需像这样在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 进程将需要对默认情况下不具有的文件夹的写入权限。

【讨论】:

  • 我尝试如下但它不起作用
  • 你的完整 NLog.config 是什么?目标是否设置并匹配&lt;target name="File"&gt;
  • 使用 Nlog.config 更新问题
  • 使用工作配置和 IIS 上的潜在问题更新了答案
【解决方案2】:

我也对此进行了研究,并从相对路径中找出了它,因为当它部署到 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" />

【讨论】:

    猜你喜欢
    • 2017-10-16
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    相关资源
    最近更新 更多