【问题标题】:using AppData location in NLog在 NLog 中使用 AppData 位置
【发布时间】:2011-01-01 07:02:52
【问题描述】:

我的 NLog 目标是这样的:

<targets>
  <target xsi:type="Console" name="console" 
    layout="${longdate}|${level}|${message}" />
  <target xsi:type="File" name="ErrorLog" fileName="${basedir}/error.txt"
          layout="${longdate}
          Trace: ${stacktrace} 
          ${message}" />
  <target xsi:type="File" name="AccessLog" fileName="${basedir}/access.txt"
          layout="${shortdate} | ${message}" />
</targets>

但是,如果用户不是他们机器上的管理员,这会导致问题,因为他们没有“程序文件”的写入权限。我怎样才能得到像%AppData% 这样的东西到 NLog 而不是 BaseDir?

【问题讨论】:

    标签: nlog


    【解决方案1】:

    您正在寻找NLog special folders

    例子:

    ...fileName="${specialfolder:folder=ApplicationData}/Program/file.txt"...
    

    【讨论】:

    • 是的,但是我该如何使用它呢?我已经尝试过了,fileName="${ApplicationData}/RentalEase/access.txt" 并且它默默地失败了。
    • 我不记得了,但它应该是这样的:${specialfolder:folder=SPECIALFOLDERNAME}
    • ${specialfolder:folder=ApplicationData}
    • 我看错了,以为你有一个“。”在特殊文件夹和文件夹之间而不是冒号
    • 如果失败,请尝试添加 &lt;nlog throwExceptions="true" /&gt; - ref: github.com/nlog/nlog/wiki/…
    【解决方案2】:

    奥伦的答案应该是正确的答案。但是,在我的一生中,我无法让它与使用 nLog 2.0.0.0 的 .NET 4.0 网站一起使用。我最终只使用了

    fileName="${basedir}app_data\logs\${shortdate}.log" 
    

    【讨论】:

    • 问题标题使用“AppData”,但我不认为 {..ApplicationData} 映射到 App_Data,我必须阅读源以仔细检查。
    • 这个问题和接受的答案是关于 Windows 特殊文件夹 %appdata%,通常是 C:\Users\\AppData\Roaming。 ASP.net App_Data 文件夹似乎没有特殊变量。
    【解决方案3】:

    ${specialfolder:ApplicationData} 也可以使用

    【讨论】:

      【解决方案4】:

      以前的答案帮助解决了我遇到的问题,但几年后,解决方案现在在 v4.3 下有所不同。目录和文件名与路径组合在一起。

      @theGecko 的链接仍然是最新的语法,但页面缺少示例:

      https://github.com/nlog/NLog/wiki/Special-Folder-Layout-Renderer

      以下示例将文件myLog.log写入当前用户应用程序数据漫游目录C:\USers\current.user\AppData\Roaming\My\Path\Somewhere

      fileName="${specialfolder:dir=My/Path/Somewhere/:file=myFile.log:folder=ApplicationData}"
      

      【讨论】:

        【解决方案5】:

        用于登录到项目目录:

        虽然前面的答案适用于原始问题,但搜索如何登录到项目APP_DATA 目录会导致这个问题。虽然bkaid 的答案适用于 ASP.NET 并且特别适用于使用 APP_DATA 文件夹,但对于 .NET Core 和 .NET 5,解决方案有点不同,因为该主题已被放弃以支持定义 @ 987654325@ 文件夹只存放那些应该提供的东西,其余的都是私有的。那么,.NET Core/5 的答案是写入解决方案根目录:

        1. 首先,确保将NLog.Web.AspNetCore 程序集添加到nlog.config
          <extensions>
              <add assembly="NLog.Web.AspNetCore"/>
          </extensions>
          
        2. 然后使用该扩展提供的布局渲染器之一,在本例中 ${aspnet-appbasepath} 引用解决方案根目录:
          <targets>
              <target name="file"
                      type="File"
                      xsi:type="File"
                      fileName="${aspnet-appbasepath}/log/${shortdate}.log"
                      layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}"/>
          </targets>
          

        这会将文件写入&lt;solution folder&gt;/log/2021-07-01.log,面向公众的网站将永远不会提供该文件。此程序集提供的其他布局渲染器是 listed on the NLog website

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-02-16
          • 2016-05-15
          • 2019-03-26
          • 2012-05-28
          • 2011-10-04
          • 2012-03-03
          • 1970-01-01
          相关资源
          最近更新 更多