Ejemplo n.º 1
0
        public void FallbackGroupTargetSyncTest1()
        {
            var myTarget1 = new MyTarget();
            var myTarget2 = new MyTarget();
            var myTarget3 = new MyTarget();

            var wrapper = new FallbackGroupTarget()
            {
                Targets = { myTarget1, myTarget2, myTarget3 },
            };

            myTarget1.Initialize(null);
            myTarget2.Initialize(null);
            myTarget3.Initialize(null);
            wrapper.Initialize(null);

            List<Exception> exceptions = new List<Exception>();

            // no exceptions
            for (int i = 0; i < 10; ++i)
            {
                wrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
            }

            Assert.AreEqual(10, exceptions.Count);
            foreach (var e in exceptions)
            {
                Assert.IsNull(e);
            }

            Assert.AreEqual(10, myTarget1.WriteCount);
            Assert.AreEqual(0, myTarget2.WriteCount);
            Assert.AreEqual(0, myTarget3.WriteCount);

            Exception flushException = null;
            var flushHit = new ManualResetEvent(false);
            wrapper.Flush(ex => { flushException = ex; flushHit.Set(); });

            flushHit.WaitOne();
            if (flushException != null)
            {
                Assert.Fail(flushException.ToString());
            }
        }
Ejemplo n.º 2
0
        public void BufferingTargetWithFallbackGroupAndFirstTargetFails_Write_SecondTargetWritesEvents()
        {
            var myTarget = new MyTarget { FailCounter = 1 };
            var myTarget2 = new MyTarget();
            var fallbackGroup = new FallbackGroupTarget(myTarget, myTarget2);
            var targetWrapper = new BufferingTargetWrapper
            {
                WrappedTarget = fallbackGroup,
                BufferSize = 10,
            };

            InitializeTargets(myTarget, targetWrapper, myTarget2, fallbackGroup);

            const int totalEvents = 100;

            var continuationHit = new bool[totalEvents];
            var lastException = new Exception[totalEvents];
            var continuationThread = new Thread[totalEvents];

            CreateContinuationFunc createAsyncContinuation =
                eventNumber =>
                    ex =>
                    {
                        lastException[eventNumber] = ex;
                        continuationThread[eventNumber] = Thread.CurrentThread;
                        continuationHit[eventNumber] = true;
                    };

            // write 9 events - they will all be buffered and no final continuation will be reached
            var eventCounter = 0;
            for (var i = 0; i < 9; ++i)
            {
                targetWrapper.WriteAsyncLogEvent(new LogEventInfo().WithContinuation(createAsyncContinuation(eventCounter++)));
            }

            Assert.Equal(0, myTarget.WriteCount);

            // write one more event - everything will be flushed
            targetWrapper.WriteAsyncLogEvent(new LogEventInfo().WithContinuation(createAsyncContinuation(eventCounter++)));
            Assert.Equal(1, myTarget.WriteCount);
            Assert.Equal(10, myTarget2.WriteCount);

            targetWrapper.Close();
            myTarget.Close();
        }
Ejemplo n.º 3
0
        public void FallbackGroupTargetSyncTest4()
        {
            // fail once
            var myTarget1 = new MyTarget() { FailCounter = 1 };
            var myTarget2 = new MyTarget();
            var myTarget3 = new MyTarget();

            var wrapper = new FallbackGroupTarget()
            {
                Targets = { myTarget1, myTarget2, myTarget3 },
                ReturnToFirstOnSuccess = true,
            };

            myTarget1.Initialize(null);
            myTarget2.Initialize(null);
            myTarget3.Initialize(null);
            wrapper.Initialize(null);

            List<Exception> exceptions = new List<Exception>();

            // no exceptions
            for (int i = 0; i < 10; ++i)
            {
                wrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
            }

            // sequence is like this:
            // t1(fail), t2(success), t1(success), ... t1(success)
            Assert.AreEqual(10, exceptions.Count);
            foreach (var e in exceptions)
            {
                Assert.IsNull(e);
            }

            Assert.AreEqual(10, myTarget1.WriteCount);
            Assert.AreEqual(1, myTarget2.WriteCount);
            Assert.AreEqual(0, myTarget3.WriteCount);

            Exception flushException = null;
            var flushHit = new ManualResetEvent(false);
            wrapper.Flush(ex => { flushException = ex; flushHit.Set(); });

            flushHit.WaitOne();
            if (flushException != null)
            {
                Assert.Fail(flushException.ToString());
            }
        }
Ejemplo n.º 4
0
        public void FallbackGroupTargetSyncTest6()
        {
            // fail once
            var myTarget1 = new MyTarget() { FailCounter = 10 };
            var myTarget2 = new MyTarget() { FailCounter = 3 };
            var myTarget3 = new MyTarget() { FailCounter = 3 };

            var wrapper = new FallbackGroupTarget()
            {
                Targets = { myTarget1, myTarget2, myTarget3 },
                ReturnToFirstOnSuccess = true,
            };

            myTarget1.Initialize(null);
            myTarget2.Initialize(null);
            myTarget3.Initialize(null);
            wrapper.Initialize(null);

            List<Exception> exceptions = new List<Exception>();

            // no exceptions
            for (int i = 0; i < 10; ++i)
            {
                wrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
            }

            Assert.AreEqual(10, exceptions.Count);
            for (int i = 0; i < 10; ++i)
            {
                if (i < 3)
                {
                    // for the first 3 rounds, no target is available
                    Assert.IsNotNull(exceptions[i]);
                    Assert.IsInstanceOfType(exceptions[i], typeof(InvalidOperationException));
                    Assert.AreEqual("Some failure.", exceptions[i].Message);
                }
                else
                {
                    Assert.IsNull(exceptions[i], Convert.ToString(exceptions[i]));
                }
            }

            Assert.AreEqual(10, myTarget1.WriteCount);
            Assert.AreEqual(10, myTarget2.WriteCount);
            Assert.AreEqual(3, myTarget3.WriteCount);

            Exception flushException = null;
            var flushHit = new ManualResetEvent(false);
            wrapper.Flush(ex => { flushException = ex; flushHit.Set(); });

            flushHit.WaitOne();
            if (flushException != null)
            {
                Assert.Fail(flushException.ToString());
            }

            Assert.AreEqual(1, myTarget1.FlushCount);
            Assert.AreEqual(1, myTarget2.FlushCount);
            Assert.AreEqual(1, myTarget3.FlushCount);
        }
Ejemplo n.º 5
0
        private static void AssertNoFlushException(FallbackGroupTarget wrapper)
        {
            Exception flushException = null;
            var flushHit = new ManualResetEvent(false);
            wrapper.Flush(ex =>
                              {
                                  flushException = ex;
                                  flushHit.Set();
                              });

            flushHit.WaitOne();
            if (flushException != null)
                Assert.True(false, flushException.ToString());
        }
Ejemplo n.º 6
0
        private static void WriteAndAssertNoExceptions(FallbackGroupTarget wrapper)
        {
            var exceptions = new List<Exception>();
            for (var i = 0; i < 10; ++i)
            {
                wrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
            }

            Assert.Equal(10, exceptions.Count);
            foreach (var e in exceptions)
            {
                Assert.Null(e);
            }
        }
Ejemplo n.º 7
0
        private static FallbackGroupTarget CreateAndInitializeFallbackGroupTarget(bool returnToFirstOnSuccess, params Target[] targets)
        {
            var wrapper = new FallbackGroupTarget(targets)
                              {
                                  ReturnToFirstOnSuccess = returnToFirstOnSuccess,
                              };

            foreach (var target in targets)
            {
                target.Initialize(null);
            }

            wrapper.Initialize(null);

            return wrapper;
        }
Ejemplo n.º 8
0
        private void ConfigureDefaultTarget()
        {
            const int RetryCount = 2;
            const int RetryIntervalMs = 500;

            // Create database target
            DatabaseTarget eventLogDatabaseTarget = ConfigureDatabaseTarget();

            RetryingTargetWrapper retryTarget = new RetryingTargetWrapper(eventLogDatabaseTarget, RetryCount, RetryIntervalMs);

            // Create file target
            FileTarget fileTarget = ConfigureFileTarget();

            // Create Event Log target
            EventLogTarget applicationEventLogTarget = ConfigureEventLogTarget(base._applicationName);

            // Create fallback group target - database target first, then Event Log target
            FallbackGroupTarget fallbackTarget = new FallbackGroupTarget(retryTarget, fileTarget, applicationEventLogTarget);

            // With ReturnToFirstOnSuccess true, when the primary target fails and the secondary
            // succeeds, NLog will switch back to the primary for the next write, which is what we
            // want. With the default of false, if the primary fails once, NLog stays on the secondary
            // for the life of the AppDomain -- i.e. only Event Log and no more database.
            fallbackTarget.ReturnToFirstOnSuccess = true;

            AsyncTargetWrapper asyncFallbackTarget = new AsyncTargetWrapper(fallbackTarget);
            asyncFallbackTarget.BatchSize = 200;
            asyncFallbackTarget.QueueLimit = 5000;

            LogManager.Configuration.AddTarget("FrameworkLoggingDefault", asyncFallbackTarget);
        }
Ejemplo n.º 9
0
        public void FallbackGroupTargetSyncTest5()
        {
            // fail once
            var myTarget1 = new MyTarget() { FailCounter = 3 };
            var myTarget2 = new MyTarget() { FailCounter = 3 };
            var myTarget3 = new MyTarget() { FailCounter = 3 };

            var wrapper = new FallbackGroupTarget()
            {
                Targets = { myTarget1, myTarget2, myTarget3 },
                ReturnToFirstOnSuccess = true,
            };

            wrapper.Initialize(CommonCfg);

            List<Exception> exceptions = new List<Exception>();

            // no exceptions
            for (int i = 0; i < 10; ++i)
            {
                wrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
            }

            Assert.AreEqual(10, exceptions.Count);
            for (int i = 0; i < 10; ++i)
            {
                if (i < 3)
                {
                    Assert.IsNotNull(exceptions[i]);
                }
                else
                {
                    Assert.IsNull(exceptions[i]);
                }
            }

            Assert.AreEqual(10, myTarget1.WriteCount);
            Assert.AreEqual(3, myTarget2.WriteCount);
            Assert.AreEqual(3, myTarget3.WriteCount);

            Exception flushException = null;
            var flushHit = new ManualResetEvent(false);
            wrapper.Flush(ex => { flushException = ex; flushHit.Set(); });

            flushHit.WaitOne();
            if (flushException != null)
            {
                Assert.Fail(flushException.ToString());
            }
        }