Example #1
0
        /// <summary>
        /// Helper method for the two tests above.
        /// </summary>
        private void Test_WriteEvent(Listener listener, bool useSelfDescribingEvents, bool isEtwListener = false)
        {
            using (var logger = new EventSourceTest(useSelfDescribingEvents))
            {
                var tests = new List <SubTest>();

                /*************************************************************************/
                tests.Add(new SubTest("WriteEvent/Basic/EventII",
                                      delegate()
                                      { logger.EventII(10, 11); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal("EventII", evt.EventName);
                    Assert.Equal(10, evt.PayloadValue(0, "arg1"));
                    Assert.Equal(11, evt.PayloadValue(1, "arg2"));
                }));
                /*************************************************************************/
                tests.Add(new SubTest("WriteEvent/Basic/EventSS",
                                      delegate()
                                      { logger.EventSS("one", "two"); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal("EventSS", evt.EventName);
                    Assert.Equal("one", evt.PayloadValue(0, "arg1"));
                    Assert.Equal("two", evt.PayloadValue(1, "arg2"));
                }));
                /*************************************************************************/
                tests.Add(new SubTest("Write/Basic/EventWith7Strings",
                                      delegate()
                {
                    logger.EventWith7Strings("s0", "s1", "s2", "s3", "s4", "s5", "s6");
                },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal("EventWith7Strings", evt.EventName);
                    Assert.Equal("s0", (string)evt.PayloadValue(0, "s0"));
                    Assert.Equal("s6", (string)evt.PayloadValue(6, "s6"));
                }));
                /*************************************************************************/
                tests.Add(new SubTest("Write/Basic/EventWith9Strings",
                                      delegate()
                {
                    logger.EventWith9Strings("s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8");
                },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal("EventWith9Strings", evt.EventName);
                    Assert.Equal("s0", (string)evt.PayloadValue(0, "s0"));
                    Assert.Equal("s8", (string)evt.PayloadValue(8, "s8"));
                }));
                /*************************************************************************/
                Test_WriteEvent_AddEtwTests(tests, logger);

                /*************************************************************************/
                /*************************** ENUM TESTING *******************************/
                /*************************************************************************/

                /*************************************************************************/
                tests.Add(new SubTest("WriteEvent/Enum/EventEnum",
                                      delegate()
                {
                    logger.EventEnum(SdtEventSources.MyColor.Blue);
                },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal("EventEnum", evt.EventName);

                    Assert.Equal(1, ((IConvertible)evt.PayloadValue(0, "x")).ToInt32(null));
                    if (evt.IsEtw && !useSelfDescribingEvents)
                    {
                        Assert.Equal("Blue", evt.PayloadString(0, "x"));
                    }
                }));

                tests.Add(new SubTest("WriteEvent/Enum/EventEnum1",
                                      delegate()
                {
                    logger.EventEnum1(MyColor.Blue);
                },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal("EventEnum1", evt.EventName);

                    Assert.Equal(1, ((IConvertible)evt.PayloadValue(0, "x")).ToInt32(null));
                    if (evt.IsEtw && !useSelfDescribingEvents)
                    {
                        Assert.Equal("Blue", evt.PayloadString(0, "x"));
                    }
                }));

                tests.Add(new SubTest("WriteEvent/Basic/EventWithIntIntString",
                                      delegate()
                                      { logger.EventWithIntIntString(10, 11, "test"); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal("EventWithIntIntString", evt.EventName);
                    Assert.Equal(10, evt.PayloadValue(0, "i1"));
                    Assert.Equal(11, evt.PayloadValue(1, "i2"));
                    Assert.Equal("test", evt.PayloadValue(2, "str"));
                }));

                tests.Add(new SubTest("WriteEvent/Basic/EventWithIntLongString",
                                      delegate()
                                      { logger.EventWithIntLongString(10, (long)11, "test"); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal("EventWithIntLongString", evt.EventName);
                    Assert.Equal(10, evt.PayloadValue(0, "i1"));
                    Assert.Equal(evt.PayloadValue(1, "l1"), (long)11);
                    Assert.Equal("test", evt.PayloadValue(2, "str"));
                }));

                tests.Add(new SubTest("WriteEvent/Basic/EventWithString",
                                      delegate()
                                      { logger.EventWithString(null); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal(1, evt.PayloadCount);
                    Assert.Equal("", evt.PayloadValue(0, null));
                }));


                tests.Add(new SubTest("WriteEvent/Basic/EventWithIntAndString",
                                      delegate()
                                      { logger.EventWithIntAndString(12, null); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal(2, evt.PayloadCount);
                    Assert.Equal(12, evt.PayloadValue(0, null));
                    Assert.Equal("", evt.PayloadValue(1, null));
                }));

                tests.Add(new SubTest("WriteEvent/Basic/EventWithLongAndString",
                                      delegate()
                                      { logger.EventWithLongAndString(120L, null); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal(2, evt.PayloadCount);
                    Assert.Equal(120L, evt.PayloadValue(0, null));
                    Assert.Equal("", evt.PayloadValue(1, null));
                }));

                tests.Add(new SubTest("WriteEvent/Basic/EventWithStringAndInt",
                                      delegate()
                                      { logger.EventWithStringAndInt(null, 12); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal(2, evt.PayloadCount);
                    Assert.Equal("", evt.PayloadValue(0, null));
                    Assert.Equal(12, evt.PayloadValue(1, null));
                }));

                tests.Add(new SubTest("WriteEvent/Basic/EventWithStringAndIntAndInt",
                                      delegate()
                                      { logger.EventWithStringAndIntAndInt(null, 12, 13); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal(3, evt.PayloadCount);
                    Assert.Equal("", evt.PayloadValue(0, null));
                    Assert.Equal(12, evt.PayloadValue(1, null));
                    Assert.Equal(13, evt.PayloadValue(2, null));
                }));

                tests.Add(new SubTest("WriteEvent/Basic/EventWithStringAndLong",
                                      delegate()
                                      { logger.EventWithStringAndLong(null, 120L); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal(2, evt.PayloadCount);
                    Assert.Equal("", evt.PayloadValue(0, null));
                    Assert.Equal(120L, evt.PayloadValue(1, null));
                }));

                tests.Add(new SubTest("WriteEvent/Basic/EventWithStringAndString",
                                      delegate()
                                      { logger.EventWithStringAndString(null, null); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal(2, evt.PayloadCount);
                    Assert.Equal("", evt.PayloadValue(0, null));
                    Assert.Equal("", evt.PayloadValue(1, null));
                }));

                tests.Add(new SubTest("WriteEvent/Basic/EventWithStringAndStringAndString",
                                      delegate()
                                      { logger.EventWithStringAndStringAndString(null, null, null); },
                                      delegate(Event evt)
                {
                    Assert.Equal(logger.Name, evt.ProviderName);
                    Assert.Equal(3, evt.PayloadCount);
                    Assert.Equal("", evt.PayloadValue(0, null));
                    Assert.Equal("", evt.PayloadValue(1, null));
                    Assert.Equal("", evt.PayloadValue(2, null));
                }));

                // Self-describing ETW does not support NULL arguments.
                if (useSelfDescribingEvents && !(isEtwListener))
                {
                    tests.Add(new SubTest("WriteEvent/Basic/EventVarArgsWithString",
                                          delegate()
                                          { logger.EventVarArgsWithString(1, 2, 12, null); },
                                          delegate(Event evt)
                    {
                        Assert.Equal(logger.Name, evt.ProviderName);
                        Assert.Equal(4, evt.PayloadCount);
                        Assert.Equal(1, evt.PayloadValue(0, null));
                        Assert.Equal(2, evt.PayloadValue(1, null));
                        Assert.Equal(12, evt.PayloadValue(2, null));
                        Assert.Equal("", evt.PayloadValue(3, null));
                    }));
                }

                // Probably belongs in the user TestUsersErrors.cs.
                if (!useSelfDescribingEvents)
                {
                    tests.Add(new SubTest("WriteEvent/Basic/EventWithIncorrectNumberOfParameters",
                                          delegate()
                    {
                        logger.EventWithIncorrectNumberOfParameters("TestMessage", "TestPath", 10);
                    },
                                          delegate(List <Event> evts)
                    {
                        Assert.True(0 < evts.Count);

                        // We give an error message in EventListener case but not the ETW case.
                        if (1 < evts.Count)
                        {
                            Assert.Equal(2, evts.Count);
                            Assert.Equal(logger.Name, evts[0].ProviderName);
                            Assert.Equal("EventSourceMessage", evts[0].EventName);
                            string errorMsg = evts[0].PayloadString(0, "message");
                            Assert.Matches("called with 1.*defined with 3", errorMsg);
                        }

                        int eventIdx = evts.Count - 1;
                        Assert.Equal(logger.Name, evts[eventIdx].ProviderName);
                        Assert.Equal("EventWithIncorrectNumberOfParameters", evts[eventIdx].EventName);
                        Assert.Equal("{TestPath:10}TestMessage", evts[eventIdx].PayloadString(0, "message"));
                    }));
                }

                // If you only wish to run one or several of the tests you can filter them here by
                // Uncommenting the following line.
                // tests = tests.FindAll(test => Regex.IsMatch(test.Name, "ventWithByteArray"));

                // Next run the same tests with the TraceLogging path.
                EventTestHarness.RunTests(tests, listener, logger);
            }
        }
Example #2
0
        public void Test_WriteEvent_ArgsBasicTypes()
        {
            TestUtilities.CheckNoEventSourcesRunning("Start");

            using (var log = new EventSourceTest())
            {
                using (var el = new LoudListener(log))
                {
                    var sources = EventSource.GetSources();
                    Assert.Contains(log, sources);

                    Assert.NotNull(EventSource.GenerateManifest(typeof(SimpleEventSource), string.Empty, EventManifestOptions.OnlyIfNeededForRegistration));
                    Assert.Null(EventSource.GenerateManifest(typeof(EventSourceTest), string.Empty, EventManifestOptions.OnlyIfNeededForRegistration));

                    log.Event0();
                    Assert.Equal(1, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(0, LoudListener.t_lastEvent.Payload.Count);

                    #region Validate "int" arguments

                    log.EventI(10);
                    Assert.Equal(2, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(1, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal(10, (int)LoudListener.t_lastEvent.Payload[0]);

                    log.EventII(10, 11);
                    Assert.Equal(3, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(2, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal(10, (int)LoudListener.t_lastEvent.Payload[0]);
                    Assert.Equal(11, (int)LoudListener.t_lastEvent.Payload[1]);

                    log.EventIII(10, 11, 12);
                    Assert.Equal(4, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(3, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal(10, (int)LoudListener.t_lastEvent.Payload[0]);
                    Assert.Equal(11, (int)LoudListener.t_lastEvent.Payload[1]);
                    Assert.Equal(12, (int)LoudListener.t_lastEvent.Payload[2]);

                    #endregion

                    #region Validate "long" arguments

                    log.EventL(10);
                    Assert.Equal(5, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(1, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal(10, (long)LoudListener.t_lastEvent.Payload[0]);

                    log.EventLL(10, 11);
                    Assert.Equal(6, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(2, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal(10, (long)LoudListener.t_lastEvent.Payload[0]);
                    Assert.Equal(11, (long)LoudListener.t_lastEvent.Payload[1]);

                    log.EventLLL(10, 11, 12);
                    Assert.Equal(7, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(3, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal(10, (long)LoudListener.t_lastEvent.Payload[0]);
                    Assert.Equal(11, (long)LoudListener.t_lastEvent.Payload[1]);
                    Assert.Equal(12, (long)LoudListener.t_lastEvent.Payload[2]);

                    #endregion

                    #region Validate "string" arguments

                    log.EventS("10");
                    Assert.Equal(8, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(1, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal("10", (string)LoudListener.t_lastEvent.Payload[0]);

                    log.EventSS("10", "11");
                    Assert.Equal(9, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(2, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal("10", (string)LoudListener.t_lastEvent.Payload[0]);
                    Assert.Equal("11", (string)LoudListener.t_lastEvent.Payload[1]);

                    log.EventSSS("10", "11", "12");
                    Assert.Equal(10, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(3, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal("10", (string)LoudListener.t_lastEvent.Payload[0]);
                    Assert.Equal("11", (string)LoudListener.t_lastEvent.Payload[1]);
                    Assert.Equal("12", (string)LoudListener.t_lastEvent.Payload[2]);

                    #endregion

                    #region Validate byte array arguments

                    byte[] arr = new byte[20];
                    log.EventWithByteArray(arr);
                    Assert.Equal(52, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(1, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal(arr.Length, ((byte[])LoudListener.t_lastEvent.Payload[0]).Length);

                    #endregion

                    #region Validate mixed type arguments

                    log.EventSI("10", 11);
                    Assert.Equal(11, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(2, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal("10", (string)LoudListener.t_lastEvent.Payload[0]);
                    Assert.Equal(11, (int)LoudListener.t_lastEvent.Payload[1]);

                    log.EventSL("10", 11);
                    Assert.Equal(12, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(2, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal("10", (string)LoudListener.t_lastEvent.Payload[0]);
                    Assert.Equal(11, (long)LoudListener.t_lastEvent.Payload[1]);

                    log.EventSII("10", 11, 12);
                    Assert.Equal(13, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(3, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal("10", (string)LoudListener.t_lastEvent.Payload[0]);
                    Assert.Equal(11, (int)LoudListener.t_lastEvent.Payload[1]);
                    Assert.Equal(12, (int)LoudListener.t_lastEvent.Payload[2]);

                    #endregion

                    #region Validate enums/flags

                    log.EventEnum(MyColor.Blue);
                    Assert.Equal(19, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(1, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal(MyColor.Blue, (MyColor)LoudListener.t_lastEvent.Payload[0]);

                    log.EventEnum1(MyColor.Green);
                    Assert.Equal(20, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(1, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal(MyColor.Green, (MyColor)LoudListener.t_lastEvent.Payload[0]);

                    log.EventFlags(MyFlags.Flag1);
                    Assert.Equal(21, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(1, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal(MyFlags.Flag1, (MyFlags)LoudListener.t_lastEvent.Payload[0]);

                    log.EventFlags1(MyFlags.Flag1);
                    Assert.Equal(22, LoudListener.t_lastEvent.EventId);
                    Assert.Equal(1, LoudListener.t_lastEvent.Payload.Count);
                    Assert.Equal(MyFlags.Flag1, (MyFlags)LoudListener.t_lastEvent.Payload[0]);

                    #endregion

                    #region Validate DateTime
                    Test_WriteEvent_ArgsBasicTypes_Etw_Validate_DateTime(log);
                    #endregion
                }
            }

            TestUtilities.CheckNoEventSourcesRunning("Stop");
        }