public void TestEventsAreSentCorrectly()
        {
            var    ex   = new Exception("Hello");
            string msg1 = "Event1";
            string msg2 = "Event2";

            SystemEvents.DispatchDiagnosicEvent(msg1, LogLevel.Error);
            SystemEvents.DispatchDiagnosicEvent(msg2, LogLevel.Fatal, ex);
            var events = _genertorUnderTest.GetEvents().Cast <Diagnostic>().ToList();

            Assert.AreEqual(2, events.Count());

            var pid = Process.GetCurrentProcess().Id;
            var tid = Thread.CurrentThread.ManagedThreadId;

            var actualEvent = events.ElementAt(0);

            Assert.AreEqual(pid, actualEvent.Payload.First().ProcessId);
            Assert.AreEqual(tid, actualEvent.Payload.First().ThreadId);
            Assert.AreEqual(msg1, actualEvent.Payload.First().Message);
            Assert.AreEqual(LogLevel.Error.ToString(), actualEvent.Payload.First().Severity);
            Assert.AreEqual(ThreadContext.Get().ExecutionId, actualEvent.Payload.First().CorrelationId);

            actualEvent = events.ElementAt(1);
            Assert.AreEqual(pid, actualEvent.Payload.First().ProcessId);
            Assert.AreEqual(tid, actualEvent.Payload.First().ThreadId);
            Assert.AreEqual(ex.FormatExceptionMessage(msg2), actualEvent.Payload.First().Message);
            Assert.AreEqual(LogLevel.Fatal.ToString(), actualEvent.Payload.First().Severity);
            Assert.AreEqual(ThreadContext.Get().ExecutionId, actualEvent.Payload.First().CorrelationId);
        }
        public void TestQueueSizeDoesNotPassLimit()
        {
            var internalQueueSize = (uint)(AgentConfiguration.RemoteConfiguration.MaxLocalCacheSize * 0.2);

            string msg1 = "Event1";

            SystemEvents.DispatchDiagnosicEvent(msg1, LogLevel.Error);
            var events    = _genertorUnderTest.GetEvents().Cast <Diagnostic>();
            var eventSize = events.First().EstimatedSize;

            uint totalSize = 0;

            while (totalSize < 6 * internalQueueSize)
            {
                SystemEvents.DispatchDiagnosicEvent(msg1, LogLevel.Error);
                totalSize += eventSize;
            }

            events = _genertorUnderTest.GetEvents().Cast <Diagnostic>();

            uint actualSize = 0;

            foreach (var ev in events)
            {
                actualSize += ev.EstimatedSize;
            }

            Assert.IsTrue(actualSize <= internalQueueSize);
        }
        /// <summary>
        /// Send a diagnostic event corresponding to a log
        /// </summary>
        /// <param name="message">The message</param>
        /// <param name="level">The severity level of the diagnostic event</param>
        /// <param name="correlationId">current correlation id</param>
        private static void SendDiagnosticEvent(string message, LogLevel level, Guid correlationId)
        {
            //the logger could potentially reach a circular call when sending diagnostic events, don't send yet another diagnostic event if that happens
            var stackTraceFrames   = new StackTrace().GetFrames();
            int currentMethodCount = stackTraceFrames.Count(frame => frame.GetMethod().Equals(stackTraceFrames[0].GetMethod()));

            if (currentMethodCount > 1)
            {
                return;
            }

            SystemEvents.DispatchDiagnosicEvent(message, level);
        }
        public void TestQueueIsClearedAfterGetEvents()
        {
            var    ex   = new Exception("Hello");
            string msg1 = "Event1";
            string msg2 = "Event2";

            SystemEvents.DispatchDiagnosicEvent(msg1, LogLevel.Error);
            SystemEvents.DispatchDiagnosicEvent(msg2, LogLevel.Fatal, ex);
            var events = _genertorUnderTest.GetEvents().Cast <Diagnostic>();

            Assert.AreEqual(2, events.Count());

            events = _genertorUnderTest.GetEvents().Cast <Diagnostic>();
            Assert.AreEqual(0, events.Count());
        }