【问题标题】:NLog Not WritingNLog 不写
【发布时间】:2018-05-10 06:01:11
【问题描述】:

我已将 NLog 添加到我的项目中,并且在开发环境中,它工作正常。

我创建了一个安装文件来部署我的应用程序。 NLog.config 文件未在安装项目中显示为依赖项。因此,我将其添加为一个文件,并且在部署时它与 exe 文件和 App.config 位于同一目录中。

它不做任何日志记录。我不知道为什么。这是配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<?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">
  <variable name="day" value="${date:format=dd}" />
  <variable name="month" value="${date:format=MM}" />
  <variable name="year" value="${date:format=yyyy}" />  
  <variable name="verbose" value="${longdate} | ${level} | ${message} | ${exception:format=tostring,message,method:maxInnerExceptionLevel=5:innerFormat=shortType,message,method}}" />

  <targets>
    <target name="logfile" xsi:type="File" fileName="${basedir}/Logs/${year}${month}${day}.log" layout="${verbose}" />
  </targets>

  <rules>
    <logger name="*" minlevel="Error" writeTo="logfile" />
  </rules>
</nlog>

任何帮助都会很棒。干杯!

【问题讨论】:

    标签: nlog


    【解决方案1】:

    NLog.config 是否将属性“复制到输出目录”设置为“始终复制”? https://stackoverflow.com/a/8881521/438760

    【讨论】:

    • 是的。我确保将其设置为“始终复制”。
    【解决方案2】:

    将您的 NLog 配置放入 yourapp.exe.config 文件中。像这样:

    <configuration>
       <configSections>
          <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
       </configSections>
       <nlog>
          <variable name="day" value="${date:format=dd}" />
          ...
          <targets>
             <target name="logfile" xsi:type="File" .../>
          </targets>
          <rules>
             <logger name="*" minlevel="Error" writeTo="logfile" />
         </rules>
       </nlog>
    </configuration>
    

    【讨论】:

    • xsi 标签抛出一个错误。我如何在其中获取架构?
    • 粘贴您自己的配置、xmlns 属性和所有内容。为了便于阅读,我删除了上面的一些内容(因此是点)。还;参见例如snipplr.com/view/24560 和 nlog-project.org/wiki/Visual_Studio_support
    • 那东西看起来不错。我会去看看它是怎么回事。
    • 把它放在 app.config 中工作。感谢您的帮助。
    【解决方案3】:

    我猜双 xml 版本语句(第 1 行和第 2 行)是复制/粘贴问题....

    可能是一个愚蠢的问题,但您将 minLevel 设置为 Error。您是否确实遇到了将被记录的错误,或者您是否尝试过将其降低为信息或调试?

    【讨论】:

    • 这是一个粘贴错误,我故意抛出一个异常,该异常在开发条件 (IDE) 中记录良好。
    【解决方案4】:

    对我来说,我的 NLog 停止记录的原因与上述建议不同。

    在更新包的过程中,一定有一些东西自动在我的 Web.config 的底部添加了一个 nlog 部分。它可能是 ApplicationInsights。显然这是优先考虑的,这导致它停止检查我的 Nlog.config 文件。

    一旦我从 web.config 中删除了该部分,它就会再次神奇地从我的 nlog.config 文件中读取。我注意从 web.config 中获取扩展、目标和规则,并确保它们整齐地放置在我的 nlog.config 文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-01
      • 1970-01-01
      • 2021-04-30
      • 1970-01-01
      相关资源
      最近更新 更多