コード例 #1
0
        public void TestMessages()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
                using (var diagnosticSourceListener = new DiagnosticListener("TestMessagesSource"))
                {
                    Assert.Equal(0, eventSourceListener.EventCount);

                    // This is just to make debugging easier.
                    var messages = new List <string>();

                    eventSourceListener.OtherEventWritten += delegate(EventWrittenEventArgs evnt)
                    {
                        if (evnt.EventName == "Message")
                        {
                            var message = (string)evnt.Payload[0];
                            messages.Add(message);
                        }
                    };

                    // This has a syntax error in the Url case, so it should be ignored.
                    eventSourceListener.Enable("TestMessagesSource/TestEvent1:-cls.Url");
                    Assert.Equal(0, eventSourceListener.EventCount);
                    Assert.True(3 <= messages.Count);
                }
        }
コード例 #2
0
        public void TestNoImplicitTransforms()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
                using (var diagnosticSourceListener = new DiagnosticListener("TestNoImplicitTransformsSource"))
                {
                    Assert.Equal(0, eventSourceListener.EventCount);

                    // use the - prefix to suppress the implicit properties.  Thus you should only get propStr and Url.
                    eventSourceListener.Enable("TestNoImplicitTransformsSource/TestEvent1:-propStr;cls.Url");

                    /***************************************************************************************/
                    // Emit an event that matches the first pattern.
                    MyClass val = new MyClass()
                    {
                        Url = "MyUrl", Point = new MyPoint()
                        {
                            X = 3, Y = 5
                        }
                    };
                    if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    {
                        diagnosticSourceListener.Write("TestEvent1", new { propStr = "hi", propInt = 4, cls = val, propStr2 = "there" });
                    }

                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("TestNoImplicitTransformsSource", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(2, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal("hi", eventSourceListener.LastEvent.Arguments["propStr"]);
                    Assert.Equal("MyUrl", eventSourceListener.LastEvent.Arguments["Url"]);
                    eventSourceListener.ResetEventCountAndLastEvent();
                }
        }
コード例 #3
0
        public void TestBadProperties()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
                using (var diagnosticSourceListener = new DiagnosticListener("TestBadPropertiesSource"))
                {
                    Assert.Equal(0, eventSourceListener.EventCount);

                    // This has a syntax error in the Url case, so it should be ignored.
                    eventSourceListener.Enable("TestBadPropertiesSource/TestEvent1:cls.Ur-+l");

                    /***************************************************************************************/
                    // Emit an event that matches the first pattern.
                    MyClass val = new MyClass()
                    {
                        Url = "MyUrl", Point = new MyPoint()
                        {
                            X = 3, Y = 5
                        }
                    };
                    if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    {
                        diagnosticSourceListener.Write("TestEvent1", new { propStr = "hi", propInt = 4, cls = val });
                    }

                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("TestBadPropertiesSource", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(3, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal(val.GetType().FullName, eventSourceListener.LastEvent.Arguments["cls"]); // ToString on cls is the class name
                    Assert.Equal("hi", eventSourceListener.LastEvent.Arguments["propStr"]);
                    Assert.Equal("4", eventSourceListener.LastEvent.Arguments["propInt"]);
                    eventSourceListener.ResetEventCountAndLastEvent();
                }
        }
コード例 #4
0
        public void TestWildCardEventName()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
                using (var diagnosticSourceListener = new DiagnosticListener("TestWildCardEventNameSource"))
                {
                    Assert.Equal(0, eventSourceListener.EventCount);

                    // Turn on events with both implicit and explicit types
                    eventSourceListener.Enable("TestWildCardEventNameSource");

                    /***************************************************************************************/
                    // Emit an event, check that all implicit properties are generated
                    MyClass val = new MyClass()
                    {
                        Url = "MyUrl", Point = new MyPoint()
                        {
                            X = 3, Y = 5
                        }
                    };
                    if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    {
                        diagnosticSourceListener.Write("TestEvent1", new { propStr = "hi", propInt = 4, cls = val });
                    }

                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("TestWildCardEventNameSource", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(2, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal("hi", eventSourceListener.LastEvent.Arguments["propStr"]);
                    Assert.Equal("4", eventSourceListener.LastEvent.Arguments["propInt"]);
                    eventSourceListener.ResetEventCountAndLastEvent();

                    /***************************************************************************************/
                    // Emit the same event, with a different set of implicit properties
                    if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    {
                        diagnosticSourceListener.Write("TestEvent1", new { propStr2 = "hi2", cls = val });
                    }

                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("TestWildCardEventNameSource", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(1, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal("hi2", eventSourceListener.LastEvent.Arguments["propStr2"]);
                    eventSourceListener.ResetEventCountAndLastEvent();

                    /***************************************************************************************/
                    // Emit an event from another diagnostic source with the same event name.
                    // It will be filtered out.
                    using (var diagnosticSourceListener2 = new DiagnosticListener("TestWildCardEventNameSource2"))
                    {
                        if (diagnosticSourceListener2.IsEnabled("TestEvent1"))
                        {
                            diagnosticSourceListener2.Write("TestEvent1", new { propStr = "hi", propInt = 4, cls = val });
                        }
                    }
                    Assert.Equal(0, eventSourceListener.EventCount);    // No Event should be fired.
                }
        }
コード例 #5
0
        public void TestWildCardSourceName()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
                using (var diagnosticSourceListener1 = new DiagnosticListener("TestWildCardSourceName1"))
                    using (var diagnosticSourceListener2 = new DiagnosticListener("TestWildCardSourceName2"))
                    {
                        eventSourceListener.Filter = (DiagnosticSourceEvent evnt) => evnt.SourceName.StartsWith("TestWildCardSourceName");

                        // Turn On Everything.  Note that because of concurrent testing, we may get other sources as well.
                        // but we filter them out because we set eventSourceListener.Filter.
                        eventSourceListener.Enable("");

                        Assert.True(diagnosticSourceListener1.IsEnabled("TestEvent1"));
                        Assert.True(diagnosticSourceListener1.IsEnabled("TestEvent2"));
                        Assert.True(diagnosticSourceListener2.IsEnabled("TestEvent1"));
                        Assert.True(diagnosticSourceListener2.IsEnabled("TestEvent2"));

                        Assert.Equal(0, eventSourceListener.EventCount);

                        diagnosticSourceListener1.Write("TestEvent1", new { prop111 = "prop111Val", prop112 = 112 });
                        Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                        Assert.Equal("TestWildCardSourceName1", eventSourceListener.LastEvent.SourceName);
                        Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                        Assert.Equal(2, eventSourceListener.LastEvent.Arguments.Count);
                        Assert.Equal("prop111Val", eventSourceListener.LastEvent.Arguments["prop111"]);
                        Assert.Equal("112", eventSourceListener.LastEvent.Arguments["prop112"]);
                        eventSourceListener.ResetEventCountAndLastEvent();

                        diagnosticSourceListener1.Write("TestEvent2", new { prop121 = "prop121Val", prop122 = 122 });
                        Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                        Assert.Equal("TestWildCardSourceName1", eventSourceListener.LastEvent.SourceName);
                        Assert.Equal("TestEvent2", eventSourceListener.LastEvent.EventName);
                        Assert.Equal(2, eventSourceListener.LastEvent.Arguments.Count);
                        Assert.Equal("prop121Val", eventSourceListener.LastEvent.Arguments["prop121"]);
                        Assert.Equal("122", eventSourceListener.LastEvent.Arguments["prop122"]);
                        eventSourceListener.ResetEventCountAndLastEvent();

                        diagnosticSourceListener2.Write("TestEvent1", new { prop211 = "prop211Val", prop212 = 212 });
                        Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                        Assert.Equal("TestWildCardSourceName2", eventSourceListener.LastEvent.SourceName);
                        Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                        Assert.Equal(2, eventSourceListener.LastEvent.Arguments.Count);
                        Assert.Equal("prop211Val", eventSourceListener.LastEvent.Arguments["prop211"]);
                        Assert.Equal("212", eventSourceListener.LastEvent.Arguments["prop212"]);
                        eventSourceListener.ResetEventCountAndLastEvent();

                        diagnosticSourceListener2.Write("TestEvent2", new { prop221 = "prop221Val", prop222 = 122 });
                        Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                        Assert.Equal("TestWildCardSourceName2", eventSourceListener.LastEvent.SourceName);
                        Assert.Equal("TestEvent2", eventSourceListener.LastEvent.EventName);
                        Assert.Equal(2, eventSourceListener.LastEvent.Arguments.Count);
                        Assert.Equal("prop221Val", eventSourceListener.LastEvent.Arguments["prop221"]);
                        Assert.Equal("122", eventSourceListener.LastEvent.Arguments["prop222"]);
                        eventSourceListener.ResetEventCountAndLastEvent();
                    }
        }
コード例 #6
0
        public void TestNoImplicitTransforms()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
            using (var diagnosticSourceListener = new DiagnosticListener("TestNoImplicitTransformsSource"))
            {
                Assert.Equal(0, eventSourceListener.EventCount);

                // use the - prefix to suppress the implicit properties.  Thus you should only get propStr and Url.  
                eventSourceListener.Enable("TestNoImplicitTransformsSource/TestEvent1:-propStr;cls.Url");

                /***************************************************************************************/
                // Emit an event that matches the first pattern. 
                MyClass val = new MyClass() { Url = "MyUrl", Point = new MyPoint() { X = 3, Y = 5 } };
                if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    diagnosticSourceListener.Write("TestEvent1", new { propStr = "hi", propInt = 4, cls = val, propStr2 = "there" });

                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("TestNoImplicitTransformsSource", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                Assert.Equal(2, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("hi", eventSourceListener.LastEvent.Arguments["propStr"]);
                Assert.Equal("MyUrl", eventSourceListener.LastEvent.Arguments["Url"]);
                eventSourceListener.ResetEventCountAndLastEvent();
            }
        }
コード例 #7
0
        public void TestNulls()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
            using (var diagnosticSourceListener = new DiagnosticListener("TestNullsTestSource"))
            {
                Assert.Equal(0, eventSourceListener.EventCount);

                // Turn on events with both implicit and explicit types 
                eventSourceListener.Enable("TestNullsTestSource/TestEvent1:cls.Url;cls_Point_X=cls.Point.X");

                /***************************************************************************************/
                // Emit a null arguments object. 

                if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    diagnosticSourceListener.Write("TestEvent1", null);

                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("TestNullsTestSource", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                Assert.Equal(0, eventSourceListener.LastEvent.Arguments.Count);
                eventSourceListener.ResetEventCountAndLastEvent();

                /***************************************************************************************/
                // Emit an arguments object with nulls in it.   

                MyClass val = null;
                string strVal = null;
                if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    diagnosticSourceListener.Write("TestEvent1", new { cls = val, propStr = "propVal1", propStrNull = strVal });

                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("TestNullsTestSource", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                Assert.Equal(2, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("propVal1", eventSourceListener.LastEvent.Arguments["propStr"]);
                Assert.Equal("", eventSourceListener.LastEvent.Arguments["propStrNull"]);   // null strings get turned into empty strings
                eventSourceListener.ResetEventCountAndLastEvent();

                /***************************************************************************************/
                // Emit an arguments object that points at null things

                MyClass val1 = new MyClass() { Url = "myUrlVal", Point = null };
                if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    diagnosticSourceListener.Write("TestEvent1", new { cls = val1, propStr = "propVal1" });

                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("TestNullsTestSource", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                Assert.Equal(2, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("propVal1", eventSourceListener.LastEvent.Arguments["propStr"]);
                Assert.Equal("myUrlVal", eventSourceListener.LastEvent.Arguments["Url"]);
                eventSourceListener.ResetEventCountAndLastEvent();

                /***************************************************************************************/
                // Emit an arguments object that points at null things (variation 2)

                MyClass val2 = new MyClass() { Url = null, Point = new MyPoint() { X = 8, Y = 9 } };
                if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    diagnosticSourceListener.Write("TestEvent1", new { cls = val2, propStr = "propVal1" });

                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("TestNullsTestSource", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                Assert.Equal(2, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("propVal1", eventSourceListener.LastEvent.Arguments["propStr"]);
                Assert.Equal("8", eventSourceListener.LastEvent.Arguments["cls_Point_X"]);
                eventSourceListener.ResetEventCountAndLastEvent();
            }
        }
コード例 #8
0
        public void TestSpecificEvents()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
            using (var diagnosticSourceListener = new DiagnosticListener("TestSpecificEventsSource"))
            {
                Assert.Equal(0, eventSourceListener.EventCount);

                // Turn on events with both implicit and explicit types You can have whitespace 
                // before and after each spec.  
                eventSourceListener.Enable(
                    "  TestSpecificEventsSource/TestEvent1:cls_Point_X=cls.Point.X;cls_Point_Y=cls.Point.Y\r\n" +
                    "  TestSpecificEventsSource/TestEvent2:cls_Url=cls.Url\r\n"
                    );

                /***************************************************************************************/
                // Emit an event that matches the first pattern. 
                MyClass val = new MyClass() { Url = "MyUrl", Point = new MyPoint() { X = 3, Y = 5 } };
                if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    diagnosticSourceListener.Write("TestEvent1", new { propStr = "hi", propInt = 4, cls = val });

                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("TestSpecificEventsSource", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                Assert.Equal(4, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("hi", eventSourceListener.LastEvent.Arguments["propStr"]);
                Assert.Equal("4", eventSourceListener.LastEvent.Arguments["propInt"]);
                Assert.Equal("3", eventSourceListener.LastEvent.Arguments["cls_Point_X"]);
                Assert.Equal("5", eventSourceListener.LastEvent.Arguments["cls_Point_Y"]);
                eventSourceListener.ResetEventCountAndLastEvent();

                /***************************************************************************************/
                // Emit an event that matches the second pattern. 
                if (diagnosticSourceListener.IsEnabled("TestEvent2"))
                    diagnosticSourceListener.Write("TestEvent2", new { prop2Str = "hello", prop2Int = 8, cls = val });

                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.  
                Assert.Equal("TestSpecificEventsSource", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestEvent2", eventSourceListener.LastEvent.EventName);
                Assert.Equal(3, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("hello", eventSourceListener.LastEvent.Arguments["prop2Str"]);
                Assert.Equal("8", eventSourceListener.LastEvent.Arguments["prop2Int"]);
                Assert.Equal("MyUrl", eventSourceListener.LastEvent.Arguments["cls_Url"]);
                eventSourceListener.ResetEventCountAndLastEvent();

                // Emit an event that does not match either pattern.  (thus will be filtered out)
                if (diagnosticSourceListener.IsEnabled("TestEvent3"))
                    diagnosticSourceListener.Write("TestEvent3", new { propStr = "prop3", });
                Assert.Equal(0, eventSourceListener.EventCount);        // No Event should be fired.  

                /***************************************************************************************/
                // Emit an event from another diagnostic source with the same event name.  
                // It will be filtered out.  
                using (var diagnosticSourceListener2 = new DiagnosticListener("TestSpecificEventsSource2"))
                {
                    if (diagnosticSourceListener2.IsEnabled("TestEvent1"))
                        diagnosticSourceListener2.Write("TestEvent1", new { propStr = "hi", propInt = 4, cls = val });
                }
                Assert.Equal(0, eventSourceListener.EventCount);        // No Event should be fired.  

                // Disable all the listener and insure that no more events come through.  
                eventSourceListener.Disable();

                diagnosticSourceListener.Write("TestEvent1", null);
                diagnosticSourceListener.Write("TestEvent2", null);

                Assert.Equal(0, eventSourceListener.EventCount);        // No Event should be received.  
            }

            // Make sure that there are no Diagnostic Listeners left over.  
            DiagnosticListener.AllListeners.Subscribe(DiagnosticSourceTest.MakeObserver(delegate (DiagnosticListener listen)
            {
                Assert.True(!listen.Name.StartsWith("BuildTestSource"));
            }));
        }
コード例 #9
0
        public void TestWildCardEventName()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
            using (var diagnosticSourceListener = new DiagnosticListener("TestWildCardEventNameSource"))
            {
                Assert.Equal(0, eventSourceListener.EventCount);

                // Turn on events with both implicit and explicit types 
                eventSourceListener.Enable("TestWildCardEventNameSource");

                /***************************************************************************************/
                // Emit an event, check that all implicit properties are generated
                MyClass val = new MyClass() { Url = "MyUrl", Point = new MyPoint() { X = 3, Y = 5 } };
                if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    diagnosticSourceListener.Write("TestEvent1", new { propStr = "hi", propInt = 4, cls = val });

                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("TestWildCardEventNameSource", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                Assert.Equal(2, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("hi", eventSourceListener.LastEvent.Arguments["propStr"]);
                Assert.Equal("4", eventSourceListener.LastEvent.Arguments["propInt"]);
                eventSourceListener.ResetEventCountAndLastEvent();

                /***************************************************************************************/
                // Emit the same event, with a different set of implicit properties 
                if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    diagnosticSourceListener.Write("TestEvent1", new { propStr2 = "hi2", cls = val });

                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("TestWildCardEventNameSource", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                Assert.Equal(1, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("hi2", eventSourceListener.LastEvent.Arguments["propStr2"]);
                eventSourceListener.ResetEventCountAndLastEvent();

                /***************************************************************************************/
                // Emit an event from another diagnostic source with the same event name.  
                // It will be filtered out.  
                using (var diagnosticSourceListener2 = new DiagnosticListener("TestWildCardEventNameSource2"))
                {
                    if (diagnosticSourceListener2.IsEnabled("TestEvent1"))
                        diagnosticSourceListener2.Write("TestEvent1", new { propStr = "hi", propInt = 4, cls = val });
                }
                Assert.Equal(0, eventSourceListener.EventCount);        // No Event should be fired.  
            }
        }
コード例 #10
0
        public void TestWildCardSourceName()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
            using (var diagnosticSourceListener1 = new DiagnosticListener("TestWildCardSourceName1"))
            using (var diagnosticSourceListener2 = new DiagnosticListener("TestWildCardSourceName2"))
            {
                eventSourceListener.Filter = (DiagnosticSourceEvent evnt) => evnt.SourceName.StartsWith("TestWildCardSourceName");

                // Turn On Everything.  Note that because of concurrent testing, we may get other sources as well.
                // but we filter them out because we set eventSourceListener.Filter.   
                eventSourceListener.Enable("");

                Assert.True(diagnosticSourceListener1.IsEnabled("TestEvent1"));
                Assert.True(diagnosticSourceListener1.IsEnabled("TestEvent2"));
                Assert.True(diagnosticSourceListener2.IsEnabled("TestEvent1"));
                Assert.True(diagnosticSourceListener2.IsEnabled("TestEvent2"));

                Assert.Equal(0, eventSourceListener.EventCount);

                diagnosticSourceListener1.Write("TestEvent1", new { prop111 = "prop111Val", prop112 = 112 });
                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("TestWildCardSourceName1", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                Assert.Equal(2, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("prop111Val", eventSourceListener.LastEvent.Arguments["prop111"]);
                Assert.Equal("112", eventSourceListener.LastEvent.Arguments["prop112"]);
                eventSourceListener.ResetEventCountAndLastEvent();

                diagnosticSourceListener1.Write("TestEvent2", new { prop121 = "prop121Val", prop122 = 122 });
                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("TestWildCardSourceName1", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestEvent2", eventSourceListener.LastEvent.EventName);
                Assert.Equal(2, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("prop121Val", eventSourceListener.LastEvent.Arguments["prop121"]);
                Assert.Equal("122", eventSourceListener.LastEvent.Arguments["prop122"]);
                eventSourceListener.ResetEventCountAndLastEvent();

                diagnosticSourceListener2.Write("TestEvent1", new { prop211 = "prop211Val", prop212 = 212 });
                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("TestWildCardSourceName2", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                Assert.Equal(2, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("prop211Val", eventSourceListener.LastEvent.Arguments["prop211"]);
                Assert.Equal("212", eventSourceListener.LastEvent.Arguments["prop212"]);
                eventSourceListener.ResetEventCountAndLastEvent();

                diagnosticSourceListener2.Write("TestEvent2", new { prop221 = "prop221Val", prop222 = 122 });
                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("TestWildCardSourceName2", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestEvent2", eventSourceListener.LastEvent.EventName);
                Assert.Equal(2, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("prop221Val", eventSourceListener.LastEvent.Arguments["prop221"]);
                Assert.Equal("122", eventSourceListener.LastEvent.Arguments["prop222"]);
                eventSourceListener.ResetEventCountAndLastEvent();
            }
        }
コード例 #11
0
        public void LinuxNewLineConventions()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
                using (var diagnosticSourceListener = new DiagnosticListener("LinuxNewLineConventionsSource"))
                {
                    Assert.Equal(0, eventSourceListener.EventCount);

                    // Turn on events with both implicit and explicit types You can have whitespace
                    // before and after each spec.   Use \n rather than \r\n
                    eventSourceListener.Enable(
                        "  LinuxNewLineConventionsSource/TestEvent1:-cls_Point_X=cls.Point.X\n" +
                        "  LinuxNewLineConventionsSource/TestEvent2:-cls_Url=cls.Url\n"
                        );

                    /***************************************************************************************/
                    // Emit an event that matches the first pattern.
                    MyClass val = new MyClass()
                    {
                        Url = "MyUrl", Point = new MyPoint()
                        {
                            X = 3, Y = 5
                        }
                    };
                    if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    {
                        diagnosticSourceListener.Write("TestEvent1", new { propStr = "hi", propInt = 4, cls = val });
                    }

                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("LinuxNewLineConventionsSource", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(1, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal("3", eventSourceListener.LastEvent.Arguments["cls_Point_X"]);
                    eventSourceListener.ResetEventCountAndLastEvent();

                    /***************************************************************************************/
                    // Emit an event that matches the second pattern.
                    if (diagnosticSourceListener.IsEnabled("TestEvent2"))
                    {
                        diagnosticSourceListener.Write("TestEvent2", new { prop2Str = "hello", prop2Int = 8, cls = val });
                    }

                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("LinuxNewLineConventionsSource", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestEvent2", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(1, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal("MyUrl", eventSourceListener.LastEvent.Arguments["cls_Url"]);
                    eventSourceListener.ResetEventCountAndLastEvent();

                    // Emit an event that does not match either pattern.  (thus will be filtered out)
                    if (diagnosticSourceListener.IsEnabled("TestEvent3"))
                    {
                        diagnosticSourceListener.Write("TestEvent3", new { propStr = "prop3", });
                    }
                    Assert.Equal(0, eventSourceListener.EventCount);    // No Event should be fired.
                }

            // Make sure that there are no Diagnostic Listeners left over.
            DiagnosticListener.AllListeners.Subscribe(DiagnosticSourceTest.MakeObserver(delegate(DiagnosticListener listen)
            {
                Assert.True(!listen.Name.StartsWith("BuildTestSource"));
            }));
        }
コード例 #12
0
        public void TestActivities()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
            using (var diagnosticSourceListener = new DiagnosticListener("TestActivitiesSource"))
            {
                Assert.Equal(0, eventSourceListener.EventCount);
                eventSourceListener.Enable(
                    "TestActivitiesSource/TestActivity1Start@Activity1Start\r\n" +
                    "TestActivitiesSource/TestActivity1Stop@Activity1Stop\r\n" +
                    "TestActivitiesSource/TestActivity2Start@Activity2Start\r\n" +
                    "TestActivitiesSource/TestActivity2Stop@Activity2Stop\r\n" +
                    "TestActivitiesSource/TestEvent\r\n"
                    );

                // Start activity 1
                diagnosticSourceListener.Write("TestActivity1Start", new { propStr = "start" });
                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("Activity1Start", eventSourceListener.LastEvent.EventSourceEventName);
                Assert.Equal("TestActivitiesSource", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestActivity1Start", eventSourceListener.LastEvent.EventName);
                Assert.Equal(1, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("start", eventSourceListener.LastEvent.Arguments["propStr"]);
                eventSourceListener.ResetEventCountAndLastEvent();

                // Start nested activity 2
                diagnosticSourceListener.Write("TestActivity2Start", new { propStr = "start" });
                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("Activity2Start", eventSourceListener.LastEvent.EventSourceEventName);
                Assert.Equal("TestActivitiesSource", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestActivity2Start", eventSourceListener.LastEvent.EventName);
                Assert.Equal(1, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("start", eventSourceListener.LastEvent.Arguments["propStr"]);
                eventSourceListener.ResetEventCountAndLastEvent();

                // Send a normal event 
                diagnosticSourceListener.Write("TestEvent", new { propStr = "event" });
                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("Event", eventSourceListener.LastEvent.EventSourceEventName);
                Assert.Equal("TestActivitiesSource", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestEvent", eventSourceListener.LastEvent.EventName);
                Assert.Equal(1, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("event", eventSourceListener.LastEvent.Arguments["propStr"]);
                eventSourceListener.ResetEventCountAndLastEvent();

                // Stop nested activity 2
                diagnosticSourceListener.Write("TestActivity2Stop", new { propStr = "stop" });
                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("Activity2Stop", eventSourceListener.LastEvent.EventSourceEventName);
                Assert.Equal("TestActivitiesSource", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestActivity2Stop", eventSourceListener.LastEvent.EventName);
                Assert.Equal(1, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("stop", eventSourceListener.LastEvent.Arguments["propStr"]);
                eventSourceListener.ResetEventCountAndLastEvent();

                // Stop activity 1
                diagnosticSourceListener.Write("TestActivity1Stop", new { propStr = "stop" });
                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("Activity1Stop", eventSourceListener.LastEvent.EventSourceEventName);
                Assert.Equal("TestActivitiesSource", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestActivity1Stop", eventSourceListener.LastEvent.EventName);
                Assert.Equal(1, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("stop", eventSourceListener.LastEvent.Arguments["propStr"]);
                eventSourceListener.ResetEventCountAndLastEvent();
            }
        }
コード例 #13
0
        public void TestActivities()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
                using (var diagnosticSourceListener = new DiagnosticListener("TestActivitiesSource"))
                {
                    Assert.Equal(0, eventSourceListener.EventCount);
                    eventSourceListener.Enable(
                        "TestActivitiesSource/TestActivity1Start@Activity1Start\r\n" +
                        "TestActivitiesSource/TestActivity1Stop@Activity1Stop\r\n" +
                        "TestActivitiesSource/TestActivity2Start@Activity2Start\r\n" +
                        "TestActivitiesSource/TestActivity2Stop@Activity2Stop\r\n" +
                        "TestActivitiesSource/TestEvent\r\n"
                        );

                    // Start activity 1
                    diagnosticSourceListener.Write("TestActivity1Start", new { propStr = "start" });
                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("Activity1Start", eventSourceListener.LastEvent.EventSourceEventName);
                    Assert.Equal("TestActivitiesSource", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestActivity1Start", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(1, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal("start", eventSourceListener.LastEvent.Arguments["propStr"]);
                    eventSourceListener.ResetEventCountAndLastEvent();

                    // Start nested activity 2
                    diagnosticSourceListener.Write("TestActivity2Start", new { propStr = "start" });
                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("Activity2Start", eventSourceListener.LastEvent.EventSourceEventName);
                    Assert.Equal("TestActivitiesSource", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestActivity2Start", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(1, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal("start", eventSourceListener.LastEvent.Arguments["propStr"]);
                    eventSourceListener.ResetEventCountAndLastEvent();

                    // Send a normal event
                    diagnosticSourceListener.Write("TestEvent", new { propStr = "event" });
                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("Event", eventSourceListener.LastEvent.EventSourceEventName);
                    Assert.Equal("TestActivitiesSource", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestEvent", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(1, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal("event", eventSourceListener.LastEvent.Arguments["propStr"]);
                    eventSourceListener.ResetEventCountAndLastEvent();

                    // Stop nested activity 2
                    diagnosticSourceListener.Write("TestActivity2Stop", new { propStr = "stop" });
                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("Activity2Stop", eventSourceListener.LastEvent.EventSourceEventName);
                    Assert.Equal("TestActivitiesSource", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestActivity2Stop", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(1, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal("stop", eventSourceListener.LastEvent.Arguments["propStr"]);
                    eventSourceListener.ResetEventCountAndLastEvent();

                    // Stop activity 1
                    diagnosticSourceListener.Write("TestActivity1Stop", new { propStr = "stop" });
                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("Activity1Stop", eventSourceListener.LastEvent.EventSourceEventName);
                    Assert.Equal("TestActivitiesSource", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestActivity1Stop", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(1, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal("stop", eventSourceListener.LastEvent.Arguments["propStr"]);
                    eventSourceListener.ResetEventCountAndLastEvent();
                }
        }
コード例 #14
0
        public void TestNulls()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
                using (var diagnosticSourceListener = new DiagnosticListener("TestNullsTestSource"))
                {
                    Assert.Equal(0, eventSourceListener.EventCount);

                    // Turn on events with both implicit and explicit types
                    eventSourceListener.Enable("TestNullsTestSource/TestEvent1:cls.Url;cls_Point_X=cls.Point.X");

                    /***************************************************************************************/
                    // Emit a null arguments object.

                    if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    {
                        diagnosticSourceListener.Write("TestEvent1", null);
                    }

                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("TestNullsTestSource", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(0, eventSourceListener.LastEvent.Arguments.Count);
                    eventSourceListener.ResetEventCountAndLastEvent();

                    /***************************************************************************************/
                    // Emit an arguments object with nulls in it.

                    MyClass val    = null;
                    string  strVal = null;
                    if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    {
                        diagnosticSourceListener.Write("TestEvent1", new { cls = val, propStr = "propVal1", propStrNull = strVal });
                    }

                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("TestNullsTestSource", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(3, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal("", eventSourceListener.LastEvent.Arguments["cls"]);         // Tostring() on a null end up as an empty string.
                    Assert.Equal("propVal1", eventSourceListener.LastEvent.Arguments["propStr"]);
                    Assert.Equal("", eventSourceListener.LastEvent.Arguments["propStrNull"]); // null strings get turned into empty strings
                    eventSourceListener.ResetEventCountAndLastEvent();

                    /***************************************************************************************/
                    // Emit an arguments object that points at null things

                    MyClass val1 = new MyClass()
                    {
                        Url = "myUrlVal", Point = null
                    };
                    if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    {
                        diagnosticSourceListener.Write("TestEvent1", new { cls = val1, propStr = "propVal1" });
                    }

                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("TestNullsTestSource", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(3, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal(val1.GetType().FullName, eventSourceListener.LastEvent.Arguments["cls"]); // ToString on cls is the class name
                    Assert.Equal("propVal1", eventSourceListener.LastEvent.Arguments["propStr"]);
                    Assert.Equal("myUrlVal", eventSourceListener.LastEvent.Arguments["Url"]);
                    eventSourceListener.ResetEventCountAndLastEvent();

                    /***************************************************************************************/
                    // Emit an arguments object that points at null things (variation 2)

                    MyClass val2 = new MyClass()
                    {
                        Url = null, Point = new MyPoint()
                        {
                            X = 8, Y = 9
                        }
                    };
                    if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    {
                        diagnosticSourceListener.Write("TestEvent1", new { cls = val2, propStr = "propVal1" });
                    }

                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("TestNullsTestSource", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(3, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal(val2.GetType().FullName, eventSourceListener.LastEvent.Arguments["cls"]); // ToString on cls is the class name
                    Assert.Equal("propVal1", eventSourceListener.LastEvent.Arguments["propStr"]);
                    Assert.Equal("8", eventSourceListener.LastEvent.Arguments["cls_Point_X"]);
                    eventSourceListener.ResetEventCountAndLastEvent();
                }
        }
コード例 #15
0
        public void TestSpecificEvents()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
                using (var diagnosticSourceListener = new DiagnosticListener("TestSpecificEventsSource"))
                {
                    Assert.Equal(0, eventSourceListener.EventCount);

                    // Turn on events with both implicit and explicit types You can have whitespace
                    // before and after each spec.
                    eventSourceListener.Enable(
                        "  TestSpecificEventsSource/TestEvent1:cls_Point_X=cls.Point.X;cls_Point_Y=cls.Point.Y\r\n" +
                        "  TestSpecificEventsSource/TestEvent2:cls_Url=cls.Url\r\n"
                        );

                    /***************************************************************************************/
                    // Emit an event that matches the first pattern.
                    MyClass val = new MyClass()
                    {
                        Url = "MyUrl", Point = new MyPoint()
                        {
                            X = 3, Y = 5
                        }
                    };
                    if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    {
                        diagnosticSourceListener.Write("TestEvent1", new { propStr = "hi", propInt = 4, cls = val });
                    }

                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("TestSpecificEventsSource", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(5, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal(val.GetType().FullName, eventSourceListener.LastEvent.Arguments["cls"]); // ToString on cls is the class name
                    Assert.Equal("hi", eventSourceListener.LastEvent.Arguments["propStr"]);
                    Assert.Equal("4", eventSourceListener.LastEvent.Arguments["propInt"]);
                    Assert.Equal("3", eventSourceListener.LastEvent.Arguments["cls_Point_X"]);
                    Assert.Equal("5", eventSourceListener.LastEvent.Arguments["cls_Point_Y"]);
                    eventSourceListener.ResetEventCountAndLastEvent();

                    /***************************************************************************************/
                    // Emit an event that matches the second pattern.
                    if (diagnosticSourceListener.IsEnabled("TestEvent2"))
                    {
                        diagnosticSourceListener.Write("TestEvent2", new { prop2Str = "hello", prop2Int = 8, cls = val });
                    }

                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("TestSpecificEventsSource", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestEvent2", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(4, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal(val.GetType().FullName, eventSourceListener.LastEvent.Arguments["cls"]); // ToString on cls is the class name
                    Assert.Equal("hello", eventSourceListener.LastEvent.Arguments["prop2Str"]);
                    Assert.Equal("8", eventSourceListener.LastEvent.Arguments["prop2Int"]);
                    Assert.Equal("MyUrl", eventSourceListener.LastEvent.Arguments["cls_Url"]);
                    eventSourceListener.ResetEventCountAndLastEvent();

                    // Emit an event that does not match either pattern.  (thus will be filtered out)
                    if (diagnosticSourceListener.IsEnabled("TestEvent3"))
                    {
                        diagnosticSourceListener.Write("TestEvent3", new { propStr = "prop3", });
                    }
                    Assert.Equal(0, eventSourceListener.EventCount);    // No Event should be fired.

                    /***************************************************************************************/
                    // Emit an event from another diagnostic source with the same event name.
                    // It will be filtered out.
                    using (var diagnosticSourceListener2 = new DiagnosticListener("TestSpecificEventsSource2"))
                    {
                        if (diagnosticSourceListener2.IsEnabled("TestEvent1"))
                        {
                            diagnosticSourceListener2.Write("TestEvent1", new { propStr = "hi", propInt = 4, cls = val });
                        }
                    }
                    Assert.Equal(0, eventSourceListener.EventCount);    // No Event should be fired.

                    // Disable all the listener and insure that no more events come through.
                    eventSourceListener.Disable();

                    diagnosticSourceListener.Write("TestEvent1", null);
                    diagnosticSourceListener.Write("TestEvent2", null);

                    Assert.Equal(0, eventSourceListener.EventCount);    // No Event should be received.
                }

            // Make sure that there are no Diagnostic Listeners left over.
            DiagnosticListener.AllListeners.Subscribe(DiagnosticSourceTest.MakeObserver(delegate(DiagnosticListener listen)
            {
                Assert.True(!listen.Name.StartsWith("BuildTestSource"));
            }));
        }
コード例 #16
0
        public void TestBadProperties()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
            using (var diagnosticSourceListener = new DiagnosticListener("TestBadPropertiesSource"))
            {
                Assert.Equal(0, eventSourceListener.EventCount);

                // This has a syntax error in the Url case, so it should be ignored.  
                eventSourceListener.Enable("TestBadPropertiesSource/TestEvent1:cls.Ur-+l");

                /***************************************************************************************/
                // Emit an event that matches the first pattern. 
                MyClass val = new MyClass() { Url = "MyUrl", Point = new MyPoint() { X = 3, Y = 5 } };
                if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    diagnosticSourceListener.Write("TestEvent1", new { propStr = "hi", propInt = 4, cls = val });

                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("TestBadPropertiesSource", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                Assert.Equal(2, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("hi", eventSourceListener.LastEvent.Arguments["propStr"]);
                Assert.Equal("4", eventSourceListener.LastEvent.Arguments["propInt"]);
                eventSourceListener.ResetEventCountAndLastEvent();
            }
        }
コード例 #17
0
        public void TestMessages()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
            using (var diagnosticSourceListener = new DiagnosticListener("TestMessagesSource"))
            {
                Assert.Equal(0, eventSourceListener.EventCount);

                // This is just to make debugging easier.  
                var messages = new List<string>();

                eventSourceListener.OtherEventWritten += delegate (EventWrittenEventArgs evnt)
                {
                    if (evnt.EventName == "Message")
                    {
                        var message = (string)evnt.Payload[0];
                        messages.Add(message);
                    }
                };

                // This has a syntax error in the Url case, so it should be ignored.  
                eventSourceListener.Enable("TestMessagesSource/TestEvent1:-cls.Url");
                Assert.Equal(0, eventSourceListener.EventCount);
                Assert.True(3 <= messages.Count);
            }
        }
コード例 #18
0
        public void TestShortcutKeywords()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
                // These are look-alikes for the real ones.
                using (var aspNetCoreSource = new DiagnosticListener("Microsoft.AspNetCore"))
                    using (var entityFrameworkCoreSource = new DiagnosticListener("Microsoft.EntityFrameworkCore"))
                    {
                        // These are from DiagnosticSourceEventListener.
                        var Messages                    = (EventKeywords)0x1;
                        var Events                      = (EventKeywords)0x2;
                        var AspNetCoreHosting           = (EventKeywords)0x1000;
                        var EntityFrameworkCoreCommands = (EventKeywords)0x2000;

                        // Turn on listener using just the keywords
                        eventSourceListener.Enable(null, Messages | Events | AspNetCoreHosting | EntityFrameworkCoreCommands);

                        Assert.Equal(0, eventSourceListener.EventCount);

                        // Start a ASP.NET Request
                        aspNetCoreSource.Write("Microsoft.AspNetCore.Hosting.BeginRequest",
                                               new
                        {
                            httpContext = new
                            {
                                Request = new
                                {
                                    Method      = "Get",
                                    Host        = "MyHost",
                                    Path        = "MyPath",
                                    QueryString = "MyQuery"
                                }
                            }
                        });
                        // Check that the morphs work as expected.
                        Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                        Assert.Equal("Activity1Start", eventSourceListener.LastEvent.EventSourceEventName);
                        Assert.Equal("Microsoft.AspNetCore", eventSourceListener.LastEvent.SourceName);
                        Assert.Equal("Microsoft.AspNetCore.Hosting.BeginRequest", eventSourceListener.LastEvent.EventName);
                        Assert.True(4 <= eventSourceListener.LastEvent.Arguments.Count);
                        Debug.WriteLine("Arg Keys = " + string.Join(" ", eventSourceListener.LastEvent.Arguments.Keys));
                        Debug.WriteLine("Arg Values = " + string.Join(" ", eventSourceListener.LastEvent.Arguments.Values));
                        Assert.Equal("Get", eventSourceListener.LastEvent.Arguments["Method"]);
                        Assert.Equal("MyHost", eventSourceListener.LastEvent.Arguments["Host"]);
                        Assert.Equal("MyPath", eventSourceListener.LastEvent.Arguments["Path"]);
                        Assert.Equal("MyQuery", eventSourceListener.LastEvent.Arguments["QueryString"]);
                        eventSourceListener.ResetEventCountAndLastEvent();

                        // Start a SQL command
                        entityFrameworkCoreSource.Write("Microsoft.EntityFrameworkCore.BeforeExecuteCommand",
                                                        new
                        {
                            Command = new
                            {
                                Connection = new
                                {
                                    DataSource = "MyDataSource",
                                    Database   = "MyDatabase",
                                },
                                CommandText = "MyCommand"
                            }
                        });
                        Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                        Assert.Equal("Activity2Start", eventSourceListener.LastEvent.EventSourceEventName);
                        Assert.Equal("Microsoft.EntityFrameworkCore", eventSourceListener.LastEvent.SourceName);
                        Assert.Equal("Microsoft.EntityFrameworkCore.BeforeExecuteCommand", eventSourceListener.LastEvent.EventName);
                        Assert.True(3 <= eventSourceListener.LastEvent.Arguments.Count);
                        Assert.Equal("MyDataSource", eventSourceListener.LastEvent.Arguments["DataSource"]);
                        Assert.Equal("MyDatabase", eventSourceListener.LastEvent.Arguments["Database"]);
                        Assert.Equal("MyCommand", eventSourceListener.LastEvent.Arguments["CommandText"]);
                        eventSourceListener.ResetEventCountAndLastEvent();

                        // Stop the SQL command
                        entityFrameworkCoreSource.Write("Microsoft.EntityFrameworkCore.AfterExecuteCommand", null);
                        Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                        Assert.Equal("Activity2Stop", eventSourceListener.LastEvent.EventSourceEventName);
                        Assert.Equal("Microsoft.EntityFrameworkCore", eventSourceListener.LastEvent.SourceName);
                        Assert.Equal("Microsoft.EntityFrameworkCore.AfterExecuteCommand", eventSourceListener.LastEvent.EventName);
                        eventSourceListener.ResetEventCountAndLastEvent();

                        // Stop the ASP.NET reqeust.
                        aspNetCoreSource.Write("Microsoft.AspNetCore.Hosting.EndRequest",
                                               new
                        {
                            httpContext = new
                            {
                                Response = new
                                {
                                    StatusCode = "200"
                                },
                                TraceIdentifier = "MyTraceId"
                            }
                        });
                        Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                        Assert.Equal("Activity1Stop", eventSourceListener.LastEvent.EventSourceEventName);
                        Assert.Equal("Microsoft.AspNetCore", eventSourceListener.LastEvent.SourceName);
                        Assert.Equal("Microsoft.AspNetCore.Hosting.EndRequest", eventSourceListener.LastEvent.EventName);
                        Assert.True(2 <= eventSourceListener.LastEvent.Arguments.Count);
                        Assert.Equal("MyTraceId", eventSourceListener.LastEvent.Arguments["TraceIdentifier"]);
                        Assert.Equal("200", eventSourceListener.LastEvent.Arguments["StatusCode"]);
                        eventSourceListener.ResetEventCountAndLastEvent();
                    }
        }
コード例 #19
0
        public void TestShortcutKeywords()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
            // These are look-alikes for the real ones.  
            using (var aspNetCoreSource = new DiagnosticListener("Microsoft.AspNetCore"))
            using (var entityFrameworkCoreSource = new DiagnosticListener("Microsoft.EntityFrameworkCore"))
            {
                // These are from DiagnosticSourceEventListener.  
                var Messages = (EventKeywords)0x1;
                var Events = (EventKeywords)0x2;
                var AspNetCoreHosting = (EventKeywords)0x1000;
                var EntityFrameworkCoreCommands = (EventKeywords)0x2000;

                // Turn on listener using just the keywords 
                eventSourceListener.Enable(null, Messages | Events | AspNetCoreHosting | EntityFrameworkCoreCommands);

                Assert.Equal(0, eventSourceListener.EventCount);

                // Start a ASP.NET Request
                aspNetCoreSource.Write("Microsoft.AspNetCore.Hosting.BeginRequest",
                    new
                    {
                        httpContext = new
                        {
                            Request = new
                            {
                                Method = "Get",
                                Host = "MyHost",
                                Path = "MyPath",
                                QueryString = "MyQuery"
                            }
                        }
                    });
                // Check that the morphs work as expected.  
                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("Activity1Start", eventSourceListener.LastEvent.EventSourceEventName);
                Assert.Equal("Microsoft.AspNetCore", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("Microsoft.AspNetCore.Hosting.BeginRequest", eventSourceListener.LastEvent.EventName);
                Assert.True(4 <= eventSourceListener.LastEvent.Arguments.Count);
                Debug.WriteLine("Arg Keys = " + string.Join(" ", eventSourceListener.LastEvent.Arguments.Keys));
                Debug.WriteLine("Arg Values = " + string.Join(" ", eventSourceListener.LastEvent.Arguments.Values));
                Assert.Equal("Get", eventSourceListener.LastEvent.Arguments["Method"]);
                Assert.Equal("MyHost", eventSourceListener.LastEvent.Arguments["Host"]);
                Assert.Equal("MyPath", eventSourceListener.LastEvent.Arguments["Path"]);
                Assert.Equal("MyQuery", eventSourceListener.LastEvent.Arguments["QueryString"]);
                eventSourceListener.ResetEventCountAndLastEvent();

                // Start a SQL command 
                entityFrameworkCoreSource.Write("Microsoft.EntityFrameworkCore.BeforeExecuteCommand",
                    new
                    {
                        Command = new
                        {
                            Connection = new
                            {
                                DataSource = "MyDataSource",
                                Database = "MyDatabase",
                            },
                            CommandText = "MyCommand"
                        }
                    });
                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("Activity2Start", eventSourceListener.LastEvent.EventSourceEventName);
                Assert.Equal("Microsoft.EntityFrameworkCore", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("Microsoft.EntityFrameworkCore.BeforeExecuteCommand", eventSourceListener.LastEvent.EventName);
                Assert.True(3 <= eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("MyDataSource", eventSourceListener.LastEvent.Arguments["DataSource"]);
                Assert.Equal("MyDatabase", eventSourceListener.LastEvent.Arguments["Database"]);
                Assert.Equal("MyCommand", eventSourceListener.LastEvent.Arguments["CommandText"]);
                eventSourceListener.ResetEventCountAndLastEvent();

                // Stop the SQL command 
                entityFrameworkCoreSource.Write("Microsoft.EntityFrameworkCore.AfterExecuteCommand", null);
                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("Activity2Stop", eventSourceListener.LastEvent.EventSourceEventName);
                Assert.Equal("Microsoft.EntityFrameworkCore", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("Microsoft.EntityFrameworkCore.AfterExecuteCommand", eventSourceListener.LastEvent.EventName);
                eventSourceListener.ResetEventCountAndLastEvent();

                // Stop the ASP.NET reqeust.  
                aspNetCoreSource.Write("Microsoft.AspNetCore.Hosting.EndRequest", null);
                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("Activity1Stop", eventSourceListener.LastEvent.EventSourceEventName);
                Assert.Equal("Microsoft.AspNetCore", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("Microsoft.AspNetCore.Hosting.EndRequest", eventSourceListener.LastEvent.EventName);
                eventSourceListener.ResetEventCountAndLastEvent();
            }
        }
コード例 #20
0
        public void LinuxNewLineConventions()
        {
            using (var eventSourceListener = new TestDiagnosticSourceEventListener())
            using (var diagnosticSourceListener = new DiagnosticListener("LinuxNewLineConventionsSource"))
            {
                Assert.Equal(0, eventSourceListener.EventCount);

                // Turn on events with both implicit and explicit types You can have whitespace 
                // before and after each spec.   Use \n rather than \r\n 
                eventSourceListener.Enable(
                    "  LinuxNewLineConventionsSource/TestEvent1:-cls_Point_X=cls.Point.X\n" +
                    "  LinuxNewLineConventionsSource/TestEvent2:-cls_Url=cls.Url\n"
                    );

                /***************************************************************************************/
                // Emit an event that matches the first pattern. 
                MyClass val = new MyClass() { Url = "MyUrl", Point = new MyPoint() { X = 3, Y = 5 } };
                if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                    diagnosticSourceListener.Write("TestEvent1", new { propStr = "hi", propInt = 4, cls = val });

                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                Assert.Equal("LinuxNewLineConventionsSource", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                Assert.Equal(1, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("3", eventSourceListener.LastEvent.Arguments["cls_Point_X"]);
                eventSourceListener.ResetEventCountAndLastEvent();

                /***************************************************************************************/
                // Emit an event that matches the second pattern. 
                if (diagnosticSourceListener.IsEnabled("TestEvent2"))
                    diagnosticSourceListener.Write("TestEvent2", new { prop2Str = "hello", prop2Int = 8, cls = val });

                Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.  
                Assert.Equal("LinuxNewLineConventionsSource", eventSourceListener.LastEvent.SourceName);
                Assert.Equal("TestEvent2", eventSourceListener.LastEvent.EventName);
                Assert.Equal(1, eventSourceListener.LastEvent.Arguments.Count);
                Assert.Equal("MyUrl", eventSourceListener.LastEvent.Arguments["cls_Url"]);
                eventSourceListener.ResetEventCountAndLastEvent();

                // Emit an event that does not match either pattern.  (thus will be filtered out)
                if (diagnosticSourceListener.IsEnabled("TestEvent3"))
                    diagnosticSourceListener.Write("TestEvent3", new { propStr = "prop3", });
                Assert.Equal(0, eventSourceListener.EventCount);        // No Event should be fired.  
            }

            // Make sure that there are no Diagnostic Listeners left over.  
            DiagnosticListener.AllListeners.Subscribe(DiagnosticSourceTest.MakeObserver(delegate (DiagnosticListener listen)
            {
                Assert.True(!listen.Name.StartsWith("BuildTestSource"));
            }));
        }