public void CreateByCodeOwnDaemon() { using (var logger = new LogDaemon(NOPApplication.Instance)) { logger.WriteInterval = 0; //set the minimum flush period Aver.Throws <AzosException>(() => logger.Start()); //can not start daemon with no sinks Aver.IsFalse(logger.Running); Aver.IsFalse(logger.Sinks.Any()); using (var sink = new MemoryBufferSink(logger)) { sink.Name.See(); Aver.IsTrue(sink.Name.StartsWith("MemoryBufferSink.")); //anonymous sinks get their name from their: 'type.fid' logger.Start(); //now it can start Aver.IsTrue(logger.Running); Aver.AreSameRef(sink, logger.Sinks.First()); //now there is a sink registered which is this one Aver.IsTrue(sink.Running); //it was auto-started by the logger logger.Write(new Message { Type = MessageType.DebugB, Text = "This is a message #1" }); //so this messages goes in it Thread.Sleep(DAEMON_FLUSH_WAIT_MS); //make sure async flush happens Aver.IsTrue(sink.Buffered.Any()); //because sink was active var logged = sink.Buffered.FirstOrDefault(); // we get the first message buffered Aver.AreEqual("This is a message #1", logged.Text); Aver.IsTrue(MessageType.DebugB == logged.Type); sink.WaitForCompleteStop();//stop the sink Aver.IsFalse(sink.Running); logger.Write(new Message { Type = MessageType.Debug, Text = "This is a message #2" }); Thread.Sleep(DAEMON_FLUSH_WAIT_MS); //make sure async flush happens Aver.AreEqual(1, sink.Buffered.Count()); //because sink was not turned on for 2nd, only the first message got in, 2nd got lost } Aver.IsFalse(logger.Sinks.Any());//again, no sinks left } }
protected override void DoStart() { try { m_CPULoadFactor = 1d; m_SubInstrCallerCount = 0; m_SubInstr.Start(); m_SubLog.Start(); m_Locker.Start(); m_WaitEvent = new AutoResetEvent(false); m_Thread = new Thread(threadSpin); m_Thread.Name = THREAD_NAME; m_Thread.Start(); } catch { if (m_Locker.Running) { try { m_Locker.WaitForCompleteStop(); } catch { } } if (m_SubLog.Running) { try { m_SubLog.WaitForCompleteStop(); } catch { } } if (m_SubInstr.Running) { try { m_SubInstr.WaitForCompleteStop(); } catch { } } AbortStart(); throw; } }
protected override void DoStart() { base.DoStart(); m_Daemon.Start(); }