public void RandomizeGroupSyncTest2() { var wrapper = new RandomizeGroupTarget() { // no targets }; wrapper.Initialize(CommonCfg); List<Exception> exceptions = new List<Exception>(); // no exceptions for (int i = 0; i < 10; ++i) { wrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add)); } Assert.AreEqual(10, exceptions.Count); foreach (var e in exceptions) { Assert.IsNull(e); } Exception flushException = new Exception("Flush not hit synchronously."); wrapper.Flush(ex => flushException = ex); if (flushException != null) { Assert.Fail(flushException.ToString()); } }
public void RandomizeGroupSyncTest1() { var myTarget1 = new MyTarget(); var myTarget2 = new MyTarget(); var myTarget3 = new MyTarget(); var wrapper = new RandomizeGroupTarget() { Targets = { myTarget1, myTarget2, myTarget3 }, }; myTarget1.Initialize(null); myTarget2.Initialize(null); myTarget3.Initialize(null); wrapper.Initialize(null); List<Exception> exceptions = new List<Exception>(); // no exceptions for (int i = 0; i < 10; ++i) { wrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add)); } Assert.AreEqual(10, exceptions.Count); foreach (var e in exceptions) { Assert.IsNull(e); } Assert.AreEqual(10, myTarget1.WriteCount + myTarget2.WriteCount + myTarget3.WriteCount); Exception flushException = null; var flushHit = new ManualResetEvent(false); wrapper.Flush(ex => { flushException = ex; flushHit.Set(); }); flushHit.WaitOne(); if (flushException != null) { Assert.Fail(flushException.ToString()); } Assert.AreEqual(1, myTarget1.FlushCount); Assert.AreEqual(1, myTarget2.FlushCount); Assert.AreEqual(1, myTarget3.FlushCount); }
static void Main(string[] args) { FileTarget file1 = new FileTarget(); file1.FileName = "${basedir}/file1.txt"; FileTarget file2 = new FileTarget(); file2.FileName = "${basedir}/file2.txt"; RandomizeGroupTarget target = new RandomizeGroupTarget(); target.Targets.Add(file1); target.Targets.Add(file2); NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug); Logger logger = LogManager.GetLogger("Example"); logger.Debug("log message"); }