【问题标题】:NLog - create log file at specific directory in linuxNLog - 在 linux 的特定目录中创建日志文件
【发布时间】:2021-01-28 18:57:15
【问题描述】:

我在 linux 服务器上有一个 .net 核心应用程序。在我使用 nlog 进行日志记录的应用程序中。我在 linux 上的应用程序路径是/var/www-ninja/html/finance.api.gurukul.ninja。但是使用 nlog 我想将日志存储在其他 linux 目录中。这就像/var/log/api/ninja/finance。所以我可以将日志存储在该目录中。我怎样才能做到这一点 ?了解更多

nlog.production.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="Info"
          internalLogFile="c:\temp\internal-nlog.txt">

        <extensions>
            <add assembly="NLog.Web.AspNetCore"/>
            <add assembly="NLog.Extensions.Logging"/>
            <add assembly="NLog"/>
        </extensions>

        <variable name="ExceptionLayout" value="${longdate} [${processid}] ${uppercase:${level}} ${logger:shortName=true} ${environment-user} ${local-ip} ${aspnet-request-url} ${aspnet-request-method} ${message}${exception:format=tostring,Stacktrace}"/>
        <variable name="CommonLayout" value="${longdate} [${processid}] ${uppercase:${level}} ${logger:shortName=true} ${environment-user} ${local-ip} ${message} "/>
        <variable name ="logDir" value="/var/log/api/ninja/finance" />
   
        <targets async="true">
            <target xsi:type="File" name="file"  layout="${CommonLayout}" fileName="${logDir}\log-${shortdate}.log" />
            <target name="fileAsException"
                    xsi:type="FilteringWrapper"
                    condition="length('${exception}')>0">
                <target xsi:type="File"
                        fileName="${logDir}\log-${shortdate}.log"
                        layout="${ExceptionLayout}" />
            </target>
        </targets>
        <rules>
            <logger name="*" writeTo="file,fileAsException"/>
            <logger name="Microsoft.*" maxlevel="Info" final="true" />
        </rules>
    </nlog>

【问题讨论】:

  • 确保使用 Unix 路径,因此停止使用反斜杠 \ 。前任。而不是${logDir}\log-${shortdate}.log,那么它应该是${logDir}/log-${shortdate}.log。如果仍然有问题,请尝试激活 NLog InternalLogger 并检查输出github.com/NLog/NLog/wiki/Internal-Logging
  • 是的,这就是问题

标签: linux logging nlog


【解决方案1】:

确保使用 Unix 路径,因此停止使用反斜杠 \

例如。而不是${logDir}\log-${shortdate}.log,那么它应该是${logDir}/log-${shortdate}.log

如果仍有问题,请尝试激活 NLog InternalLogger 并检查输出 https://github.com/NLog/NLog/wiki/Internal-Logging

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多