Beispiel #1
0
        private static void SplitGroupTarget_Exercise(Target[] targets, int logEventFailCount = 0, Func <Target, MyTarget> lookupTarget = null)
        {
            // Arrange
            var wrapper = new SplitGroupTarget(targets);

            foreach (var target in targets)
            {
                target.Initialize(null);
            }
            wrapper.Initialize(null);
            List <Exception> exceptions = new List <Exception>();
            var flushComplete           = new ManualResetEvent(false);

            lookupTarget = lookupTarget ?? new Func <Target, MyTarget>(t => (MyTarget)t);
            const int LogEventBatchSize  = 2;
            const int LogEventWriteCount = LogEventBatchSize + 2;

            // Act
            wrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Info, "", null, 1).WithContinuation(exceptions.Add));
            wrapper.WriteAsyncLogEvents(new[] { LogEventInfo.Create(LogLevel.Info, "", null, 2).WithContinuation(exceptions.Add) });
            wrapper.WriteAsyncLogEvents(Enumerable.Range(3, LogEventBatchSize).Select(i => LogEventInfo.Create(LogLevel.Info, "", null, i).WithContinuation(exceptions.Add)).ToArray());
            wrapper.Flush(ex => { exceptions.Add(ex); flushComplete.Set(); });
            flushComplete.WaitOne(5000);

            // Assert
            Assert.Equal(LogEventWriteCount + 1, exceptions.Count); // +1 because of flush
            Assert.Equal(LogEventWriteCount + 1 - logEventFailCount, exceptions.Count(ex => ex == null));
            foreach (var target in targets)
            {
                var myTarget = lookupTarget(target);
                Assert.Equal(LogEventWriteCount, myTarget.WrittenEvents.Count);
                Assert.Equal(1, myTarget.FlushCount);
                Assert.Equal(myTarget.WrittenEvents, myTarget.WrittenEvents.OrderBy(l => l.FormattedMessage).ToList());
            }
        }
Beispiel #2
0
        public void SplitGroupSyncTest2()
        {
            var wrapper = new SplitGroupTarget()
            {
                // no targets
            };

            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 = new Exception("Flush not hit synchronously.");

            wrapper.Flush(ex => flushException = ex);

            if (flushException != null)
            {
                Assert.True(false, flushException.ToString());
            }
        }
Beispiel #3
0
        public void SplitGroupToStringTest()
        {
            var myTarget1 = new MyTarget();
            var myTarget2 = new FileTarget("file1");
            var myTarget3 = new ConsoleTarget("Console2");

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

            Assert.Equal("SplitGroup[MyTarget, FileTarget(Name=file1), ConsoleTarget(Name=Console2)]", wrapper.ToString());
        }
Beispiel #4
0
        public void SplitGroupToStringTest()
        {
            var myTarget1 = new MyTarget();
            var myTarget2 = new FileTarget("file1");
            var myTarget3 = new ConsoleTarget("Console2");

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

            Assert.Equal("SplitGroup Target[(unnamed)](MyTarget, File Target[file1], Console Target[Console2])", wrapper.ToString());
        }
Beispiel #5
0
        void Awake()
        {
            LoggingConfiguration config = new LoggingConfiguration();

            List <TargetWithLayout> targets = new List <TargetWithLayout>();

            for (int i = 0; i < logTargets.Length; i++)
            {
                Type t = Type.GetType(logTargets[i]);
                if (t == null)
                {
                    Debug.LogWarning("Could not get logger target of type " + logTargets[i]);
                    continue;
                }
                TargetWithLayout target = System.Activator.CreateInstance(t) as TargetWithLayout;
                if (target == null)
                {
                    Debug.LogError("Couldn't instantiate logger: " + logTargets[i]);
                    continue;
                }
                target.Layout = new SimpleLayout("[${logger}] ${message}");
                targets.Add(target);
            }
            SplitGroupTarget splitTarget = new SplitGroupTarget(targets.ToArray());


            config.AddTarget("Split Target", splitTarget);
            LogManager.Configuration = config;

            for (int i = 0; i < logSources.Length; i++)
            {
                string   logName  = logSources[i].name;
                LogLevel logLevel = ToLogLevel(logSources[i].logLevel);
                AddRule(config, logName, logLevel, splitTarget);
            }

            LogManager.Configuration = config;
        }
Beispiel #6
0
        public void SplitGroupSyncTest1()
        {
            var myTarget1 = new MyTarget();
            var myTarget2 = new MyTarget();
            var myTarget3 = new MyTarget();

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

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

            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.WriteAsyncLogEvent(inputEvents[i].WithContinuation(ex =>
                {
                    lock (exceptions)
                    {
                        exceptions.Add(ex);
                        if (Interlocked.Decrement(ref remaining) == 0)
                        {
                            allDone.Set();
                        }
                    };
                }));
            }

            allDone.WaitOne();

            Assert.Equal(inputEvents.Count, exceptions.Count);
            foreach (var e in exceptions)
            {
                Assert.Null(e);
            }

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

            for (int i = 0; i < inputEvents.Count; ++i)
            {
                Assert.Same(inputEvents[i], myTarget1.WrittenEvents[i]);
                Assert.Same(inputEvents[i], myTarget2.WrittenEvents[i]);
                Assert.Same(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.True(false, flushException.ToString());
            }

            Assert.Equal(1, myTarget1.FlushCount);
            Assert.Equal(1, myTarget2.FlushCount);
            Assert.Equal(1, myTarget3.FlushCount);
        }