当前位置: 首页>>代码示例>>C#>>正文


C# AsyncTargetWrapper.Initialize方法代码示例

本文整理汇总了C#中NLog.Targets.Wrappers.AsyncTargetWrapper.Initialize方法的典型用法代码示例。如果您正苦于以下问题:C# AsyncTargetWrapper.Initialize方法的具体用法?C# AsyncTargetWrapper.Initialize怎么用?C# AsyncTargetWrapper.Initialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在NLog.Targets.Wrappers.AsyncTargetWrapper的用法示例。


在下文中一共展示了AsyncTargetWrapper.Initialize方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AsyncTargetWrapperAsyncTest1

        public void AsyncTargetWrapperAsyncTest1()
        {
            var myTarget = new MyAsyncTarget();
            var targetWrapper = new AsyncTargetWrapper(myTarget) { Name = "AsyncTargetWrapperAsyncTest1_Wrapper" };
            targetWrapper.Initialize(null);
            myTarget.Initialize(null);
            var logEvent = new LogEventInfo();
            Exception lastException = null;
            var continuationHit = new ManualResetEvent(false);
            AsyncContinuation continuation =
                ex =>
                {
                    lastException = ex;
                    continuationHit.Set();
                };

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

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

            continuationHit.Reset();
            targetWrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));
            continuationHit.WaitOne();
            Assert.Null(lastException);
            Assert.Equal(2, myTarget.WriteCount);
        }
开发者ID:MikeFH,项目名称:NLog,代码行数:28,代码来源:AsyncTargetWrapperTests.cs

示例2: MyTarget

        public void AsyncTargetWrapperInitTest_WhenTimeToSleepBetweenBatchesIsEqualToZero_ShouldThrowNLogConfigurationException() {
            LogManager.ThrowConfigExceptions = true;

            var myTarget = new MyTarget();
            var targetWrapper = new AsyncTargetWrapper() {
                WrappedTarget = myTarget,
                TimeToSleepBetweenBatches = 0,
            };
            Assert.Throws<NLogConfigurationException>(() => targetWrapper.Initialize(null));
        }
开发者ID:bryjamus,项目名称:NLog,代码行数:10,代码来源:AsyncTargetWrapperTests.cs

示例3: AsyncTargetWrapperSyncTest1

        public void AsyncTargetWrapperSyncTest1()
        {
            var myTarget = new MyTarget();
            var targetWrapper = new AsyncTargetWrapper
            {
                WrappedTarget = myTarget,
            };
            targetWrapper.Initialize(null);
            myTarget.Initialize(null);

            var logEvent = new LogEventInfo();
            Exception lastException = null;
            ManualResetEvent continuationHit = new ManualResetEvent(false);
            Thread continuationThread = null;
            AsyncContinuation continuation =
                ex =>
                    {
                        lastException = ex;
                        continuationThread = Thread.CurrentThread;
                        continuationHit.Set();
                    };

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

            // continuation was not hit 
            Assert.True(continuationHit.WaitOne(2000));
            Assert.NotSame(continuationThread, Thread.CurrentThread);
            Assert.Null(lastException);
            Assert.Equal(1, myTarget.WriteCount);

            continuationHit.Reset();
            targetWrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));
            continuationHit.WaitOne();
            Assert.NotSame(continuationThread, Thread.CurrentThread);
            Assert.Null(lastException);
            Assert.Equal(2, myTarget.WriteCount);
        }
开发者ID:RexTremendae,项目名称:NLog,代码行数:37,代码来源:AsyncTargetWrapperTests.cs

示例4: FlushingMultipleTimesSimultaneous

        public void FlushingMultipleTimesSimultaneous()
        {
            var asyncTarget = new AsyncTargetWrapper
            {
                TimeToSleepBetweenBatches = 2000,
                WrappedTarget = new DebugTarget(),
            };
            asyncTarget.Initialize(null);

            asyncTarget.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(ex => { }));

            var firstContinuationCalled = false;
            var secondContinuationCalled = false;
            var firstContinuationResetEvent = new ManualResetEvent(false);
            var secondContinuationResetEvent = new ManualResetEvent(false);
            asyncTarget.Flush(ex =>
            {
                firstContinuationCalled = true;
                firstContinuationResetEvent.Set();
            });
            asyncTarget.Flush(ex =>
            {
                secondContinuationCalled = true;
                secondContinuationResetEvent.Set();
            });

            firstContinuationResetEvent.WaitOne();
            secondContinuationResetEvent.WaitOne();
            Assert.True(firstContinuationCalled);
            Assert.True(secondContinuationCalled);
        }
开发者ID:RexTremendae,项目名称:NLog,代码行数:31,代码来源:AsyncTargetWrapperTests.cs

示例5: AsyncTargetWrapperExceptionTest

        public void AsyncTargetWrapperExceptionTest()
        {
            var targetWrapper = new AsyncTargetWrapper
            {
                OverflowAction = AsyncTargetWrapperOverflowAction.Grow,
                TimeToSleepBetweenBatches = 500,
                WrappedTarget = new DebugTarget(),
            };

            targetWrapper.Initialize(null);

            // null out wrapped target - will cause exception on the timer thread
            targetWrapper.WrappedTarget = null;

            string internalLog = RunAndCaptureInternalLog(
                () =>
                {
                    targetWrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(ex => { }));
                    Thread.Sleep(3000);
                },
                LogLevel.Trace);

            targetWrapper.Close();
            Assert.True(internalLog.StartsWith("Error Error in lazy writer timer procedure: System.NullReferenceException", StringComparison.Ordinal), internalLog);
        }
开发者ID:RexTremendae,项目名称:NLog,代码行数:25,代码来源:AsyncTargetWrapperTests.cs

示例6: AsyncTargetWrapperCloseTest

        public void AsyncTargetWrapperCloseTest()
        {
            var myTarget = new MyAsyncTarget
            {
                ThrowExceptions = true,

            };

            var targetWrapper = new AsyncTargetWrapper(myTarget)
            {
                OverflowAction = AsyncTargetWrapperOverflowAction.Grow,
                TimeToSleepBetweenBatches = 1000,
            };

            targetWrapper.Initialize(null);
            myTarget.Initialize(null);

            targetWrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(ex => { }));

            // quickly close the target before the timer elapses
            targetWrapper.Close();
        }
开发者ID:RexTremendae,项目名称:NLog,代码行数:22,代码来源:AsyncTargetWrapperTests.cs

示例7: AsyncTargetWrapperFlushTest

        public void AsyncTargetWrapperFlushTest()
        {
            var myTarget = new MyAsyncTarget
            {
                ThrowExceptions = true,
               
            };

            var targetWrapper = new AsyncTargetWrapper(myTarget)
            {
                OverflowAction = AsyncTargetWrapperOverflowAction.Grow,
            };

            targetWrapper.Initialize(null);
            myTarget.Initialize(null);

            List<Exception> exceptions = new List<Exception>();

            int eventCount = 5000;

            for (int i = 0; i < eventCount; ++i)
            {
                targetWrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(
                    ex =>
                    {
                        lock (exceptions)
                        {
                            exceptions.Add(ex);
                        }
                    }));
            }

            Exception lastException = null;
            ManualResetEvent mre = new ManualResetEvent(false);

            string internalLog = RunAndCaptureInternalLog(
                () =>
                {
                    targetWrapper.Flush(
                        cont =>
                        {
                            try
                            {
                                // by this time all continuations should be completed
                                Assert.Equal(eventCount, exceptions.Count);

                                // with just 1 flush of the target
                                Assert.Equal(1, myTarget.FlushCount);

                                // and all writes should be accounted for
                                Assert.Equal(eventCount, myTarget.WriteCount);
                            }
                            catch (Exception ex)
                            {
                                lastException = ex;
                            }
                            finally
                            {
                                mre.Set();
                            }
                        });
                    Assert.True(mre.WaitOne());
                },
                LogLevel.Trace);

            if (lastException != null)
            {
                Assert.True(false, lastException.ToString() + "\r\n" + internalLog);
            }
        }
开发者ID:RexTremendae,项目名称:NLog,代码行数:70,代码来源:AsyncTargetWrapperTests.cs

示例8: AsyncTargetWrapperAsyncWithExceptionTest1

        public void AsyncTargetWrapperAsyncWithExceptionTest1()
        {
            var myTarget = new MyAsyncTarget
            {
                ThrowExceptions = true,
            };

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

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

            Assert.True(continuationHit.WaitOne());
            Assert.NotNull(lastException);
            Assert.IsType(typeof(InvalidOperationException), lastException);

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

            continuationHit.Reset();
            lastException = null;
            targetWrapper.WriteAsyncLogEvent(logEvent.WithContinuation(continuation));
            continuationHit.WaitOne();
            Assert.NotNull(lastException);
            Assert.IsType(typeof(InvalidOperationException), lastException);
            Assert.Equal(0, myTarget.FlushCount);
            Assert.Equal(2, myTarget.WriteCount);
        }
开发者ID:RexTremendae,项目名称:NLog,代码行数:39,代码来源:AsyncTargetWrapperTests.cs

示例9: AsyncTargetWrapperSyncTest_WhenTimeToSleepBetweenBatchesIsEqualToZero

        public void AsyncTargetWrapperSyncTest_WhenTimeToSleepBetweenBatchesIsEqualToZero()
        {
            LogManager.ThrowConfigExceptions = true;

            var myTarget = new MyTarget();
            var targetWrapper = new AsyncTargetWrapper() {
                WrappedTarget = myTarget,
                TimeToSleepBetweenBatches = 0,
                BatchSize = 4,
                QueueLimit = 2, // Will make it "sleep" between every second write
                OverflowAction = AsyncTargetWrapperOverflowAction.Block
            };
            targetWrapper.Initialize(null);
            myTarget.Initialize(null);

            try
            {
                int flushCounter = 0;
                AsyncContinuation flushHandler = (ex) => { ++flushCounter; };

                List<KeyValuePair<LogEventInfo, AsyncContinuation>> itemPrepareList = new List<KeyValuePair<LogEventInfo, AsyncContinuation>>(2500);
                List<int> itemWrittenList = new List<int>(itemPrepareList.Capacity);
                for (int i = 0; i< itemPrepareList.Capacity; ++i)
                {
                    var logEvent = new LogEventInfo();
                    int sequenceID = logEvent.SequenceID;
                    itemPrepareList.Add(new KeyValuePair<LogEventInfo, AsyncContinuation>(logEvent, (ex) => itemWrittenList.Add(sequenceID)));
                }

                long startTicks = Environment.TickCount;
                for (int i = 0; i < itemPrepareList.Count; ++i)
                {
                    var logEvent = itemPrepareList[i].Key;
                    targetWrapper.WriteAsyncLogEvent(logEvent.WithContinuation(itemPrepareList[i].Value));
                }

                targetWrapper.Flush(flushHandler);

                for (int i = 0; i < itemPrepareList.Count * 2 && itemWrittenList.Count != itemPrepareList.Count; ++i)
                    System.Threading.Thread.Sleep(1);

                long elapsedMilliseconds = Environment.TickCount - startTicks;

                Assert.Equal(itemPrepareList.Count, itemWrittenList.Count);
                int prevSequenceID = 0;
                for (int i = 0; i < itemWrittenList.Count; ++i)
                {
                    Assert.True(prevSequenceID < itemWrittenList[i]);
                    prevSequenceID = itemWrittenList[i];
                }

#if MONO || NET3_5
                Assert.True(elapsedMilliseconds < 2500);    // Skip timing test when running within OpenCover.Console.exe
#endif

                targetWrapper.Flush(flushHandler);
                for (int i = 0; i < 2000 && flushCounter != 2; ++i)
                    System.Threading.Thread.Sleep(1);
                Assert.Equal(2, flushCounter);
            }
            finally
            {
                myTarget.Close();
                targetWrapper.Close();
            }
        }
开发者ID:daniefer,项目名称:NLog,代码行数:66,代码来源:AsyncTargetWrapperTests.cs

示例10: AsyncTargetWrapperExceptionTest

        public void AsyncTargetWrapperExceptionTest()
        {
            var targetWrapper = new AsyncTargetWrapper
            {
                OverflowAction = AsyncTargetWrapperOverflowAction.Grow,
                TimeToSleepBetweenBatches = 500,
                WrappedTarget = new DebugTarget(),
                Name = "AsyncTargetWrapperExceptionTest_Wrapper"
            };

            LogManager.ThrowExceptions = false;

            targetWrapper.Initialize(null);

            // null out wrapped target - will cause exception on the timer thread
            targetWrapper.WrappedTarget = null;

            string internalLog = RunAndCaptureInternalLog(
                () =>
                {
                    targetWrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(ex => { }));
                    targetWrapper.Close();
                },
                LogLevel.Trace);

            Assert.True(internalLog.Contains("AsyncWrapper 'AsyncTargetWrapperExceptionTest_Wrapper': WrappedTarget is NULL"), internalLog);
        }
开发者ID:daniefer,项目名称:NLog,代码行数:27,代码来源:AsyncTargetWrapperTests.cs

示例11: NoEmptyEventLists

 public void NoEmptyEventLists()
 {
     var configuration = new LoggingConfiguration();
     var target = new MyLogReceiverWebServiceTarget();
     target.EndpointAddress = "http://notimportant:9999/";
     target.Initialize(configuration);
     var asyncTarget = new AsyncTargetWrapper(target)
     {
         Name = "NoEmptyEventLists_wrapper"
     };
     try
     {
         asyncTarget.Initialize(configuration);
         asyncTarget.WriteAsyncLogEvents(new[] { LogEventInfo.Create(LogLevel.Info, "logger1", "message1").WithContinuation(ex => { }) });
         Thread.Sleep(1000);
         Assert.Equal(1, target.SendCount);
     }
     finally
     {
         asyncTarget.Close();
         target.Close();
     }
 }
开发者ID:CharlieBP,项目名称:NLog,代码行数:23,代码来源:LogReceiverWebServiceTargetTests.cs

示例12: AsyncTargetWrapperCloseTest

        public void AsyncTargetWrapperCloseTest()
        {
            var myTarget = new MyAsyncTarget
            {
                ThrowExceptions = true,

            };

            var targetWrapper = new AsyncTargetWrapper(myTarget)
            {
                OverflowAction = AsyncTargetWrapperOverflowAction.Grow,
                TimeToSleepBetweenBatches = 1000,
            };

            targetWrapper.Initialize(null);
            myTarget.Initialize(null);

            bool continuationHit = false;

            targetWrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(ex => { continuationHit = true; }));

            // quickly close the target before the timer elapses
            targetWrapper.Close();

            // continuation will not be hit because the thread is down.
            Thread.Sleep(1000);
            Assert.IsFalse(continuationHit);
        }
开发者ID:rameshr,项目名称:NLog,代码行数:28,代码来源:AsyncTargetWrapperTests.cs


注:本文中的NLog.Targets.Wrappers.AsyncTargetWrapper.Initialize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。