コード例 #1
0
ファイル: SplitGroupTargetTests.cs プロジェクト: igalse/NLog
        public void SplitGroupSyncTest1()
        {
            var myTarget1 = new MyTarget();
            var myTarget2 = new MyTarget();
            var myTarget3 = new MyTarget();

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

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

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

            var inputEvents = new List<LogEventInfo>();
            for (int i = 0; i < 10; ++i)
            {
                inputEvents.Add(LogEventInfo.CreateNullEvent());
            }

            int remaining = inputEvents.Count;
            var allDone = new ManualResetEvent(false);

            // no exceptions
            for (int i = 0; i < inputEvents.Count; ++i)
            {
                wrapper.WriteLogEvent(inputEvents[i], ex =>
                    {
                        lock (exceptions)
                        {
                            exceptions.Add(ex);
                            if (Interlocked.Decrement(ref remaining) == 0)
                            {
                                allDone.Set();
                            }
                        };
                    });
            }

            allDone.WaitOne();

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

            Assert.AreEqual(inputEvents.Count, myTarget1.WriteCount);
            Assert.AreEqual(inputEvents.Count, myTarget2.WriteCount);
            Assert.AreEqual(inputEvents.Count, myTarget3.WriteCount);

            for (int i = 0; i < inputEvents.Count; ++i)
            {
                Assert.AreSame(inputEvents[i], myTarget1.WrittenEvents[i]);
                Assert.AreSame(inputEvents[i], myTarget2.WrittenEvents[i]);
                Assert.AreSame(inputEvents[i], myTarget3.WrittenEvents[i]);
            }

            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);
        }
コード例 #2
0
ファイル: SplitGroupTargetTests.cs プロジェクト: igalse/NLog
        public void SplitGroupSyncTest2()
        {
            var wrapper = new SplitGroupTarget()
            {
                // no targets
            };

            ((ISupportsInitialize)wrapper).Initialize();

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

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

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

            Exception flushException = new Exception("Flush not hit synchronously.");
            wrapper.Flush(ex => flushException = ex);

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