예제 #1
0
파일: Example.cs 프로젝트: tdhieu/openvss
    static void Main(string[] args)
    {
        FileTarget wrappedTarget = new FileTarget();
        wrappedTarget.FileName = "${basedir}/file.txt";

        RepeatingTargetWrapper target = new RepeatingTargetWrapper();
        target.WrappedTarget = wrappedTarget;
        target.RepeatCount = 3;

        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);

        Logger logger = LogManager.GetLogger("Example");
        logger.Debug("log message");
    }
예제 #2
0
        public void RepeatingTargetWrapperTest1()
        {
            var target = new MyTarget();
            var wrapper = new RepeatingTargetWrapper()
            {
                WrappedTarget = target,
                RepeatCount = 3,
            };
            ((ISupportsInitialize)wrapper).Initialize();
            ((ISupportsInitialize)target).Initialize();

            var events = new LogEventInfo[]
            {
                new LogEventInfo(LogLevel.Debug, "Logger1", "Hello"),
                new LogEventInfo(LogLevel.Info, "Logger1", "Hello"),
                new LogEventInfo(LogLevel.Info, "Logger2", "Hello"),
            };

            var exceptions = new List<Exception>();

            var continuations = new AsyncContinuation[events.Length];
            for (int i = 0; i < continuations.Length; ++i)
            {
                continuations[i] = exceptions.Add;
            }

            wrapper.WriteLogEvents(events, continuations);

            // make sure all events went through and were replicated 3 times
            Assert.AreEqual(9, target.Events.Count);
            Assert.AreSame(events[0], target.Events[0]);
            Assert.AreSame(events[0], target.Events[1]);
            Assert.AreSame(events[0], target.Events[2]);
            Assert.AreSame(events[1], target.Events[3]);
            Assert.AreSame(events[1], target.Events[4]);
            Assert.AreSame(events[1], target.Events[5]);
            Assert.AreSame(events[2], target.Events[6]);
            Assert.AreSame(events[2], target.Events[7]);
            Assert.AreSame(events[2], target.Events[8]);

            Assert.AreEqual(continuations.Length, exceptions.Count, "Some continuations were not invoked.");
        }
        public void RepeatingTargetWrapperTest1()
        {
            var target = new MyTarget();
            var wrapper = new RepeatingTargetWrapper()
            {
                WrappedTarget = target,
                RepeatCount = 3,
            };
            wrapper.Initialize(null);
            target.Initialize(null);

            var exceptions = new List<Exception>();

            var events = new[]
            {
                new LogEventInfo(LogLevel.Debug, "Logger1", "Hello").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Info, "Logger1", "Hello").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Info, "Logger2", "Hello").WithContinuation(exceptions.Add),
            };

            wrapper.WriteAsyncLogEvents(events);

            // make sure all events went through and were replicated 3 times
            Assert.Equal(9, target.Events.Count);
            Assert.Same(events[0].LogEvent, target.Events[0]);
            Assert.Same(events[0].LogEvent, target.Events[1]);
            Assert.Same(events[0].LogEvent, target.Events[2]);
            Assert.Same(events[1].LogEvent, target.Events[3]);
            Assert.Same(events[1].LogEvent, target.Events[4]);
            Assert.Same(events[1].LogEvent, target.Events[5]);
            Assert.Same(events[2].LogEvent, target.Events[6]);
            Assert.Same(events[2].LogEvent, target.Events[7]);
            Assert.Same(events[2].LogEvent, target.Events[8]);

            Assert.Equal(events.Length, exceptions.Count);
        }
        public void RepeatingTargetWrapperTest2()
        {
            var target = new MyTarget();
            target.ThrowExceptions = true;
            var wrapper = new RepeatingTargetWrapper()
            {
                WrappedTarget = target,
                RepeatCount = 3,
            };
            wrapper.Initialize(null);
            target.Initialize(null);

            var exceptions = new List<Exception>();

            var events = new []
            {
                new LogEventInfo(LogLevel.Debug, "Logger1", "Hello").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Info, "Logger1", "Hello").WithContinuation(exceptions.Add),
                new LogEventInfo(LogLevel.Info, "Logger2", "Hello").WithContinuation(exceptions.Add),
            };

            wrapper.WriteAsyncLogEvents(events);

            // make sure all events went through but were registered only once
            // since repeating target wrapper will not repeat in case of exception.
            Assert.Equal(3, target.Events.Count);
            Assert.Same(events[0].LogEvent, target.Events[0]);
            Assert.Same(events[1].LogEvent, target.Events[1]);
            Assert.Same(events[2].LogEvent, target.Events[2]);

            Assert.Equal(events.Length, exceptions.Count);
            foreach (var exception in exceptions)
            {
                Assert.NotNull(exception);
                Assert.Equal("Some exception has occurred.", exception.Message);
            }
        }
예제 #5
0
        public void RepeatingTargetWrapperTest2()
        {
            var target = new MyTarget();
            target.ThrowExceptions = true;
            var wrapper = new RepeatingTargetWrapper()
            {
                WrappedTarget = target,
                RepeatCount = 3,
            };
            ((ISupportsInitialize)wrapper).Initialize();
            ((ISupportsInitialize)target).Initialize();

            var events = new LogEventInfo[]
            {
                new LogEventInfo(LogLevel.Debug, "Logger1", "Hello"),
                new LogEventInfo(LogLevel.Info, "Logger1", "Hello"),
                new LogEventInfo(LogLevel.Info, "Logger2", "Hello"),
            };

            var exceptions = new List<Exception>();

            var continuations = new AsyncContinuation[events.Length];
            for (int i = 0; i < continuations.Length; ++i)
            {
                continuations[i] = exceptions.Add;
            }

            wrapper.WriteLogEvents(events, continuations);

            // make sure all events went through but were registered only once
            // since repeating target wrapper will not repeat in case of exception.
            Assert.AreEqual(3, target.Events.Count);
            Assert.AreSame(events[0], target.Events[0]);
            Assert.AreSame(events[1], target.Events[1]);
            Assert.AreSame(events[2], target.Events[2]);

            Assert.AreEqual(continuations.Length, exceptions.Count, "Some continuations were not invoked.");
            foreach (var exception in exceptions)
            {
                Assert.IsNotNull(exception);
                Assert.AreEqual("Some exception has ocurred.", exception.Message);
            }
        }