【问题标题】:Redirect output from specific logger to a specific NLog Target将特定记录器的输出重定向到特定的 NLog 目标
【发布时间】:2021-05-04 03:46:56
【问题描述】:

使用 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 中。 我需要知道是否有办法从所有日志/所有记录所有内容的文件中排除特殊目标?

【问题讨论】:

    标签: nlog asp.net-core-3.1


    【解决方案1】:

    我会将 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-15
      • 1970-01-01
      • 2019-12-23
      • 2012-03-13
      • 1970-01-01
      • 2023-04-08
      相关资源
      最近更新 更多