コード例 #1
0
        public void CanImplementInterfaceWithGenericTaskMethods()
        {
            // turn on logging
            var log = EventSourceImplementer.GetEventSourceAs <ITestServiceWithGenericTaskMethods>();

            _listener.EnableEvents((EventSource)log, EventLevel.LogAlways, (EventKeywords)(-1));

            Assert.AreEqual(null, log.GetNothing());
            Assert.AreEqual(null, log.GetItem((int)1));

            // look at the events
            var events = _listener.Events.ToArray();

            Assert.AreEqual(2, events.Length);

            // check the individual events to make sure the data came back in the payload
            //Assert.AreEqual(1, events[0].Payload.Count);

            // create a proxy on the interface
            _listener.Reset();
            var proxy = TracingProxy.Create <ITestServiceWithGenericTaskMethods>(new TestServiceWithGenericTaskMethods());

            proxy.GetNothing().Wait();
            proxy.GetItem(1).Wait();

            events = _listener.Events.ToArray();
            Assert.AreEqual(4, events.Length);

            // check the individual events to make sure the data came back in the payload
            //Assert.AreEqual(1, events[0].Payload.Count);
            //Assert.AreEqual(1, events[1].Payload.Count);
        }
コード例 #2
0
        public void ExceptionAttributeShouldDetermineLevel()
        {
            Assert.AreEqual(EventLevel.Error, new EventAttributeProvider().ExceptionEventLevel);

            var logger = EventSourceImplementer.GetEventSourceAs <ILogInterfaceWithExceptionAttribute>();

            Listener.EnableEvents((EventSource)logger, EventLevel.LogAlways);

            logger.DoSomething();

            // look at the events
            var events = Listener.Events.ToArray();

            Assert.AreEqual(1, events.Length);
            Assert.AreEqual(EventLevel.Informational, events[0].Level);

            Listener.Reset();
            var proxy = TracingProxy.Create <ILogInterfaceWithExceptionAttribute>(new LogInterfaceWithExceptionAttribute());

            try { proxy.DoSomething(); }
            catch { }

            events = Listener.Events.ToArray();
            Assert.AreEqual(2, events.Length);
            Assert.AreEqual(EventLevel.Critical, events[1].Level);
        }
コード例 #3
0
        public void CanImplementInterfaceWithTaskReturn()
        {
            // this was causing issues with the completed method
            EventSourceImplementer.RegisterProvider <ITaskService>(new JsonObjectSerializer());
            var log = EventSourceImplementer.GetEventSourceAs <ITaskService>();

            _listener.EnableEvents((EventSource)log, EventLevel.LogAlways);

            // check the service
            var service = new TaskService();

            Assert.AreEqual("foo", service.GetItem("foo").Result);

            // try a proxy
            var proxy = TracingProxy.Create <ITaskService>(service);
            var task  = proxy.GetItem("foo");

            Assert.AreEqual("foo", task.Result);

            // look at the events
            var events = _listener.Events.ToArray();

            Assert.AreEqual(2, events.Length);

            // check the individual events to make sure the task came back in the payload
            Assert.AreEqual(1, events[1].Payload.Count);
            Assert.AreEqual(new JsonObjectSerializer().SerializeObject(task, null), events[1].Payload[0]);
        }
コード例 #4
0
        static void Main(string[] args)
        {
            // create the log
            var         log = EventSourceImplementer.GetEventSourceAs <IExampleLogSource>();
            EventSource es  = (EventSource)log;

            Console.WriteLine("Provider GUID = {0}", es.Guid);

            // create a listener
            var listener = new TestListener();

            listener.EnableEvents(es, EventLevel.LogAlways);

            using (new EventActivityScope())
            {
                log.Starting();
                for (int i = 0; i < 10; i++)
                {
                    using (new EventActivityScope())
                    {
                        log.AnEvent(String.Format("i = {0}", i));
                    }
                }
                log.Stopping();
            }

            TracingProxy.Create <Foo>(new Foo()).Bar();
            TracingProxy.Create <Foo>(new Foo()).Bar2();
        }
コード例 #5
0
        public void AsyncExceptionsAreLoggedToExceptionEvent()
        {
            EventSourceImplementer.RegisterProvider <IThrowExceptionsAsync>(new JsonObjectSerializer());
            var logger = EventSourceImplementer.GetEventSourceAs <IThrowExceptionsAsync>();

            _listener.EnableEvents((EventSource)logger, EventLevel.LogAlways, (EventKeywords)(-1));

            var proxy = TracingProxy.Create <IThrowExceptionsAsync>(new ThrowExceptionsAsync());

            // run the method and catch the exception
            Assert.Throws <AggregateException>(() => proxy.DoThrowAsync().Wait());

            // check the events
            var events = _listener.Events.ToArray();

            Assert.AreEqual(2, events.Length);

            // check the individual events
            Assert.AreEqual(logger, events[0].EventSource);
            Assert.AreEqual("", events[0].Message);
            Assert.AreEqual(EventLevel.Informational, events[0].Level);
            Assert.IsTrue(events[0].Keywords.HasFlag((EventKeywords)1));
            Assert.AreEqual(0, events[0].Payload.Count);

            // the exception is logged
            Assert.AreEqual(logger, events[1].EventSource);
            Assert.AreEqual("{0}", events[1].Message);
            Assert.AreEqual(EventLevel.Error, events[1].Level);
            Assert.IsTrue(events[1].Keywords.HasFlag((EventKeywords)1));
            Assert.AreEqual(1, events[1].Payload.Count);
            Assert.IsTrue(events[1].Payload[0].ToString().Contains("WhoopsAsync!"));
        }
コード例 #6
0
        public void ProxyFromVirtualClassWithoutInterfaceToUnrelatedInterfaceCanCollapseParameters()
        {
            EnableLogging <IHaveTraceAs>();

            var proxy = TracingProxy.Create <VirtualHasTraceAsWithoutInterface, IHaveTraceAs>(new VirtualHasTraceAsWithoutInterface());

            proxy.TraceAsData("p1", "p2");
            proxy.TraceSomeParameters("p", "p1", "p2");

            VerifyEvents();
        }
コード例 #7
0
        public void ProxyFromClassToInterfaceCanCollapseParameters()
        {
            EnableLogging <IHaveTraceAs>();

            var proxy = TracingProxy.Create <IHaveTraceAs>(new HasTraceAs());

            proxy.TraceAsData("p1", "p2");
            proxy.TraceSomeParameters("p", "p1", "p2");

            VerifyEvents();
        }
コード例 #8
0
        public void DerivedInterfaceCanBeImplemented()
        {
            var testLog = EventSourceImplementer.GetEventSourceAs <IDerivedCalculator>();

            _listener.EnableEvents((EventSource)testLog, EventLevel.LogAlways, (EventKeywords)(-1));

            var proxy = TracingProxy.Create <IDerivedCalculator>(new Calculator());

            proxy.Clear();
            proxy.AddNumbers(1, 2);
        }
コード例 #9
0
        public void TestLoggingProxyWithCompletedEvents()
        {
            // create a logger for the interface and listen on it
            var logger = EventSourceImplementer.GetEventSource <ICalculatorWithCompleted>();

            _listener.EnableEvents(logger, EventLevel.LogAlways, (EventKeywords)(-1));

            // create a calculator and a proxy
            var proxy = TracingProxy.Create <VirtualCalculator, ICalculatorWithCompleted>(new VirtualCalculator());

            // call the method through the proxy
            proxy.Clear();
            Assert.AreEqual(3, proxy.AddNumbers(1, 2));

            VerifyEvents(logger);

            // check the events
            var events = _listener.Events.ToArray();

            Assert.AreEqual(4, events.Length);
            var eventSource = events[0].EventSource;

            // check the individual events
            Assert.AreEqual(logger, events[0].EventSource);
            Assert.AreEqual(1, events[0].EventId);
            Assert.AreEqual("", events[0].Message);
            Assert.AreEqual(EventLevel.Informational, events[0].Level);
            Assert.IsTrue(events[0].Keywords.HasFlag((EventKeywords)1));
            Assert.AreEqual(0, events[0].Payload.Count);

            Assert.AreEqual(logger, events[1].EventSource);
            Assert.AreEqual("", events[1].Message);
            Assert.AreEqual(EventLevel.Informational, events[1].Level);
            Assert.IsTrue(events[1].Keywords.HasFlag((EventKeywords)2));
            Assert.AreEqual(0, events[0].Payload.Count);

            Assert.AreEqual(logger, events[2].EventSource);
            Assert.AreEqual("{0} {1}", events[2].Message);
            Assert.AreEqual(EventLevel.Informational, events[2].Level);
            Assert.IsTrue(events[2].Keywords.HasFlag((EventKeywords)4));
            Assert.AreEqual(2, events[2].Payload.Count);
            Assert.AreEqual(1, events[2].Payload[0]);
            Assert.AreEqual(2, events[2].Payload[1]);

            // a fourth event for completed
            Assert.AreEqual(logger, events[3].EventSource);
            Assert.AreEqual("{0}", events[3].Message);
            Assert.AreEqual(EventLevel.Informational, events[3].Level);
            Assert.IsTrue(events[3].Keywords.HasFlag((EventKeywords)8));
            Assert.AreEqual(1, events[3].Payload.Count);
            Assert.AreEqual(3, events[3].Payload[0]);
        }
コード例 #10
0
        public void MethodInterfaceShouldCreateNotActivityWhenNotSpecified()
        {
            Assert.AreEqual(Guid.Empty, EventActivityScope.CurrentActivityId);

            // this proxy does not create a new activity scope
            var tester = new AutomaticActivity();
            var proxy  = TracingProxy.Create <AutomaticActivity>(tester);

            proxy.Method();

            Assert.AreEqual(Guid.Empty, tester.ActivityId);

            Assert.AreEqual(Guid.Empty, EventActivityScope.CurrentActivityId);
        }
コード例 #11
0
        public void TestLoggingProxyFromVirtualClassToVirtualClass()
        {
            var logger = EventSourceImplementer.GetEventSource <VirtualCalculatorWithoutInterface>();

            _listener.EnableEvents(logger, EventLevel.LogAlways, (EventKeywords)(-1));

            // create a calculator and a proxy
            var proxy = TracingProxy.Create <VirtualCalculatorWithoutInterface>(new VirtualCalculatorWithoutInterface());

            // call the method through the proxy
            proxy.Clear();
            Assert.AreEqual(3, proxy.AddNumbers(1, 2));

            VerifyEvents(logger);
        }
コード例 #12
0
        public void TestThatInstancePointerIsPassedProperly()
        {
            Instance i = new Instance()
            {
                Value = 99
            };

            Assert.AreEqual(99, i.GetValue());

            var proxy = TracingProxy.Create <IInstance>(i);

            Assert.AreEqual(99, proxy.GetValue());

            proxy = (IInstance)TracingProxy.Create(i, typeof(IInstance));
            Assert.AreEqual(99, proxy.GetValue());
        }
コード例 #13
0
        public void CanImplementInterfaceWithGenericMethods()
        {
            // turn on logging
            var log = EventSourceImplementer.GetEventSourceAs <ITestServiceWithGenericMethods>();

            _listener.EnableEvents((EventSource)log, EventLevel.LogAlways, (EventKeywords)(-1));

            log.GetItem((int)1);
            log.GetItem((string)"s");
            log.GetItem((decimal)1);
            log.GetItem2 <int, int>((int)1, (int)2);
            log.GetItem2 <string, string>("x", "y");
            log.GetItem2 <decimal, decimal>((decimal)1, (decimal)2);
            log.GetItem3 <int, int>((int)1);
            log.GetItem3 <string, string>("x");
            log.Constrained <string>("y");

            // look at the events
            var events = _listener.Events.ToArray();

            Assert.AreEqual(9, events.Length);

            // check the individual events to make sure the data came back in the payload
            Assert.AreEqual(1, events[0].Payload.Count);
            Assert.AreEqual(1, events[1].Payload.Count);

            // create a proxy on the interface
            _listener.Reset();
            var proxy = TracingProxy.Create <ITestServiceWithGenericMethods>(new TestServiceWithGenericMethods());

            proxy.GetItem(1);
            proxy.GetItem((string)"s");
            proxy.GetItem((decimal)1);
            proxy.GetItem2 <int, int>((int)1, (int)2);
            proxy.GetItem2 <string, string>("x", "y");
            proxy.GetItem2 <decimal, decimal>((decimal)1, (decimal)2);
            proxy.GetItem3 <int, int>((int)1);
            proxy.GetItem3 <string, string>("x");
            proxy.Constrained <string>("y");

            events = _listener.Events.ToArray();
            Assert.AreEqual(18, events.Length);

            // check the individual events to make sure the data came back in the payload
            Assert.AreEqual(1, events[0].Payload.Count);
            Assert.AreEqual(1, events[1].Payload.Count);
        }
コード例 #14
0
        public void TestLoggingProxyFromClassToInterface()
        {
            // create a logger for the interface and listen on it
            var logger = EventSourceImplementer.GetEventSource <ICalculator>();

            _listener.EnableEvents(logger, EventLevel.LogAlways, (EventKeywords)(-1));

            // create a calculator and a proxy
            var proxy = TracingProxy.Create <ICalculator>(new Calculator());

            // call the method through the proxy
            proxy.Clear();
            Assert.AreEqual(3, proxy.AddNumbers(1, 2));

            // look at the events in the log
            VerifyEvents(logger);
        }
コード例 #15
0
        public void MethodThatThrowsShouldUnwindActivity()
        {
            Assert.AreEqual(Guid.Empty, EventActivityScope.CurrentActivityId);

            var tester = new AutomaticActivity();
            var proxy  = TracingProxy.Create <AutomaticActivity>(tester);

            try
            {
                proxy.Throws();
            }
            catch
            {
            }

            Assert.AreEqual(Guid.Empty, EventActivityScope.CurrentActivityId);
        }
コード例 #16
0
        public void MethodInterfaceShouldNotChangeActivity()
        {
            Assert.AreEqual(Guid.Empty, EventActivityScope.CurrentActivityId);

            using (EventActivityScope scope = new EventActivityScope())
            {
                Assert.AreNotEqual(Guid.Empty, EventActivityScope.CurrentActivityId);

                var tester = new AutomaticActivity();
                var proxy  = TracingProxy.Create <AutomaticActivity>(tester);
                proxy.Method();

                Assert.AreEqual(scope.ActivityId, tester.ActivityId);
                Assert.AreEqual(scope.ActivityId, EventActivityScope.CurrentActivityId);
            }

            Assert.AreEqual(Guid.Empty, EventActivityScope.CurrentActivityId);
        }
コード例 #17
0
        public void ClassImplementingAnInterfaceShouldSerializeData()
        {
            EventSourceImplementer.RegisterProvider <ILogInterfaceWithClassData2>(new JsonObjectSerializer());
            var logger = EventSourceImplementer.GetEventSourceAs <ILogInterfaceWithClassData2>();

            _listener.EnableEvents((EventSource)logger, EventLevel.LogAlways, (EventKeywords)(-1));

            var proxy = TracingProxy.Create <ILogInterfaceWithClassData2>(new ILogClassWithClassData());

            proxy.SendData(ClassData.Test);

            // look at the events
            var events = _listener.Events.ToArray();

            Assert.AreEqual(2, events.Length);
            Assert.AreEqual(1, events[0].EventId);
            Assert.AreEqual(ClassData.TestJson, events[0].Payload[0]);
        }
コード例 #18
0
        public void CanImplementInterfaceWithDuplicateNames()
        {
            // this was causing issues with the completed method
            var log = EventSourceImplementer.GetEventSourceAs<IHaveDuplicateNames>();
            Listener.EnableEvents((EventSource)log, EventLevel.LogAlways);

            log.Get(1);
            log.Get("s");

            // try a proxy
            var proxy = TracingProxy.Create<IHaveDuplicateNames>(new HaveDuplicateNames());
            Assert.AreEqual(2, proxy.Get(2));
            Assert.AreEqual("foo", proxy.Get("foo"));

            // look at the events
            var events = Listener.Events.ToArray();
            Assert.AreEqual(6, events.Length);
        }
コード例 #19
0
        public void AttributeShouldDetermineProvider()
        {
            var logger = EventSourceImplementer.GetEventSourceAs <ILogInterfaceWithAttribute>();

            _listener.EnableEvents((EventSource)logger, EventLevel.LogAlways);

            logger.DoSomething();

            // look at the events
            var events = _listener.Events.ToArray();

            Assert.AreEqual(1, events.Length);
            Assert.AreEqual(EventLevel.Warning, events[0].Level);

            _listener.Reset();
            var proxy = TracingProxy.Create <ILogInterfaceWithAttribute>(new LogInterfaceWithAttribute());

            try { proxy.DoSomething(); }
            catch { }

            events = _listener.Events.ToArray();
            Assert.AreEqual(2, events.Length);
            Assert.AreEqual(EventLevel.Critical, events[1].Level);
        }
コード例 #20
0
 public void ProxyingAnInvalidClassShouldThrow()
 {
     Assert.Throws <ArgumentException>(() => TracingProxy.Create <ITestServiceWithGenericMethods>(new DoesNotImplement()));
 }
コード例 #21
0
        public void CanImplementInterfaceWithReferenceParameter()
        {
            int value;

            // test the service
            var service = new TaskService();

            value = 1;
            service.GetItem(ref value);
            Assert.AreEqual(2, value);

            // log a class reference
            var data = new ReferenceData()
            {
                Data = 5
            };
            var resultData = data;

            service.GetData(ref resultData);
            Assert.AreNotEqual(data, resultData);
            Assert.AreEqual(data.Data + 1, resultData.Data);

            // turn on logging
            EventSourceImplementer.RegisterProvider <ITestServiceWithReferenceParameters>(new JsonObjectSerializer());
            var log = EventSourceImplementer.GetEventSourceAs <ITestServiceWithReferenceParameters>();

            _listener.EnableEvents((EventSource)log, EventLevel.LogAlways, (EventKeywords)(-1));

            // log a built-in type reference
            value = 1;
            log.GetItem(ref value);

            // log a class reference
            data = new ReferenceData()
            {
                Data = 5
            };
            resultData = data;
            log.GetData(ref resultData);

            // create a proxy
            var proxy = TracingProxy.Create <ITestServiceWithReferenceParameters>(service);

            // proxy a built-in type reference
            value = 1;
            proxy.GetItem(ref value);
            Assert.AreEqual(2, value);

            // proxy a class reference
            data = new ReferenceData()
            {
                Data = 7
            };
            resultData = data;
            proxy.GetData(ref resultData);
            Assert.AreNotEqual(data, resultData);
            Assert.AreEqual(data.Data + 1, resultData.Data);

            // look at the events
            var events = _listener.Events.ToArray();

            Assert.AreEqual(6, events.Length);

            // check the individual events to make sure the data came back in the payload
            Assert.AreEqual(1, events[0].Payload[0]);
            Assert.AreEqual(new JsonObjectSerializer().SerializeObject(data, null), events[4].Payload[0]);
        }