Esempio n. 1
0
        public void MonitorPublisherEventScope()
        {
            var c = new TestMonitorEventConsumer(MonitorEventLevel.Information, MonitorEventOpCode.All);

            List <MonitorEventEntry> items = new List <MonitorEventEntry>();

            c.OnEventAction = (e) =>
            {
                items.Add(e);
            };

            using (EventPublisher.AddConsumer(c))
            {
                using (EventPublisher.Default.RaiseScope(MonitorEventLevel.Information, name: "foo"))
                {
                    Thread.Sleep(2);
                }
            }

            Assert.AreEqual(2, items.Count);
            Assert.AreEqual(MonitorEventOpCode.Start, items[0].OpCode);
            Assert.AreEqual(0.0, items[0].RuntimeMilliseconds);
            Assert.AreEqual(MonitorEventOpCode.Stop, items[1].OpCode);
            Assert.AreNotEqual(0.0, items[1].RuntimeMilliseconds);
        }
        public void MonitorEventTimeStampSet()
        {
            DateTime start = DateTime.UtcNow;

            var consumer = new TestMonitorEventConsumer(MonitorEventLevel.All, MonitorEventOpCode.All);

            DateTime eventTime      = DateTime.MinValue;
            bool     callbackRaised = false;

            consumer.OnEventAction = (e) =>
            {
                eventTime      = e.TimeStamp;
                callbackRaised = true;
            };

            using (EventPublisher.AddConsumer(consumer))
            {
                EventPublisher.Default.Raise(name: "Foo");

                DateTime end = DateTime.UtcNow;

                Assert.IsTrue(callbackRaised, "Callback raised");
                Assert.IsTrue(eventTime >= start, "Timestamp after start of test (Start: {0}, TimeStamp: {1})", start, eventTime);
                Assert.IsTrue(eventTime <= end, "Timestamp before end of test (End: {0}, Timestamp: {1})", end, eventTime);
            }
        }
        public void Monitor2Consumers()
        {
            var consumer = new TestMonitorEventConsumer(MonitorEventLevel.All, MonitorEventOpCode.All);

            int callCount = 0;

            consumer.OnEventAction = (e) =>
            {
                Assert.AreEqual(e.OpCode, MonitorEventOpCode.Stop);
                Assert.AreEqual(e.Level, MonitorEventLevel.Information);
                Assert.AreEqual("Test1", e.Name);
                callCount++;
            };

            var consumer2 = new TestMonitorEventConsumer(MonitorEventLevel.All, MonitorEventOpCode.All);

            consumer2.OnEventAction = (e) =>
            {
                Assert.AreEqual(e.OpCode, MonitorEventOpCode.Stop);
                Assert.AreEqual(e.Level, MonitorEventLevel.Information);
                Assert.AreEqual("Test1", e.Name);
                callCount++;
            };

            using (EventPublisher.AddConsumer(consumer))
            {
                using (EventPublisher.AddConsumer(consumer2))
                {
                    EventPublisher.Default.Raise(MonitorEventLevel.Information, MonitorEventOpCode.Stop, name: "Test1");
                }
            }

            Assert.AreEqual(2, callCount);
        }
        public void MonitorMarkFiltering()
        {
            var consumer = new TestMonitorEventConsumer(MonitorEventLevel.All, MonitorEventOpCode.Start);

            bool eventCalled = false;

            consumer.OnEventAction = (e) => { eventCalled = true; };

            using (EventPublisher.AddConsumer(consumer))
            {
                EventPublisher.Default.Raise(MonitorEventLevel.Information, MonitorEventOpCode.Stop);
            }

            Assert.IsFalse(eventCalled);
        }
        public void MonitorLevelFiltering()
        {
            var consumer = new TestMonitorEventConsumer(MonitorEventLevel.Error, MonitorEventOpCode.Start);

            bool eventCalled = false;

            consumer.OnEventAction = (e) => { eventCalled = true; };

            using (EventPublisher.AddConsumer(consumer))
            {
                EventPublisher.Default.Raise(MonitorEventLevel.Error, MonitorEventOpCode.Mark);
                EventPublisher.Default.Raise(MonitorEventLevel.Warning, MonitorEventOpCode.Start);
                Assert.IsFalse(eventCalled, "Event should not be raised to consumer");
                EventPublisher.Default.Raise(MonitorEventLevel.Error, MonitorEventOpCode.Start);
                Assert.IsTrue(eventCalled, "Event should be raised to consumer");
            }
        }
        public void MonitorLevelChange()
        {
            var consumer = new TestMonitorEventConsumer(MonitorEventLevel.Error, MonitorEventOpCode.All);

            bool eventCalled = false;

            consumer.OnEventAction = (e) => { eventCalled = true; };

            using (EventPublisher.AddConsumer(consumer))
            {
                EventPublisher.Default.Raise(MonitorEventLevel.Information, MonitorEventOpCode.Mark);
                Assert.IsFalse(eventCalled, "Event should not be raised to consumer");
                consumer.ChangeEventLevel(MonitorEventLevel.All, MonitorEventOpCode.All);

                EventPublisher.Default.Raise(MonitorEventLevel.Information, MonitorEventOpCode.Mark);
                Assert.IsTrue(eventCalled, "Event should be raised to consumer");
            }
        }
        public void MonitorSingleEvent()
        {
            var consumer = new TestMonitorEventConsumer(MonitorEventLevel.All, MonitorEventOpCode.All);

            bool wasCalled = false;

            consumer.OnEventAction = (e) =>
            {
                Assert.AreEqual(e.OpCode, MonitorEventOpCode.Stop);
                Assert.AreEqual(e.Level, MonitorEventLevel.Information);
                Assert.AreEqual("Test1", e.Name);
                wasCalled = true;
            };

            using (EventPublisher.AddConsumer(consumer))
            {
                EventPublisher.Default.Raise(MonitorEventLevel.Information, MonitorEventOpCode.Stop, name: "Test1");
            }

            Assert.IsTrue(wasCalled);
        }
Esempio n. 8
0
        public void MonitorPublisherScopedEventSource()
        {
            var c = new TestMonitorEventConsumer(MonitorEventLevel.All, MonitorEventOpCode.All);

            List <MonitorEventEntry> items = new List <MonitorEventEntry>();
            bool eventRaised = false;

            c.OnEventAction = (e) =>
            {
                Assert.AreEqual("MySource", e.Source);
                Assert.AreEqual("test", e.Name);
                eventRaised = true;
            };

            using (EventPublisher.AddConsumer(c))
            {
                var source = EventPublisher.CreateEventSource("MySource");

                source.Raise(name: "test");
            }

            Assert.IsTrue(eventRaised);
        }
        public void MonitorDispose()
        {
            var consumer = new TestMonitorEventConsumer(MonitorEventLevel.All, MonitorEventOpCode.All);

            bool disposeCalled = false;
            bool eventCalled   = false;

            consumer.OnDisposeAction = () => { disposeCalled = true; };
            consumer.OnEventAction   = (e) => { eventCalled = true; };

            using (EventPublisher.AddConsumer(consumer))
            {
                EventPublisher.Default.Raise(MonitorEventLevel.Information);
                Assert.IsTrue(eventCalled);
                Assert.IsFalse(disposeCalled);

                eventCalled = false;
            }

            Assert.IsTrue(disposeCalled);

            EventPublisher.Default.Raise(MonitorEventLevel.Information);
            Assert.IsFalse(eventCalled);
        }