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


C# AsyncTargetWrapper.WriteLogEvent方法代码示例

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


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

示例1: AsyncTargetWrapperAsyncTest1

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

            targetWrapper.WriteLogEvent(logEvent, continuation);

            continuationHit.WaitOne();
            Assert.IsNull(lastException);
            Assert.AreEqual(1, myTarget.WriteCount);

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

示例2: AsyncTargetWrapperAsyncWithExceptionTest1

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

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

            targetWrapper.WriteLogEvent(logEvent, continuation);

            continuationHit.WaitOne();
            Assert.IsNotNull(lastException);
            Assert.IsInstanceOfType(lastException, typeof(InvalidOperationException));

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

            continuationHit.Reset();
            lastException = null;
            targetWrapper.WriteLogEvent(logEvent, continuation);
            continuationHit.WaitOne();
            Assert.IsNotNull(lastException);
            Assert.IsInstanceOfType(lastException, typeof(InvalidOperationException));
            Assert.AreEqual(0, myTarget.FlushCount);
            Assert.AreEqual(2, myTarget.WriteCount);
        }
开发者ID:igalse,项目名称:NLog,代码行数:39,代码来源:AsyncTargetWrapperTests.cs

示例3: AsyncTargetWrapperSyncTest1

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

            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.WriteLogEvent(logEvent, continuation);

            // continuation was not hit
            continuationHit.WaitOne();
            Assert.AreNotSame(continuationThread, Thread.CurrentThread);
            Assert.IsNull(lastException);
            Assert.AreEqual(1, myTarget.WriteCount);

            continuationHit.Reset();
            targetWrapper.WriteLogEvent(logEvent, continuation);
            continuationHit.WaitOne();
            Assert.AreNotSame(continuationThread, Thread.CurrentThread);
            Assert.IsNull(lastException);
            Assert.AreEqual(2, myTarget.WriteCount);
        }
开发者ID:igalse,项目名称:NLog,代码行数:37,代码来源:AsyncTargetWrapperTests.cs

示例4: AsyncTargetWrapperFlushTest

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

            };

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

            ((ISupportsInitialize)targetWrapper).Initialize();
            ((ISupportsInitialize)myTarget).Initialize();

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

            int eventCount = 5000;

            for (int i = 0; i < eventCount; ++i)
            {
                targetWrapper.WriteLogEvent(LogEventInfo.CreateNullEvent(),
                    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.AreEqual(eventCount, exceptions.Count);

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

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

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

示例5: AsyncTargetWrapperExceptionTest

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

            ((ISupportsInitialize)targetWrapper).Initialize();

            // don't set wrapped taret - will cause exception on the timer thread
            string internalLog = RunAndCaptureInternalLog(
                () =>
                {
                    targetWrapper.WriteLogEvent(LogEventInfo.CreateNullEvent(), ex => { });
                    Thread.Sleep(1000);
                },
                LogLevel.Trace);

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

示例6: AsyncTargetWrapperCloseTest

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

            };

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

            ((ISupportsInitialize)targetWrapper).Initialize();
            ((ISupportsInitialize)myTarget).Initialize();

            bool continuationHit = false;

            targetWrapper.WriteLogEvent(LogEventInfo.CreateNullEvent(), ex => { continuationHit = true; });

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

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


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