1

I am using Nlog's FallbackGroup wrapper to setup failover logging. In case my first target - "Network" logger fails/errors out, it should log to second target/fallback target "EventLog". But while I am running my console app, I could see that it is only trying to log to the first one and always fails. It is not even trying to log to "EventLog". FYI, the EventLog target works fine in isolation(with out fallback setup).

I have got above mentioned details, by enabling the "InternalLogging" on NLog, which is basically writing all the NLog execution to a file.

Below is my NLog config:

<configuration>
<configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">
    <targets>
        <!-- For logging over network with json layout -->
        <target xsi:type="FallbackGroup" name="XXXFallBackLoggingGroup" returnToFirstOnSuccess="true">
            <target name="network" xsi:type="Network" address="https://XXXX/api/log">
                <layout xsi:type="JsonLayout">
                    <attribute name="time" layout="${longdate:universalTime=true}" />
                    <attribute name="level" layout="${level:upperCase=true}"/>
                    <attribute name="message" layout="${message}" />
                </layout>
            </target>
            <target xsi:type="EventLog" name="eventlog" source="xxxx" layout="${level:upperCase=true}${newline}${longdate:universalTime=true}${newline}${message}${newline}"/>
        </target>
    </targets>
    <rules>
        <logger name="*" minlevel="Trace" writeTo="XXXFallBackLoggingGroup" />
    </rules>
</nlog>

This is driving me crazy. I have given many attempts to fix it, but nothing works. Any ideas/suggestions or solutions, please let me know.

Edit: I tried to update the names of targets but this too does not work, however as I mentioned earlier both are working in isolation. Another point, in order to test fallback logging, I have removed my API - the first target, so that it always throws 404 error, but still the NLog is trying to pick the first target always

Julian
  • 33,915
  • 22
  • 119
  • 174
Sujith
  • 1,604
  • 9
  • 16
  • Tried your posted config (with the invalid url), but modified EventLog to Console, and it works just fine when using NLog 4.4.10. Guess you should study the internal-log a little more. – Rolf Kristensen Jun 16 '17 at 17:42
  • Not sure.. what's wrong in the code I used.. I have gone through the internal logs many times, could not find anything there. However il go through once again to see, if I have missed anything. But the NLog is just trying to post to the API, though is fails with 404 every time – Sujith Jun 17 '17 at 00:08
  • 1
    Think that is your problem. Believe Network-target only fails when there is a network-error, but when getting a HTTP-reply back it is a success (Even if the reply-payload includes HTTP-error 404). Maybe WebService-target is more your thing, if you have a WebServer as endpoint. – Rolf Kristensen Jun 17 '17 at 07:15

2 Answers2

1

I had exactly the same issue. I notice that when throwExceptions attribute is activated. It's throw the exception (that it cannot log on sql server database or an other target). And next it terminate the process.

Remove this attribute and your failover configuration should work.

1

https://github.com/NLog/NLog/blob/v4.4.12/src/NLog/Internal/ExceptionHelper.cs#L100

This is the line responsible for this behavior.

You can change throwExcepions=true to throwConfigExceptions=true if you still want to be aware of configuration issues.

pmarek
  • 45
  • 7