简体   繁体   English

是否在不更改代码的情况下通过配置支持NLog中Logger的链接?

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

here is the configuration of my nlog: 这是我的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. 为了登录到应用程序 ,但当出现异常时, 应用程序应将其传递给appErrors ,该错误实际上可以记录完整的异常。

so in the end app contains only the messages even of exceptions. 因此最终应用程序仅包含异常消息。 while appError will contain the detail of all the exceptions that occurred. 而appError将包含发生的所有异常的详细信息。 Is it possible using some configuration in nlog? 是否可以在nlog中使用某些配置?

It looks like you already got that. 看来您已经知道了。 I don't get what the appErrors should do, but you could do like this: 我不知道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>

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 因此,来自LogLevel> = Error的应用程序的所有日志都将同时存储在logs\\quantum_${shortdate}.log并且(仅错误日志)将同时包含在logs\\exceptions_${shortdate}.log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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