简体   繁体   中英

Is chaining of Loggers in NLog supported through configuration without changing the code?

here is the configuration of my 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>

Now I want to chain the loggers, without changing the code based on condition. So that I log to app but when there is an Exception, the app should pass it down to appErrors which can actually record the complete exception.

so in the end app contains only the messages even of exceptions. while appError will contain the detail of all the exceptions that occurred. Is it possible using some configuration in nlog?

It looks like you already got that. I don't get what the appErrors should do, but you could do like this:

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

So that all logs from app with LogLevel >= Error will be both in logs\\quantum_${shortdate}.log and (only error logs) in logs\\exceptions_${shortdate}.log

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM