Inheritance: NLog.Targets.Wrappers.WrapperTargetBase
Example #1
0
        public void InitializeThrowsNLogConfigurationExceptionIfIntervalIsZero()
        {
            MyTarget wrappedTarget = new MyTarget();
            LimitingTargetWrapper wrapper = new LimitingTargetWrapper(wrappedTarget) {Interval = TimeSpan.Zero};
            wrappedTarget.Initialize(null);
            LogManager.ThrowConfigExceptions = true;

            Assert.Throws<NLogConfigurationException>(() => wrapper.Initialize(null));
            LogManager.ThrowConfigExceptions = false;
        }
Example #2
0
        public void ConstructorWithNameInitialisesDefaultsCorrectly()
        {
            MyTarget wrappedTarget = new MyTarget();
            LimitingTargetWrapper wrapper = new LimitingTargetWrapper("Wrapper", wrappedTarget);

            Assert.Equal(1000, wrapper.MessageLimit);
            Assert.Equal(TimeSpan.FromHours(1), wrapper.Interval);
        }
Example #3
0
        public void InitializeThrowsNLogConfigurationExceptionIfMessageLimitIsSmallerZero()
        {
            MyTarget wrappedTarget = new MyTarget();
            LimitingTargetWrapper wrapper = new LimitingTargetWrapper(wrappedTarget) {MessageLimit = -1};
            wrappedTarget.Initialize(null);
            LogManager.ThrowConfigExceptions = true;

            Assert.Throws<NLogConfigurationException>(() => wrapper.Initialize(null));
            LogManager.ThrowConfigExceptions = false;
        }
Example #4
0
        public void ConstructorWithNoParametersInitialisesDefaultsCorrectly()
        {
            LimitingTargetWrapper wrapper = new LimitingTargetWrapper();

            Assert.Equal(1000, wrapper.MessageLimit);
            Assert.Equal(TimeSpan.FromHours(1), wrapper.Interval);
        }
Example #5
0
        public void TestWritingMessagesOverMultipleIntervals()
        {
            MyTarget wrappedTarget = new MyTarget();
            LimitingTargetWrapper wrapper = new LimitingTargetWrapper(wrappedTarget, 5, TimeSpan.FromSeconds(1));
            InitializeTargets(wrappedTarget, wrapper);
            Exception lastException = null;

            lastException = WriteNumberAsyncLogEventsStartingAt(0, 10, wrapper);
            
            //Let the interval expire.
            Thread.Sleep(1000);

            Assert.Equal(5, wrappedTarget.WriteCount);
            Assert.Equal("Hello 4", wrappedTarget.LastWrittenMessage);
            Assert.Null(lastException);

            lastException = WriteNumberAsyncLogEventsStartingAt(10, 10, wrapper);

            //We should have 10 messages (5 from first, 5 from second interval).
            Assert.Equal(10, wrappedTarget.WriteCount);
            Assert.Equal("Hello 14", wrappedTarget.LastWrittenMessage);
            Assert.Null(lastException);

            //Let the interval expire.
            Thread.Sleep(2300);

            lastException = WriteNumberAsyncLogEventsStartingAt(20, 10, wrapper);

            //We should have 15 messages (5 from first, 5 from second, 5 from third interval).
            Assert.Equal(15, wrappedTarget.WriteCount);
            Assert.Equal("Hello 24", wrappedTarget.LastWrittenMessage);
            Assert.Null(lastException);

            //Let the interval expire.
            Thread.Sleep(200);
            lastException = WriteNumberAsyncLogEventsStartingAt(30, 10, wrapper);

            //No more messages shouldve been written, since we are still in the third interval.
            Assert.Equal(15, wrappedTarget.WriteCount);
            Assert.Equal("Hello 24", wrappedTarget.LastWrittenMessage);
            Assert.Null(lastException);
        }
Example #6
0
        public void WriteMessageAfterIntervalHasExpiredStartsNewInterval()
        {
            MyTarget wrappedTarget = new MyTarget();
            LimitingTargetWrapper wrapper = new LimitingTargetWrapper(wrappedTarget, 5, TimeSpan.FromSeconds(1));
            InitializeTargets(wrappedTarget, wrapper);
            Exception lastException = null;
            wrapper.WriteAsyncLogEvent(
                new LogEventInfo(LogLevel.Debug, "test", "first interval").WithContinuation(ex => lastException = ex));

            //Let the interval expire.
            Thread.Sleep(1000);

            //Writing a logEvent should start a new Interval. This should be written to InternalLogger.Debug.
            string internalLog = RunAndCaptureInternalLog(() =>
            {
                // We can write 5 messages again since a new interval started.
                lastException = WriteNumberAsyncLogEventsStartingAt(0, 5, wrapper);

            }, LogLevel.Trace);

            //We should have written 6 messages (1 in first interval and 5 in second interval).
            Assert.Equal(6, wrappedTarget.WriteCount);
            Assert.True(internalLog.Contains("new interval"));
            Assert.Null(lastException);
        }
Example #7
0
        public void WriteMoreMessagesThanMessageLimitDiscardsExcessMessages()
        {
            MyTarget wrappedTarget = new MyTarget();
            LimitingTargetWrapper wrapper = new LimitingTargetWrapper(wrappedTarget, 5, TimeSpan.FromHours(1));
            InitializeTargets(wrappedTarget, wrapper);
            Exception lastException = null;

            // Write limit number of messages should just write them to the wrappedTarget.
            lastException = WriteNumberAsyncLogEventsStartingAt(0, 5, wrapper);

            Assert.Equal(5, wrappedTarget.WriteCount);

            //Additional messages will be discarded, but InternalLogger will write to trace.
            string internalLog = RunAndCaptureInternalLog(() =>
            {
                wrapper.WriteAsyncLogEvent(
                    new LogEventInfo(LogLevel.Debug, "test", $"Hello {5}").WithContinuation(ex => lastException = ex));
            }, LogLevel.Trace);

            Assert.Equal(5, wrappedTarget.WriteCount);
            Assert.True(internalLog.Contains("MessageLimit"));
            Assert.Null(lastException);
        }
Example #8
0
        public void WriteMessagesLessThanMessageLimitWritesToWrappedTarget()
        {
            MyTarget wrappedTarget = new MyTarget();
            LimitingTargetWrapper wrapper = new LimitingTargetWrapper(wrappedTarget, 5, TimeSpan.FromSeconds(1));
            InitializeTargets(wrappedTarget, wrapper);

            Exception lastException = null;
            // Write limit number of messages should just write them to the wrappedTarget.
            lastException = WriteNumberAsyncLogEventsStartingAt(0, 5, wrapper);

            Assert.Equal(5, wrappedTarget.WriteCount);

            //Let the interval expire to start a new one.
            Thread.Sleep(1000);

            // Write limit number of messages should just write them to the wrappedTarget.
            lastException = WriteNumberAsyncLogEventsStartingAt(5, 5, wrapper);

            // We should have 10 messages (5 from first interval, 5 from second interval).
            Assert.Equal(10, wrappedTarget.WriteCount);
            Assert.Null(lastException);
        }