本文整理汇总了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);
}
示例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);
}
示例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);
}
示例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);
}
}
示例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);
}
示例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);
}