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();
                }
        }
        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"));
            }));
        }
        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();
                }
        }
        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(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();

                    /***************************************************************************************/
                    // 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(2, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal(val.GetType().FullName, eventSourceListener.LastEvent.Arguments["cls"]); // ToString on cls is the class name
                    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.
                }
        }