Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }