将特定记录器的输出重定向到特定的 NLog 目标

Redirect output from specific logger to a specific NLog Target

使用 ASP.NET Core3.1 和 NLog,这是我的一部分 NLog.config:

<nlog>
   <targets>
        <!-- write logs to file  -->
        <target name="allfile" xsi:type="File" 
                fileName="${aspnet-appbasepath}/AspNetCore_Nlog/nlog-all-${shortdate}.log"
                layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}  | url: ${aspnet-request-url} | controller: ${aspnet-mvc-controller}  | action: ${aspnet-mvc-action}"
                archiveAboveSize="1000000"
                maxArchiveFiles="20" />

        <target name="searchedFile" xsi:type="File"
                fileName="${aspnet-appbasepath}/AspNetCore_Nlog/whatIsSearched-${shortdate}.txt"
                layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message}"
                archiveAboveSize="1000000"
                maxArchiveFiles="20"/>
    </targets>

    <!-- rules to map from logger name to target -->
    <rules>
        <logger name="*" minlevel="Trace"  writeTo="allfile" />     
        <logger name="whatIsSearched" minlevel="Trace" writeTo="searchedFile" />
    </rules>
</nlog>

我的问题是,searchedFile 中的所有日志也存在于 allfile 中。 我需要知道是否有办法从记录所有内容的所有 logs/all 文件中排除一个特殊目标?

我会将 whatIsSearched 的日志记录规则移动到顶部,并添加 final="true":

<rules>
   <logger name="whatIsSearched" minlevel="Trace" writeTo="searchedFile" final="true" />
   <logger name="*" minlevel="Trace" writeTo="allfile" />
</rules>

另请参阅:https://github.com/nlog/NLog/wiki/Configuration-file#rules