Is there a fail over mechanism with NLog?

被刻印的时光 ゝ 提交于 2019-12-11 11:47:05

问题


I am using NLog for DB logging. If the DB is down then I only need it to log to a file. Is there a way to do this?

I don't want to record log entries log in both a file and the DB. If the DB target fails to log (if DB is down) only then do I have to log to the file. Is there any fail over or fallback feature with Nlog?


回答1:


Yes, there is a way. It's called a FallbackGroup.

Documentation can be found here: https://github.com/nlog/NLog/wiki/FallbackGroup-target

Here is an example taken blatantly and ashamedly from the documentation:

<target xsi:type="FallbackGroup" 
        name="mail"
        returnToFirstOnSuccess="true">
    <target xsi:type="Mail"
            name="mailserver1"
            subject="Layout"
            to="Layout"
            from="Layout"
            smtpServer="mx1.example.com" 
            smtpPort="Integer"
            layout="Layout" />
    <target xsi:type="Mail"
            name="mailserver2" 
            subject="Layout"
            to="Layout"
            from="Layout"
            smtpServer="mx2.example.com" 
            smtpPort="Integer"
            layout="Layout" />
</target>
<rules>
  <logger name="*" minlevel="Trace" writeTo="mail" />
</rules>

It'll try another mail server if the first fails. It'll also return to the first target if it's successful.

I never used it in production, maybe the other options in the documentation can help you fine tune it to your problem.



来源:https://stackoverflow.com/questions/49788880/is-there-a-fail-over-mechanism-with-nlog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!