【问题标题】:NLog works fine on windows .Net Core application but not on LinuxNLog 在 windows .Net Core 应用程序上运行良好,但在 Linux 上运行良好
【发布时间】:2020-05-25 20:07:29
【问题描述】:

我的 .Net Core 控制台应用程序中有这个 nlog.config 文件:

    <?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"
      internalLogLevel="Error"
      internalLogFile="c:\temp\IBTest\internal-nlog.txt">
  <targets>
    <target xsi:type="File" name="allfile" fileName="c:\temp\IBTest\nlog-all-${shortdate}.log"
                layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />
    <target xsi:type="File" name="ownFile-web" fileName="c:\temp\IBTest\nlog-own-${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|  ${message} ${exception}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
    <target xsi:type="Null" name="blackhole" />
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" writeTo="allfile" />
    <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
    <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
  </rules>
</nlog>

当我在 Windows 上启动我的应用程序时,它可以正常工作并创建所有应该创建的日志文件。 但是,当我在 Linux 上部署我的应用时,它不起作用。

这就是我的 nlog.config 文件在 Linux 上的样子:

<?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"
      internalLogLevel="Info"
      internalLogFile="/home/ib_tests_internal-nlog.log">
  <targets>
    <!-- write logs to file -->
    <target xsi:type="File" name="allfile" fileName="/home/ib_tests-all-${shortdate}.log"
                layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />

    <!-- another file log, only own logs. Uses some ASP.NET core renderers -->
    <target xsi:type="File" name="ownFile-web" fileName="/home/ib_tests_nlog-own-${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|  ${message} ${exception}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />

    <!-- write to the void aka just remove -->
    <target xsi:type="Null" name="blackhole" />
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="allfile" />

    <!--Skip Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
    <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
  </rules>
</nlog>

我真的不明白有什么问题。我检查了数百万次路径,所有文件(至少是我认为的文件)都具有读/写的所有权限。 我在 Windows 中尝试了 dotnet publish 然后 dotnet app.dll 并且它有效。当我在 Linux 上以相同的方式启动应用程序时,它不会。我真的没有想法。似乎我一直在做的只是从我拥有的样本中复制粘贴。有谁知道可能出了什么问题?或者还有什么要检查的?或一些手动方式来创建该日志文件并查看它是否运行?认真地坐了半天到我以为我会被解雇的地步

【问题讨论】:

  • ib_tests_internal-nlog.log 中的任何输出?您是否尝试过启用 ThrowConfigExceptions ?您是否正确配置了文件权限?你能在没有 NLog 的情况下写入文件吗?
  • 这可能会有所帮助:stackoverflow.com/questions/43875431/…
  • application.dllroot 上运行,所以我猜它拥有所有权限...
  • 按照我发布的链接中的建议,确认文件夹和文件名正确。请记住,Linux 文件系统区分大小写

标签: c# .net linux web-config nlog


【解决方案1】:

我的猜测是NLog.config 的文件名大小写不正确。除非您已将代码配置为使用默认值以外的其他内容,否则nlog.config 将不起作用,它必须是NLog.config(注意大写的 N 和 L)。

【讨论】:

  • 今天这是不正确的(在某一时刻它可能是正确的)。 Nlog documentation instructsnlog.config 文件命名为全小写。
【解决方案2】:

我刚刚遇到了类似的问题。 Nlog 在 Windows 和 Mac 开发机器上运行良好,但在部署到 Linux 时无法记录。对我来说,问题是权限(我通过反复试验发现了这一点,而不是从任何错误或消息中发现的)。尝试调整正在登录的目录的权限。

您直接登录到/home 目录,我建议您更新nlog.config 以使用/home/logs 子目录。

<target xsi:type="File" name="ownFile-web" fileName="/home/logs/ib_tests_nlog-own-${shortdate}.log" ...

然后设置权限:

sudo chmod -R 777 /home/logs

【讨论】:

    猜你喜欢
    • 2021-09-28
    • 2020-08-21
    • 1970-01-01
    • 2017-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多