コード例 #1
0
        public void AutoFlushTargetWrapperSyncTest1()
        {
            var myTarget = new MyTarget();
            var wrapper  = new AutoFlushTargetWrapper
            {
                WrappedTarget = myTarget,
            };

            myTarget.Initialize(null);
            wrapper.Initialize(null);
            var               logEvent        = new LogEventInfo();
            Exception         lastException   = null;
            bool              continuationHit = false;
            AsyncContinuation continuation    =
                ex =>
            {
                lastException   = ex;
                continuationHit = true;
            };

            wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));

            Assert.True(continuationHit);
            Assert.Null(lastException);
            Assert.Equal(1, myTarget.FlushCount);
            Assert.Equal(1, myTarget.WriteCount);

            continuationHit = false;
            wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));
            Assert.True(continuationHit);
            Assert.Null(lastException);
            Assert.Equal(2, myTarget.WriteCount);
            Assert.Equal(2, myTarget.FlushCount);
        }
コード例 #2
0
        public void AutoFlushTargetWrapperAsyncTest1()
        {
            var myTarget = new MyAsyncTarget();
            var wrapper  = new AutoFlushTargetWrapper(myTarget);

            myTarget.Initialize(null);
            wrapper.Initialize(null);
            var               logEvent        = new LogEventInfo();
            Exception         lastException   = null;
            var               continuationHit = new ManualResetEvent(false);
            AsyncContinuation continuation    =
                ex =>
            {
                lastException = ex;
                continuationHit.Set();
            };

            wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));

            continuationHit.WaitOne();
            Assert.Null(lastException);
            Assert.Equal(1, myTarget.FlushCount);
            Assert.Equal(1, myTarget.WriteCount);

            continuationHit.Reset();
            wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));
            continuationHit.WaitOne();
            Assert.Null(lastException);
            Assert.Equal(2, myTarget.WriteCount);
            Assert.Equal(2, myTarget.FlushCount);
        }
コード例 #3
0
        public void MultipleConditionalAutoFlushWrappersTest()
        {
            var testTarget = new MyTarget();
            var autoFlushOnLevelWrapper = new AutoFlushTargetWrapper(testTarget);

            autoFlushOnLevelWrapper.Condition = "level > LogLevel.Info";
            var autoFlushOnMessageWrapper = new AutoFlushTargetWrapper(autoFlushOnLevelWrapper);

            autoFlushOnMessageWrapper.Condition = "contains('${message}','FlushThis')";
            testTarget.Initialize(null);
            autoFlushOnLevelWrapper.Initialize(null);
            autoFlushOnMessageWrapper.Initialize(null);

            AsyncContinuation continuation = ex => { };

            autoFlushOnMessageWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Trace, "*", "test").WithContinuation(continuation));
            Assert.Equal(1, testTarget.WriteCount);
            Assert.Equal(0, testTarget.FlushCount);
            autoFlushOnMessageWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Fatal, "*", "test").WithContinuation(continuation));
            Assert.Equal(2, testTarget.WriteCount);
            Assert.Equal(1, testTarget.FlushCount);
            autoFlushOnMessageWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Trace, "*", "Please FlushThis").WithContinuation(continuation));
            Assert.Equal(3, testTarget.WriteCount);
            Assert.Equal(2, testTarget.FlushCount);
        }
コード例 #4
0
        public void AutoFlushTargetWrapperAsyncTest2()
        {
            var myTarget = new MyAsyncTarget();
            var wrapper  = new AutoFlushTargetWrapper(myTarget);

            myTarget.Initialize(null);
            wrapper.Initialize(null);
            var       logEvent      = new LogEventInfo();
            Exception lastException = null;

            for (int i = 0; i < 100; ++i)
            {
                wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(ex => lastException = ex));
            }

            var continuationHit            = new ManualResetEvent(false);
            AsyncContinuation continuation =
                ex =>
            {
                continuationHit.Set();
            };

            wrapper.Flush(ex => { });
            Assert.Null(lastException);
            wrapper.Flush(continuation);
            Assert.Null(lastException);
            continuationHit.WaitOne();
            Assert.Null(lastException);
            wrapper.Flush(ex => { });   // Executed right away
            Assert.Null(lastException);
            Assert.Equal(100, myTarget.WriteCount);
            Assert.Equal(103, myTarget.FlushCount);
        }
コード例 #5
0
        public void IgnoreExplicitAutoFlushWrapperTest()
        {
            var testTarget              = new MyTarget();
            var bufferingTargetWrapper  = new BufferingTargetWrapper(testTarget, 100);
            var autoFlushOnLevelWrapper = new AutoFlushTargetWrapper(bufferingTargetWrapper);

            autoFlushOnLevelWrapper.Condition            = "level > LogLevel.Info";
            autoFlushOnLevelWrapper.FlushOnConditionOnly = true;
            testTarget.Initialize(null);
            bufferingTargetWrapper.Initialize(null);
            autoFlushOnLevelWrapper.Initialize(null);

            AsyncContinuation continuation = ex => { };

            autoFlushOnLevelWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Trace, "*", "test").WithContinuation(continuation));
            Assert.Equal(0, testTarget.WriteCount);
            Assert.Equal(0, testTarget.FlushCount);
            autoFlushOnLevelWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Fatal, "*", "test").WithContinuation(continuation));
            Assert.Equal(2, testTarget.WriteCount);
            Assert.Equal(1, testTarget.FlushCount);
            autoFlushOnLevelWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Trace, "*", "Please do not FlushThis").WithContinuation(continuation));
            Assert.Equal(2, testTarget.WriteCount);
            Assert.Equal(1, testTarget.FlushCount);
            autoFlushOnLevelWrapper.Flush(continuation);
            Assert.Equal(2, testTarget.WriteCount);
            Assert.Equal(1, testTarget.FlushCount);
        }
コード例 #6
0
        public void AutoFlushOnConditionTest()
        {
            var testTarget       = new MyTarget();
            var autoFlushWrapper = new AutoFlushTargetWrapper(testTarget);

            autoFlushWrapper.Condition = "level > LogLevel.Info";
            testTarget.Initialize(null);
            autoFlushWrapper.Initialize(null);
            AsyncContinuation continuation = ex => { };

            autoFlushWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Info, "*", "test").WithContinuation(continuation));
            autoFlushWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Trace, "*", "test").WithContinuation(continuation));
            Assert.Equal(2, testTarget.WriteCount);
            Assert.Equal(0, testTarget.FlushCount);
            autoFlushWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Warn, "*", "test").WithContinuation(continuation));
            autoFlushWrapper.WriteAsyncLogEvent(LogEventInfo.Create(LogLevel.Error, "*", "test").WithContinuation(continuation));
            Assert.Equal(4, testTarget.WriteCount);
            Assert.Equal(2, testTarget.FlushCount);
        }
コード例 #7
0
        public void AutoFlushTargetWrapperAsyncWithExceptionTest1()
        {
            var myTarget = new MyAsyncTarget
            {
                ThrowExceptions = true,
            };

            var wrapper = new AutoFlushTargetWrapper(myTarget);

            myTarget.Initialize(null);
            wrapper.Initialize(null);
            var               logEvent        = new LogEventInfo();
            Exception         lastException   = null;
            var               continuationHit = new ManualResetEvent(false);
            AsyncContinuation continuation    =
                ex =>
            {
                lastException = ex;
                continuationHit.Set();
            };

            wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));

            continuationHit.WaitOne();
            Assert.NotNull(lastException);
            Assert.IsType <InvalidOperationException>(lastException);

            // no flush on exception
            Assert.Equal(0, myTarget.FlushCount);
            Assert.Equal(1, myTarget.WriteCount);

            continuationHit.Reset();
            lastException = null;
            wrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));
            continuationHit.WaitOne();
            Assert.NotNull(lastException);
            Assert.IsType <InvalidOperationException>(lastException);
            Assert.Equal(0, myTarget.FlushCount);
            Assert.Equal(2, myTarget.WriteCount);
        }