public void RoundRobinGroupTargetSyncTest2()
        {
            var wrapper = new RoundRobinGroupTarget()
            {
                // empty target list
            };

            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.Equal(10, exceptions.Count);
            foreach (var e in exceptions)
            {
                Assert.Null(e);
            }

            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());
            }
        }
        public void RoundRobinGroupTargetSyncTest1()
        {
            var myTarget1 = new MyTarget();
            var myTarget2 = new MyTarget();
            var myTarget3 = new MyTarget();

            var wrapper = new RoundRobinGroupTarget()
            {
                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.Equal(10, exceptions.Count);
            foreach (var e in exceptions)
            {
                Assert.Null(e);
            }

            Assert.Equal(4, myTarget1.WriteCount);
            Assert.Equal(3, myTarget2.WriteCount);
            Assert.Equal(3, myTarget3.WriteCount);

            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());
            }

            Assert.Equal(1, myTarget1.FlushCount);
            Assert.Equal(1, myTarget2.FlushCount);
            Assert.Equal(1, myTarget3.FlushCount);
        }
Exemplo n.º 3
0
        public void CompoundRefTest()
        {
            LoggingConfiguration c = CreateConfigurationFromString(@"
            <nlog>
                <targets>
                    <target name='d1' type='Debug' layout='${message}1' />
                    <target name='d2' type='Debug' layout='${message}2' />
                    <target name='d3' type='Debug' layout='${message}3' />
                    <target name='d4' type='Debug' layout='${message}4' />

                    <compound-target name='rr' type='RoundRobinGroup'>
                        <target-ref name='d1' />
                        <target-ref name='d2' />
                        <target-ref name='d3' />
                        <target-ref name='d4' />
                    </compound-target>
                </targets>
            </nlog>");

            Assert.IsNotNull(c.FindTargetByName("rr"));
            Assert.IsNotNull(c.FindTargetByName("d1"));
            Assert.IsNotNull(c.FindTargetByName("d2"));
            Assert.IsNotNull(c.FindTargetByName("d3"));
            Assert.IsNotNull(c.FindTargetByName("d4"));

            Assert.IsInstanceOfType(typeof(RoundRobinGroupTarget), c.FindTargetByName("rr"));
            Assert.IsInstanceOfType(typeof(DebugTarget), c.FindTargetByName("d1"));
            Assert.IsInstanceOfType(typeof(DebugTarget), c.FindTargetByName("d2"));
            Assert.IsInstanceOfType(typeof(DebugTarget), c.FindTargetByName("d3"));
            Assert.IsInstanceOfType(typeof(DebugTarget), c.FindTargetByName("d4"));

            RoundRobinGroupTarget rr = c.FindTargetByName("rr") as RoundRobinGroupTarget;
            DebugTarget           d1 = c.FindTargetByName("d1") as DebugTarget;
            DebugTarget           d2 = c.FindTargetByName("d2") as DebugTarget;
            DebugTarget           d3 = c.FindTargetByName("d3") as DebugTarget;
            DebugTarget           d4 = c.FindTargetByName("d4") as DebugTarget;

            Assert.AreEqual(4, rr.Targets.Count);
            Assert.AreSame(d1, rr.Targets[0]);
            Assert.AreSame(d2, rr.Targets[1]);
            Assert.AreSame(d3, rr.Targets[2]);
            Assert.AreSame(d4, rr.Targets[3]);

            Assert.AreEqual(((SimpleLayout)d1.Layout).Text, "${message}1");
            Assert.AreEqual(((SimpleLayout)d2.Layout).Text, "${message}2");
            Assert.AreEqual(((SimpleLayout)d3.Layout).Text, "${message}3");
            Assert.AreEqual(((SimpleLayout)d4.Layout).Text, "${message}4");
        }
        public void CompoundTest()
        {
            LoggingConfiguration c = XmlLoggingConfiguration.CreateFromXmlString(@"
            <nlog>
                <targets>
                    <compound-target name='rr' type='RoundRobinGroup'>
                        <target name='d1' type='Debug' layout='${message}1' />
                        <target name='d2' type='Debug' layout='${message}2' />
                        <target name='d3' type='Debug' layout='${message}3' />
                        <target name='d4' type='Debug' layout='${message}4' />
                    </compound-target>
                </targets>
            </nlog>");

            Assert.NotNull(c.FindTargetByName("rr"));
            Assert.NotNull(c.FindTargetByName("d1"));
            Assert.NotNull(c.FindTargetByName("d2"));
            Assert.NotNull(c.FindTargetByName("d3"));
            Assert.NotNull(c.FindTargetByName("d4"));

            Assert.IsType <RoundRobinGroupTarget>(c.FindTargetByName("rr"));
            Assert.IsType <DebugTarget>(c.FindTargetByName("d1"));
            Assert.IsType <DebugTarget>(c.FindTargetByName("d2"));
            Assert.IsType <DebugTarget>(c.FindTargetByName("d3"));
            Assert.IsType <DebugTarget>(c.FindTargetByName("d4"));

            RoundRobinGroupTarget rr = c.FindTargetByName("rr") as RoundRobinGroupTarget;
            DebugTarget           d1 = c.FindTargetByName("d1") as DebugTarget;
            DebugTarget           d2 = c.FindTargetByName("d2") as DebugTarget;
            DebugTarget           d3 = c.FindTargetByName("d3") as DebugTarget;
            DebugTarget           d4 = c.FindTargetByName("d4") as DebugTarget;

            Assert.Equal(4, rr.Targets.Count);
            Assert.Same(d1, rr.Targets[0]);
            Assert.Same(d2, rr.Targets[1]);
            Assert.Same(d3, rr.Targets[2]);
            Assert.Same(d4, rr.Targets[3]);

            Assert.Equal("${message}1", ((SimpleLayout)d1.Layout).Text);
            Assert.Equal("${message}2", ((SimpleLayout)d2.Layout).Text);
            Assert.Equal("${message}3", ((SimpleLayout)d3.Layout).Text);
            Assert.Equal("${message}4", ((SimpleLayout)d4.Layout).Text);
        }