static void Main(string[] args) { FileTarget target = new FileTarget(); target.Layout = "${longdate} ${logger} ${message}"; target.FileName = "${basedir}/logs/logfile.txt"; target.KeepFileOpen = false; target.Encoding = "iso-8859-2"; AsyncTargetWrapper wrapper = new AsyncTargetWrapper(); wrapper.WrappedTarget = target; wrapper.QueueLimit = 5000; wrapper.OverflowAction = AsyncTargetWrapperOverflowAction.Discard; NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(wrapper, LogLevel.Debug); Logger logger = LogManager.GetLogger("Example"); logger.Debug("log message"); wrapper.Flush(); }
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); }
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); } }
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(); } }