NLog - 在 linux 中的特定目录创建日志文件

NLog - create log file at specific directory in linux

我在 linux server.also 上有一个 .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-path,因此停止使用反斜杠 \

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

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