【问题标题】:Is chaining of Loggers in NLog supported through configuration without changing the code?是否通过配置支持 NLog 中的 Logger 链接而不更改代码?
【发布时间】:2019-06-27 12:42:01
【问题描述】:

这是我的nlog的配置:

<targets async="true">
    <target name="console" xsi:type="ColoredConsole" useDefaultRowHighlightingRules="false" layout="${date:format=MMM dd, yyyy hh\:mm\:ss.ff tt} | ${level} ${message}" >
    </target>

    <target xsi:type="File" name="app" fileName="logs\quantum_${shortdate}.log"
            layout="${date:universalTime=True:format=MMM dd, yyyy hh\:mm\:ss.ff tt} | ${event-properties:item=EventId_Id} ${uppercase:${level}} | ${message} ${exception:format=message}" />

    <target xsi:type="File" name="errors" fileName="logs\exceptions_${shortdate}.log"
            layout="${date:universalTime=True:format=MMM dd, yyyy hh\:mm\:ss.ff tt} | ${event-properties:item=EventId_Id} ${uppercase:${level}} | ${message} ${exception:format=tostring}" />

    <target xsi:type="File" name="aspcore" fileName="logs\aspcore_${shortdate}.log"
            layout="${date:universalTime=True:format=MMM dd, yyyy hh\:mm\:ss.ff tt} | ${event-properties:item=EventId_Id} | ${uppercase:${level}} | ${message} ${exception:format=tostring} | url: ${aspnet-request-url} | action: ${aspnet-mvc-action}" />
</targets>

<rules>
    <logger name="app" minlevel="Trace" writeTo="app,console" />
    <logger name ="appErrors" minlevel="Error" writeTo="errors" />
    <logger name="Microsoft.*" maxLevel="Error" writeTo="aspcore"/> 
</rules>

现在我想链接记录器,而不根据条件更改代码。所以我登录到 app 但是当有异常时, app 应该将它传递给 appErrors ,它实际上可以记录完整的异常。

所以最终应用程序只包含异常消息。而 appError 将包含所有发生的异常的详细信息。是否可以在 nlog 中使用一些配置?

【问题讨论】:

    标签: c# asp.net nlog


    【解决方案1】:

    看起来你已经明白了。我不明白 appErrors 应该做什么,但你可以这样做:

    <rules>
        <logger name="app" minlevel="Trace" writeTo="app,console" />
        <logger name="app" minlevel="Error" writeTo="errors" />
        <logger name="Microsoft.*" maxLevel="Error" writeTo="aspcore"/> 
    </rules>
    

    因此,来自应用程序的所有 LogLevel >= Error 的日志都将在logs\quantum_${shortdate}.loglogs\exceptions_${shortdate}.log 中(仅错误日志)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-20
      • 2020-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-05
      • 2018-06-04
      • 1970-01-01
      相关资源
      最近更新 更多