コード例 #1
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]);
        }
コード例 #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 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);
        }
コード例 #4
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!"));
        }
コード例 #5
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();
        }
コード例 #6
0
        public void AbstractClassWithExternalEnumsCanBeImplemented()
        {
            var testLog = EventSourceImplementer.GetEventSourceAs <ITestLogWithExternalEnums>();

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

            // do some logging
            testLog.Event("hello, world!");

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

            Assert.AreEqual(1, events.Length);

            // check the event source
            // make sure that the EventSource attribute makes it to the event source
            var eventSource = events[0].EventSource;

            Assert.AreEqual("ITestLogWithExternalEnums", eventSource.Name);

            // check the individual events
            Assert.AreEqual(testLog, events[0].EventSource);
            Assert.AreEqual(19, events[0].EventId);
            Assert.AreEqual("Event: {0}", events[0].Message);
            Assert.AreEqual(EventLevel.Informational, events[0].Level);
            Assert.AreEqual(1, events[0].Payload.Count);
            Assert.AreEqual("hello, world!", events[0].Payload[0]);
        }
コード例 #7
0
        public void RegisterProviderShouldOverrideAttribute()
        {
            EventSourceImplementer.RegisterProvider <ILogInterfaceWithAttribute2>(new JsonObjectSerializer(EventLevel.Verbose));
            var logger = EventSourceImplementer.GetEventSourceAs <ILogInterfaceWithAttribute2>();

            _listener.EnableEvents((EventSource)logger, EventLevel.Informational, (EventKeywords)(-1));
            logger.SendData(ClassData.Test);

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

            Assert.AreEqual(1, events.Length);
            Assert.AreEqual(1, events[0].EventId);
            Assert.AreEqual(null, events[0].Payload[0]);

            _listener.Reset();
            _listener.EnableEvents((EventSource)logger, EventLevel.Verbose, (EventKeywords)(-1));
            logger.SendData(ClassData.Test);

            // look at the events
            events = _listener.Events.ToArray();
            Assert.AreEqual(1, events.Length);
            Assert.AreEqual(1, events[0].EventId);
            Assert.AreEqual(ClassData.TestJson, events[0].Payload[0]);
        }
コード例 #8
0
        protected void EnableLogging <TLog>() where TLog : class
        {
            // create the logger and make sure it is serializing the parameters properly
            var logger = EventSourceImplementer.GetEventSource <TLog>();

            _listener.EnableEvents(logger, EventLevel.LogAlways);
        }
コード例 #9
0
        public void TestAddingContextToEachMethod()
        {
            TraceParameterProvider.Default.For <IHaveRichContext>()
            .AddContextData <string>("Site.Id")
            .AddContextData <string>("Site.Name")
            .AddContextData <int>("PID");

            var proxy = EventSourceImplementer.GetEventSourceAs <IHaveRichContext>();

            EnableLogging(proxy);

            using (var context = TraceContext.Begin())
            {
                context["PID"] = 1234;

                using (var context2 = TraceContext.Begin())
                {
                    context2["Site.Id"]   = "Ts1";
                    context2["Site.Name"] = "TestSite1";

                    proxy.Log("message");
                }
            }

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

            Assert.AreEqual(1, events.Length);
            Assert.AreEqual(4, events[0].Payload.Count);
            Assert.AreEqual("message", events[0].Payload[0]);
            Assert.AreEqual("Ts1", events[0].Payload[1]);
            Assert.AreEqual("TestSite1", events[0].Payload[2]);
            Assert.AreEqual(1234, events[0].Payload[3]);
        }
コード例 #10
0
        public void EventSourceThrowsConfigured()
        {
            var subject = EventSourceImplementer.GetEventSourceAs <IThrowsLog>();
            var actual  = (((EventSource)subject).Settings & EventSourceSettings.ThrowOnEventWriteErrors) != 0;

            Assert.IsTrue(actual);
        }
コード例 #11
0
        public void TestLoggingObjectsWithNoDefaultConstructor()
        {
            var testLog = EventSourceImplementer.GetEventSourceAs<IMessageNoDefaultConstructor>();
            Listener.EnableEvents((EventSource)testLog, EventLevel.LogAlways, (EventKeywords)(-1));

            testLog.Sample(new ClassNoDefaultConstructor(1), new StructNoDefaultConstructor(1));
        }
コード例 #12
0
        public void RegisterProviderTwiceShouldFail()
        {
            var contextProvider = new MyTraceContextProvider();

            EventSourceImplementer.RegisterProvider <ILog2>(contextProvider);
            Assert.Throws <InvalidOperationException>(() => EventSourceImplementer.RegisterProvider <ILog>(contextProvider));
        }
コード例 #13
0
 public void EventSourceThrowsConfigured()
 {
     var subject = EventSourceImplementer.GetEventSourceAs<IThrowsLog>();
     var propertyInfo = typeof(EventSource).GetProperty( "ThrowOnEventWriteErrors", BindingFlags.Instance | BindingFlags.NonPublic );
     var actual = propertyInfo == null || (bool) propertyInfo.GetValue( subject );
     Assert.IsTrue( actual );
 }
コード例 #14
0
        public void DerivedAgainInterfaceCanBeImplemented()
        {
            var testLog = EventSourceImplementer.GetEventSourceAs<IJustAnInterfaceDerivedAgain>();
            Listener.EnableEvents((EventSource)testLog, EventLevel.LogAlways);

            // do some logging
            testLog.AddNumbers(2, 3);
            testLog.SubtractNumbers(4, 5);

            // look at the events
            var events = Listener.Events.ToArray();
            Assert.AreEqual(2, events.Length);

            // check the event source
            // make sure that the EventSource attribute makes it to the event source
            var eventSource = events[0].EventSource;
            Assert.AreEqual("IJustAnInterfaceDerivedAgain", eventSource.Name);

            // check the individual events
            Assert.AreEqual(testLog, events[0].EventSource);
            Assert.AreEqual(1, events[0].EventId);
            Assert.AreEqual("{0} {1}", events[0].Message);
            Assert.AreEqual(EventLevel.Informational, events[0].Level);
            Assert.AreEqual(2, events[0].Payload.Count);
            Assert.AreEqual(2, events[0].Payload[0]);
            Assert.AreEqual(3, events[0].Payload[1]);

            Assert.AreEqual(testLog, events[1].EventSource);
            Assert.AreEqual(4, events[1].EventId);
            Assert.AreEqual("{0} {1}", events[1].Message);
            Assert.AreEqual(EventLevel.Informational, events[1].Level);
            Assert.AreEqual(2, events[1].Payload.Count);
            Assert.AreEqual(4, events[1].Payload[0]);
            Assert.AreEqual(5, events[1].Payload[1]);
        }
コード例 #15
0
        public void EventSourceThrowsConfigured()
        {
            var subject   = EventSourceImplementer.GetEventSourceAs <IThrowsLog>();
            var fieldInfo = typeof(EventSource).GetField("m_throwOnEventWriteErrors", BindingFlags.Instance | BindingFlags.NonPublic);
            var actual    = (bool)fieldInfo.GetValue(subject);

            Assert.IsTrue(actual);
        }
コード例 #16
0
        public void RegisterProviderAfterSourceCreationShouldFail()
        {
            var log = EventSourceImplementer.GetEventSource <ILog3>();

            var contextProvider = new MyTraceContextProvider();

            Assert.Throws <InvalidOperationException>(() => EventSourceImplementer.RegisterProvider <ILog>(contextProvider));
        }
コード例 #17
0
            static MyTestEventSource()
            {
                TraceParameterProvider.Default.ForAnything()
                .With <Exception>()
                .Trace(e => e.Message).As("ExceptionMessage")
                .Trace(e => e.StackTrace);

                Log = EventSourceImplementer.GetEventSourceAs <MyTestEventSource>();
            }
コード例 #18
0
        public static T EventSourceFor <TLogger>()
        {
            var type = typeof(TLogger);

            lock (_eventSources)
            {
                return(_eventSources.GetOrAdd(type, t => EventSourceImplementer.GetLog <TLogger, T>()));
            }
        }
コード例 #19
0
        public void TestRenamingInterface()
        {
            EventSourceImplementationAttribute.For <IHateThisName>(new EventSourceImplementationAttribute()
            {
                Name = "A Better Name"
            });
            var logger = EventSourceImplementer.GetEventSourceAs <IHateThisName>();

            Assert.AreEqual("A Better Name", ((EventSource)logger).Name);
        }
コード例 #20
0
        public static T LogFor <TLogger>()
        {
            var type = typeof(TLogger);

            lock (_eventSources)
            {
                return(_eventSources.GetOrAdd(type, t => EventSourceImplementer.GetLog <TLogger, T>()));
            }
            //var logger = EventSourceImplementer.GetEventSource(typeof(T), typeof(TLogger).FullName);
            //return (T)(object)logger;
        }
コード例 #21
0
        public void BuiltInSerializedTypesCanBeLoggedInDirectMethods()
        {
            var listener = new TestEventListener();
            var testLog  = EventSourceImplementer.GetEventSourceAs <TypeLogWithSerializedTypesInDirectMethod>();

            listener.EnableEvents(testLog, EventLevel.LogAlways);

            testLog.LogIntPtr(new IntPtr(1234)); Assert.AreEqual("1234", listener.Events.Last().Payload[0].ToString());
            testLog.LogChar('c'); Assert.AreEqual("c", listener.Events.Last().Payload[0].ToString());
            testLog.LogDecimal(3.456m); Assert.AreEqual("3.456", listener.Events.Last().Payload[0].ToString());
        }
コード例 #22
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);
        }
コード例 #23
0
        public void TestMessageFormatting()
        {
            var testLog = EventSourceImplementer.GetEventSourceAs<IHaveMessageFormatting>();
            Listener.EnableEvents((EventSource)testLog, EventLevel.LogAlways, (EventKeywords)(-1));

            testLog.StartJobManually(new DateTime(2015, 4, 1));

            // look at the events
            var events = Listener.Events.ToArray();
            Assert.AreEqual(1, events.Length);
            Assert.AreEqual("2015-04-01 00:00:00Z", events[0].Payload[0]);
        }
コード例 #24
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]);
        }
コード例 #25
0
        public void BadExpressionGeneratesMeaningfulException()
        {
            EnableLogging <ILogEmailChangesWithBadExpressionTPP>();

            // do some logging
            var log    = EventSourceImplementer.GetEventSourceAs <ILogEmailChangesWithBadExpressionTPP>();
            var change = new EmailChange()
            {
                From = "me", To = "you", When = new DateTime(2010, 1, 1)
            };

            log.LogChange(change);
        }
コード例 #26
0
        /// <summary>
        /// Initializes a new instance of the TracingProxyImplementer class.
        /// </summary>
        /// <param name="executeType">The type of object to execute.</param>
        /// <param name="logType">The type of object to log.</param>
        /// <param name="callWithActivityScope">True to generate a proxy that wraps an activity scope around all calls.</param>
        public TracingProxyImplementer(Type executeType, Type logType, bool callWithActivityScope)
        {
            _executeType           = executeType;
            _callWithActivityScope = callWithActivityScope;
            _serializationProvider = TraceSerializationProvider.GetSerializationProvider(logType);

            // create a log of the given log type
            var log = EventSourceImplementer.GetEventSource(logType);

            _logType = log.GetType();

            CreateMethod = ImplementProxy(log);
        }
コード例 #27
0
        public void CanReturnEmptySourceList()
        {
            EnableLogging <InterfaceWithEmptyProvider>();

            var log = EventSourceImplementer.GetEventSourceAs <InterfaceWithEmptyProvider>();

            log.TraceAsData("p1", "p2");

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

            Assert.AreEqual(1, events.Length);
            Assert.AreEqual(0, events[0].Payload.Count);
        }
コード例 #28
0
        public void ContextCanBeControlledByMethodAttribute()
        {
            var testLog = EventSourceImplementer.GetEventSourceAs <ILogWithAttributeDisabledAndMethodEnabled>();

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

            testLog.DoSomething();

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

            Assert.AreEqual(1, events.Length);
            Assert.AreEqual(1, events[0].Payload.Count);
        }
コード例 #29
0
        public void ProviderCanBeSpecifiedByAttribute()
        {
            var testLog = EventSourceImplementer.GetEventSourceAs <ILogWithProviderAttribute>();

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

            testLog.DoSomething();

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

            Assert.AreEqual(1, events.Length);
            Assert.AreEqual("context", events[0].Payload[0]);
        }
コード例 #30
0
        public void AbstractClassCanBeImplemented()
        {
            var testLog = EventSourceImplementer.GetEventSourceAs <TestLog>();

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

            // do some logging
            testLog.Failure("whoops!");
            testLog.Other("doing something", 19);
            testLog.Direct("factory direct");

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

            Assert.AreEqual(3, events.Length);

            // check the event source
            // make sure that the EventSource attribute makes it to the event source
            var eventSource = events[0].EventSource;

            Assert.AreEqual("TestLog", eventSource.Name);
            Assert.AreEqual(new Guid("0469abfa-1bb2-466a-b645-e3e15a02f3ff"), eventSource.Guid);

            // check the individual events
            Assert.AreEqual(testLog, events[0].EventSource);
            Assert.AreEqual(1, events[0].EventId);
            Assert.AreEqual("Application Failure: {0}", events[0].Message);
            Assert.AreEqual(EventLevel.Error, events[0].Level);
            Assert.IsTrue(events[0].Keywords.HasFlag(TestLog.Keywords.Diagnostic));
            Assert.AreEqual(TestLog.Tasks.DBQuery, events[0].Task);
            Assert.AreEqual(1, events[0].Payload.Count);
            Assert.AreEqual("whoops!", events[0].Payload[0]);

            Assert.AreEqual(testLog, events[1].EventSource);
            Assert.AreEqual(4, events[1].EventId);
            Assert.AreEqual("Other Information: {0} {1}", events[1].Message);
            Assert.AreEqual(EventLevel.Informational, events[1].Level);
            Assert.IsTrue(events[1].Keywords.HasFlag(TestLog.Keywords.Perf));
            Assert.AreEqual(2, events[1].Payload.Count);
            Assert.AreEqual("doing something", events[1].Payload[0]);
            Assert.AreEqual(19, events[1].Payload[1]);

            Assert.AreEqual(testLog, events[2].EventSource);
            Assert.AreEqual(5, events[2].EventId);
            Assert.AreEqual("Direct Call: {0}", events[2].Message);
            Assert.AreEqual(EventLevel.Warning, events[2].Level);
            Assert.IsTrue(events[2].Keywords.HasFlag(TestLog.Keywords.Diagnostic));
            Assert.AreEqual(1, events[2].Payload.Count);
            Assert.AreEqual("factory direct", events[2].Payload[0]);
        }