繁体   English   中英

Nlog 日志到另一个目录

[英]Nlog log to another directory

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

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

我也研究了这个,我从相对路径中找出了它,因为当它部署到服务器(例如 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" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM