Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DiagnosticSourceEventSourceBridgeTests.cs « tests « System.Diagnostics.DiagnosticSource « libraries « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ecf73aa7f06cb79eab2c1fc115c7780fc5799206 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Text;
using System.Threading;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;

namespace System.Diagnostics.Tests
{
    //Complex types are not supported on EventSource for .NET 4.5
    public class DiagnosticSourceEventSourceBridgeTests
    {
        // To avoid interactions between tests when they are run in parallel, we run all these tests in their
        // own sub-process using RemoteExecutor.Invoke()  However this makes it very inconvenient to debug the test.
        // By seting this #if to true you stub out RemoteInvoke and the code will run in-proc which is useful
        // in debugging.
#if false
        class NullDispose : IDisposable
        {
            public void Dispose()
            {
            }
        }
        static IDisposable RemoteExecutor.Invoke(Action a)
        {
            a();
            return new NullDispose();
        }
#endif

        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public void TestEnableAllActivitySourcesAllEvents()
        {
            RemoteExecutor.Invoke(() =>
            {
                using TestDiagnosticSourceEventListener eventSourceListener = new TestDiagnosticSourceEventListener();
                ActivitySource [] sources = new ActivitySource[10];
                for (int i = 0; i < 10; i++)
                {
                    sources[i] = new ActivitySource($"Source{i}");
                }

                int eventsCount = 0;
                Assert.Equal(eventsCount, eventSourceListener.EventCount);
                eventSourceListener.Enable("  [AS]*  \r\n"); // All Sources + All Events

                Activity [] activities = new Activity[10];

                for (int i = 0; i < 10; i++)
                {
                    activities[i] = sources[i].StartActivity($"activity{i}");
                    Assert.NotNull(activities[i]);
                    Assert.Equal(++eventsCount, eventSourceListener.EventCount);
                    ValidateActivityEvents(eventSourceListener, "ActivityStart", sources[i].Name, activities[i].OperationName);
                    Assert.True(activities[i].IsAllDataRequested);
                    Assert.Equal(ActivityTraceFlags.Recorded, activities[i].ActivityTraceFlags);
                }

                for (int i = 0; i < 10; i++)
                {
                    activities[i].Dispose();
                    Assert.Equal(++eventsCount, eventSourceListener.EventCount);
                    ValidateActivityEvents(eventSourceListener, "ActivityStop", sources[i].Name, activities[i].OperationName);
                    sources[i].Dispose();
                }

            }).Dispose();
        }

        [ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        [InlineData("Start")]
        [InlineData("Stop")]
        [InlineData("start")]
        [InlineData("stop")]
        public void TestEnableAllActivitySourcesWithOneEvent(string eventName)
        {
            RemoteExecutor.Invoke((eventname) =>
            {
                using TestDiagnosticSourceEventListener eventSourceListener = new TestDiagnosticSourceEventListener();
                ActivitySource [] sources = new ActivitySource[10];
                for (int i = 0; i < 10; i++)
                {
                    sources[i] = new ActivitySource($"Source{i}");
                }

                int eventsCount = 0;
                Assert.Equal(eventsCount, eventSourceListener.EventCount);
                eventSourceListener.Enable($"  [AS]* / {eventname}  \r\n"); // All Sources + one Event

                Activity [] activities = new Activity[10];

                for (int i = 0; i < 10; i++)
                {
                    activities[i] = sources[i].StartActivity($"activity{i}");
                    Assert.NotNull(activities[i]);

                    if (eventname.Equals("Start", StringComparison.OrdinalIgnoreCase))
                    {
                        eventsCount++;
                        ValidateActivityEvents(eventSourceListener, "ActivityStart", sources[i].Name, activities[i].OperationName);
                    }
                    Assert.Equal(eventsCount, eventSourceListener.EventCount);
                    Assert.True(activities[i].IsAllDataRequested);
                    Assert.Equal(ActivityTraceFlags.Recorded, activities[i].ActivityTraceFlags);
                }

                for (int i = 0; i < 10; i++)
                {
                    activities[i].Dispose();

                    if (eventname.Equals("Stop", StringComparison.OrdinalIgnoreCase))
                    {
                        eventsCount++;
                        ValidateActivityEvents(eventSourceListener, "ActivityStop", sources[i].Name, activities[i].OperationName);
                    }

                    Assert.Equal(eventsCount, eventSourceListener.EventCount);
                    sources[i].Dispose();
                }
            }, eventName).Dispose();
        }

        [ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        [InlineData("Propagate", false, ActivityTraceFlags.None)]
        [InlineData("PROPAGATE", false, ActivityTraceFlags.None)]
        [InlineData("Record",    true,  ActivityTraceFlags.None)]
        [InlineData("recorD",    true,  ActivityTraceFlags.None)]
        [InlineData("",          true,  ActivityTraceFlags.Recorded)]
        public void TestEnableAllActivitySourcesWithSpeciifcSamplingResult(string samplingResult, bool alldataRequested, ActivityTraceFlags activityTraceFlags)
        {
            RemoteExecutor.Invoke((result, dataRequested, traceFlags) =>
            {
                using TestDiagnosticSourceEventListener eventSourceListener = new TestDiagnosticSourceEventListener();
                using ActivitySource source = new ActivitySource("SamplingSource");

                Assert.Equal(0, eventSourceListener.EventCount);
                eventSourceListener.Enable($"  [AS]* /- {result}  \r\n"); // All Sources + one Event


                Activity activity = source.StartActivity($"samplingActivity");
                Assert.NotNull(activity);
                Assert.Equal(1, eventSourceListener.EventCount);
                ValidateActivityEvents(eventSourceListener, "ActivityStart", source.Name, activity.OperationName);

                Assert.Equal(bool.Parse(dataRequested), activity.IsAllDataRequested);
                Assert.Equal(traceFlags, activity.ActivityTraceFlags.ToString());

                activity.Dispose();

                Assert.Equal(2, eventSourceListener.EventCount);
                ValidateActivityEvents(eventSourceListener, "ActivityStop", source.Name, activity.OperationName);
            }, samplingResult, alldataRequested.ToString(), activityTraceFlags.ToString()).Dispose();
        }

        [ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        [InlineData("",      "",          true)]
        [InlineData("Start", "",          true)]
        [InlineData("stop",  "",          true)]
        [InlineData("",      "Propagate", false)]
        [InlineData("",      "Record",    true)]
        [InlineData("Start", "Propagate", false)]
        [InlineData("Stop",  "Record",    true)]
        public void TestDefaultActivitySource(string eventName, string samplingResult, bool alldataRequested)
        {
            RemoteExecutor.Invoke((eventname, result, dataRequested) =>
            {
                using TestDiagnosticSourceEventListener eventSourceListener = new TestDiagnosticSourceEventListener();
                Activity a = new Activity(""); // we need this to ensure DiagnosticSourceEventSource.Logger creation.

                Assert.Equal(0, eventSourceListener.EventCount);
                eventSourceListener.Enable($"  [AS]/{eventname}-{result}\r\n");
                Assert.Equal("", a.Source.Name);

                a = a.Source.StartActivity("newOne");

                Assert.Equal(bool.Parse(dataRequested), a.IsAllDataRequested);
                // All Activities created with "new Activity(...)" will have ActivityTraceFlags is `None`;
                Assert.Equal(result.Length == 0 ? ActivityTraceFlags.Recorded : ActivityTraceFlags.None, a.ActivityTraceFlags);

                a.Dispose();

                int eCount = eventname.Length == 0 ? 2 : 1;
                Assert.Equal(eCount, eventSourceListener.EventCount);

                // None Default Source
                ActivitySource source = new ActivitySource("NoneDefault"); // not the default ActivitySource
                Activity activity = source.StartActivity($"ActivityFromNoneDefault"); // Shouldn't fire any event
                Assert.Equal(eCount, eventSourceListener.EventCount);
                Assert.Null(activity);
            }, eventName, samplingResult, alldataRequested.ToString()).Dispose();
        }

        [ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        [InlineData("[AS]*\r\n [AS]Specific/-Propagate\r\n [AS]*/-Propagate\r\n", false, true)]
        [InlineData("[AS]AnotherSource/-Propagate\r\n [AS]*/-Propagate\r\n [AS]Specific/-Record\r\n [AS]*\r\n", true, true)]
        [InlineData("[AS]*/-Propagate\r\n  [AS]*/-Propagate\r\n [AS]Specific/-Propagate\r\n[AS]Specific/-Record\r\n", true, false)]
        [InlineData("[AS]*/-Propagate\r\n", false, false)]
        [InlineData("[AS]*/-Record\r\n", true, true)]
        [InlineData("[AS]Specific/-Propagate\r\n [AS]NoneSpecific/-Record\r\n", false, true)]
        [InlineData("[AS]Specific/-Record\r\n [AS]NoneSpecific/-Propagate\r\n", true, false)]
        [InlineData("[AS]Specific\r\n [AS]NoneSpecific\r\n", true, true)]
        public void TestMultipleSpecs(string spec, bool isAlldataRequestedFromSpecif, bool alldataRequestedFromNoneSpecific)
        {
            RemoteExecutor.Invoke((specString, specificAllData, noneSpecificAllData) =>
            {
                using TestDiagnosticSourceEventListener eventSourceListener = new TestDiagnosticSourceEventListener();
                using ActivitySource aSource1 = new ActivitySource("Specific");
                eventSourceListener.Enable(specString);

                Assert.Equal(0, eventSourceListener.EventCount);

                Activity a1 = aSource1.StartActivity("a1");
                Assert.NotNull(a1);
                Assert.Equal(1, eventSourceListener.EventCount);
                Assert.Equal(bool.Parse(specificAllData), a1.IsAllDataRequested);
                a1.Dispose();
                Assert.Equal(2, eventSourceListener.EventCount);

                using ActivitySource aSource2 = new ActivitySource("NoneSpecific");
                Activity a2 = aSource2.StartActivity("a2");
                Assert.NotNull(a2);
                Assert.Equal(3, eventSourceListener.EventCount);
                Assert.Equal(bool.Parse(noneSpecificAllData), a2.IsAllDataRequested);
                a2.Dispose();
                Assert.Equal(4, eventSourceListener.EventCount);

            }, spec, isAlldataRequestedFromSpecif.ToString(), alldataRequestedFromNoneSpecific.ToString()).Dispose();
        }

        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public void TestTransformSpecs()
        {
            RemoteExecutor.Invoke(() =>
            {
                using (TestDiagnosticSourceEventListener eventSourceListener = new TestDiagnosticSourceEventListener())
                {
                    using ActivitySource aSource1 = new ActivitySource("TransSpecsSource");
                    eventSourceListener.Enable("[AS]*");

                    Activity a = aSource1.StartActivity("TransSpecs");
                    ValidateActivityEvents(eventSourceListener, "ActivityStart", "TransSpecsSource", "TransSpecs");

                    var list1 = eventSourceListener.LastEvent.Arguments;
                    Assert.Equal(a.OperationName, list1["OperationName"]);

                    a.Dispose();
                    ValidateActivityEvents(eventSourceListener, "ActivityStop", "TransSpecsSource", "TransSpecs");

                    var list2 = eventSourceListener.LastEvent.Arguments;
                    Assert.Equal(a.OperationName, list2["OperationName"]);

                    Assert.Equal(list1.Count, list2.Count);
                    foreach (string key in list1.Keys)
                    {
                        Assert.NotNull(list2[key]);
                    }

                    // Suppress all
                    eventSourceListener.Enable("[AS]*:-");
                    a = aSource1.StartActivity("TransSpecs");
                    Assert.Equal(0, eventSourceListener.LastEvent.Arguments.Count);
                    a.Stop();
                    Assert.Equal(0, eventSourceListener.LastEvent.Arguments.Count);

                    // Suppress all except ActivitySource name
                    eventSourceListener.Enable("[AS]*:-ActivitySourceName=Source.Name");
                    a = aSource1.StartActivity("TransSpecs");
                    Assert.Equal(1, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal(aSource1.Name, eventSourceListener.LastEvent.Arguments["ActivitySourceName"]);

                    a.Stop();
                    Assert.Equal(1, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal(aSource1.Name, eventSourceListener.LastEvent.Arguments["ActivitySourceName"]);

                    // Collect TraceId, SpanId, and ParentSpanId only
                    eventSourceListener.Enable("[AS]*:-TraceId;SpanId;ParentSpanId");
                    a = aSource1.StartActivity("ActivityData");
                    Assert.Equal(3, eventSourceListener.LastEvent.Arguments.Count);

                    Assert.Equal(a.SpanId.ToString(), eventSourceListener.LastEvent.Arguments["SpanId"]);
                    Assert.Equal(a.TraceId.ToString(), eventSourceListener.LastEvent.Arguments["TraceId"]);
                    Assert.Equal(a.ParentSpanId.ToString(), eventSourceListener.LastEvent.Arguments["ParentSpanId"]);

                    a.Stop();
                    Assert.Equal(3, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal(a.SpanId.ToString(), eventSourceListener.LastEvent.Arguments["SpanId"]);
                    Assert.Equal(a.TraceId.ToString(), eventSourceListener.LastEvent.Arguments["TraceId"]);
                    Assert.Equal(a.ParentSpanId.ToString(), eventSourceListener.LastEvent.Arguments["ParentSpanId"]);
                }

            }).Dispose();
        }


        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public void TestFilteringWithActivityName()
        {
            RemoteExecutor.Invoke(() =>
            {
                using (TestDiagnosticSourceEventListener eventSourceListener = new TestDiagnosticSourceEventListener())
                {
                    using ActivitySource aSource1 = new ActivitySource("Source1");
                    using ActivitySource aSource2 = new ActivitySource("Source2");

                    eventSourceListener.Enable("[AS]*+MyActivity");
                    Assert.Equal(0, eventSourceListener.EventCount);

                    Activity a = aSource1.StartActivity("NotMyActivity"); // Shouldn't get created because nt matching MyActivity
                    Assert.Null(a);
                    Assert.Equal(0, eventSourceListener.EventCount);

                    a = aSource1.StartActivity("MyActivity");
                    Assert.NotNull(a);
                    Assert.Equal(1, eventSourceListener.EventCount);
                    Assert.Equal(a.OperationName, eventSourceListener.LastEvent.Arguments["OperationName"]);
                    a.Stop();
                    Assert.Equal(2, eventSourceListener.EventCount);
                    Assert.Equal(a.OperationName, eventSourceListener.LastEvent.Arguments["OperationName"]);

                    a = aSource2.StartActivity("MyActivity");
                    Assert.NotNull(a);
                    Assert.Equal(3, eventSourceListener.EventCount);
                    Assert.Equal(a.OperationName, eventSourceListener.LastEvent.Arguments["OperationName"]);
                    a.Stop();
                    Assert.Equal(4, eventSourceListener.EventCount);
                    Assert.Equal(a.OperationName, eventSourceListener.LastEvent.Arguments["OperationName"]);

                    //
                    // Now test with specific ActivitySource and ActivityName
                    //

                    eventSourceListener.Enable("[AS]MySource+MyActivity");
                    a = aSource1.StartActivity("MyActivity"); // Not from MySource
                    Assert.Null(a);
                    Assert.Equal(4, eventSourceListener.EventCount);
                    using ActivitySource aSource3 = new ActivitySource("MySource");
                    a = aSource3.StartActivity("NotMyActivity"); // from MySource but NoMyActivity
                    Assert.Null(a);
                    Assert.Equal(4, eventSourceListener.EventCount);

                    a = aSource3.StartActivity("MyActivity"); // from MySource and MyActivity
                    Assert.NotNull(a);
                    Assert.Equal(5, eventSourceListener.EventCount);
                    Assert.Equal(a.OperationName, eventSourceListener.LastEvent.Arguments["OperationName"]);

                    a.Stop();
                    Assert.Equal(6, eventSourceListener.EventCount);
                    Assert.Equal(a.OperationName, eventSourceListener.LastEvent.Arguments["OperationName"]);

                    //
                    // test with full query
                    //
                    eventSourceListener.Enable("[AS]MySource+MyActivity/Start-Propagate");
                    a = aSource3.StartActivity("MyActivity"); // from MySource and MyActivity
                    Assert.NotNull(a);
                    Assert.Equal(7, eventSourceListener.EventCount);
                    Assert.Equal(a.OperationName, eventSourceListener.LastEvent.Arguments["OperationName"]);
                    Assert.False(a.IsAllDataRequested);

                    a.Stop(); // shouldn't fire
                    Assert.Equal(7, eventSourceListener.EventCount);

                    //
                    // test with default Source
                    //
                    eventSourceListener.Enable("[AS]+MyActivity");
                    a = new Activity("MyActivity");
                    a.Start();
                    Assert.Equal(8, eventSourceListener.EventCount);
                    Assert.Equal(a.OperationName, eventSourceListener.LastEvent.Arguments["OperationName"]);
                    Assert.True(a.IsAllDataRequested);

                    a.Stop();
                    Assert.Equal(9, eventSourceListener.EventCount);
                    Assert.Equal(a.OperationName, eventSourceListener.LastEvent.Arguments["OperationName"]);

                    a = new Activity("NotMyActivity");
                    a.Start(); // nothing fire
                    Assert.Equal(9, eventSourceListener.EventCount);

                    //
                    // Test with Empty ActivityName
                    //
                    eventSourceListener.Enable("[AS]+");
                    a = new Activity("");
                    a.Start();
                    Assert.Equal(10, eventSourceListener.EventCount);
                    a.Stop();
                    Assert.Equal(11, eventSourceListener.EventCount);

                    a = aSource3.StartActivity("");
                    Assert.Null(a);
                }

            }).Dispose();
        }

        internal void ValidateActivityEvents(TestDiagnosticSourceEventListener eventSourceListener, string eventName, string sourceName, string activityName)
        {
            Assert.Equal(eventName, eventSourceListener.LastEvent.EventSourceEventName);
            Assert.Equal(sourceName, eventSourceListener.LastEvent.SourceName);
            Assert.Equal(activityName, eventSourceListener.LastEvent.EventName);
        }

        /// <summary>
        /// Tests the basic functionality of turning on specific EventSources and specifying
        /// the events you want.
        /// </summary>
        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public void TestSpecificEvents()
        {
            RemoteExecutor.Invoke(() =>
            {
                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"));
                }));
            }).Dispose();
        }

        /// <summary>
        /// Tests that DiagnosticSourceEventSource can read property values from base classes
        /// </summary>
        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public void TestBaseClassProperties()
        {
            RemoteExecutor.Invoke(() =>
            {
                using (var eventSourceListener = new TestDiagnosticSourceEventListener())
                using (var diagnosticSourceListener = new DiagnosticListener("TestBaseClassProperties"))
                {
                    Assert.Equal(0, eventSourceListener.EventCount);
                    eventSourceListener.Enable(
                        "  TestBaseClassProperties/TestEvent1:Point_X=Point.X;Point_Y=Point.Y;Url_2=Url2\r\n");

                    /***************************************************************************************/
                    // Emit an event that matches the first pattern.
                    MyClass val = new MyDerivedClass() { Url = "MyUrl", Point = new MyPoint() { X = 3, Y = 5 }, Url2 = "Second url", AnotherString = "another" };
                    if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                        diagnosticSourceListener.Write("TestEvent1", val);

                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("TestBaseClassProperties", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(7, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal("another", eventSourceListener.LastEvent.Arguments["AnotherString"]);
                    Assert.Equal("3", eventSourceListener.LastEvent.Arguments["Point_X"]);
                    Assert.Equal("5", eventSourceListener.LastEvent.Arguments["Point_Y"]);
                    Assert.Equal("Second url", eventSourceListener.LastEvent.Arguments["Url_2"]);
                    eventSourceListener.ResetEventCountAndLastEvent();
                }
            }).Dispose();
        }

        /// <summary>
        /// Test that things work properly for Linux newline conventions.
        /// </summary>
        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public void LinuxNewLineConventions()
        {
            RemoteExecutor.Invoke(() =>
            {
                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"));
                }));
            }).Dispose();
        }

        /// <summary>
        /// Tests what happens when you wildcard the source name (empty string)
        /// </summary>
        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public void TestWildCardSourceName()
        {
            RemoteExecutor.Invoke(() =>
            {
                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();
                }
            }).Dispose();
        }

        /// <summary>
        /// Tests what happens when you wildcard event name (but not the source name)
        /// </summary>
        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public void TestWildCardEventName()
        {
            RemoteExecutor.Invoke(() =>
            {
                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 with the same schema as the first event.   (uses first-event cache)
                    val = new MyClass() { };
                    if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                        diagnosticSourceListener.Write("TestEvent1", new { propStr = "hiThere", propInt = 5, 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("hiThere", eventSourceListener.LastEvent.Arguments["propStr"]);
                    Assert.Equal("5", eventSourceListener.LastEvent.Arguments["propInt"]);
                    eventSourceListener.ResetEventCountAndLastEvent();

                    /***************************************************************************************/
                    // Emit an event with the same schema as the second event.  (uses dictionary cache)
                    if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                        diagnosticSourceListener.Write("TestEvent1", new { propStr2 = "hi3", 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("hi3", 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.
                }
            }).Dispose();
        }

        public class PropertyThrow
        {
            public string property1 => "P1";
            public string property2 => throw new Exception("Always throw!");
            public string property3 => "P3";
        }

        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public void TestWithPropertyThrowing()
        {
            RemoteExecutor.Invoke(() =>
            {
                using (var eventSourceListener = new TestDiagnosticSourceEventListener())
                using (var diagnosticSourceListener = new DiagnosticListener("TestThrows"))
                {
                    eventSourceListener.Enable("TestThrows/TestEvent1");

                    if (diagnosticSourceListener.IsEnabled("TestEvent1"))
                        diagnosticSourceListener.Write("TestEvent1", new PropertyThrow());

                    Assert.Equal(1, eventSourceListener.EventCount); // Exactly one more event has been emitted.
                    Assert.Equal("TestThrows", eventSourceListener.LastEvent.SourceName);
                    Assert.Equal("TestEvent1", eventSourceListener.LastEvent.EventName);
                    Assert.Equal(3, eventSourceListener.LastEvent.Arguments.Count);
                    Assert.Equal("P1", eventSourceListener.LastEvent.Arguments["property1"]);
                    Assert.Equal("P3", eventSourceListener.LastEvent.Arguments["property3"]);
                    Assert.Equal("", eventSourceListener.LastEvent.Arguments["property2"]);
                    eventSourceListener.ResetEventCountAndLastEvent();
                }
            }).Dispose();
        }

        /// <summary>
        /// Test what happens when there are nulls passed in the event payloads
        /// Basically strings get turned into empty strings and other nulls are typically
        /// ignored.
        /// </summary>
        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public void TestNulls()
        {
            RemoteExecutor.Invoke(() =>
            {
                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();
                }
            }).Dispose();
        }

        /// <summary>
        /// Tests the feature that suppresses the implicit inclusion of serialable properties
        /// of the payload object.
        /// </summary>
        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public void TestNoImplicitTransforms()
        {
            RemoteExecutor.Invoke(() =>
            {
                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();
                }
            }).Dispose();
        }

        /// <summary>
        /// Tests what happens when wacky characters are used in property specs.
        /// </summary>
        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public void TestBadProperties()
        {
            RemoteExecutor.Invoke(() =>
            {
                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();
                }
            }).Dispose();
        }

        // Tests that messages about DiagnosticSourceEventSource make it out.
        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public void TestMessages()
        {
            RemoteExecutor.Invoke(() =>
            {
                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);
                }
            }).Dispose();
        }

        /// <summary>
        /// Tests the feature to send the messages as EventSource Activities.
        /// </summary>
        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public void TestActivities()
        {
            RemoteExecutor.Invoke(() =>
            {
                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();
                }
            }).Dispose();
        }

        /// <summary>
        /// Tests that keywords that define shortcuts work.
        /// </summary>
        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public void TestShortcutKeywords()
        {
            RemoteExecutor.Invoke(() =>
            {
                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"))
                {
                    // Sadly we have a problem where if something else has turned on Microsoft-Diagnostics-DiagnosticSource then
                    // its keywords are ORed with our and because the shortcuts require that IgnoreShortCutKeywords is OFF
                    // Something outside this test (the debugger seems to do this), will cause the test to fail.
                    // Currently we simply give up in that case (but it really is a deeper problem.
                    var IgnoreShortCutKeywords = (EventKeywords)0x0800;
                    foreach (var eventSource in EventSource.GetSources())
                    {
                        if (eventSource.Name == "Microsoft-Diagnostics-DiagnosticSource")
                        {
                            if (eventSource.IsEnabled(EventLevel.Informational, IgnoreShortCutKeywords))
                                return; // Don't do the testing.
                        }
                    }

                    // 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 request.
                    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();
                }
            }).Dispose();
        }

        [OuterLoop("Runs for several seconds")]
        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public void Stress_WriteConcurrently_DoesntCrash()
        {
            const int StressTimeSeconds = 4;
            RemoteExecutor.Invoke(() =>
            {
                using (new TurnOnAllEventListener())
                using (var source = new DiagnosticListener("testlistener"))
                {
                    var ce = new CountdownEvent(Environment.ProcessorCount * 2);
                    for (int i = 0; i < ce.InitialCount; i++)
                    {
                        new Thread(() =>
                        {
                            DateTime end = DateTime.UtcNow.Add(TimeSpan.FromSeconds(StressTimeSeconds));
                            while (DateTime.UtcNow < end)
                            {
                                source.Write("event1", Tuple.Create(1));
                                source.Write("event2", Tuple.Create(1, 2));
                                source.Write("event3", Tuple.Create(1, 2, 3));
                                source.Write("event4", Tuple.Create(1, 2, 3, 4));
                                source.Write("event5", Tuple.Create(1, 2, 3, 4, 5));
                            }
                            ce.Signal();
                        })
                        { IsBackground = true }.Start();
                    }
                    ce.Wait();
                }
            }).Dispose();
        }

        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public void IndexGetters_DontThrow()
        {
            RemoteExecutor.Invoke(() =>
            {
                using (var eventListener = new TestDiagnosticSourceEventListener())
                using (var diagnosticListener = new DiagnosticListener("MySource"))
                {
                    eventListener.Enable(
                        "MySource/MyEvent"
                    );
                    // The type MyEvent only declares 3 Properties, but actually
                    // has 4 due to the implicit Item property from having the index
                    // operator implemented. The Getter for this Item property
                    // is unusual for Property getters because it takes
                    // an int32 as an input. This test ensures that this
                    // implicit Property isn't implicitly serialized by
                    // DiagnosticSourceEventSource.
                    diagnosticListener.Write(
                        "MyEvent",
                        new MyEvent
                        {
                            Number = 1,
                            OtherNumber = 2
                        }
                    );
                    Assert.Equal(1, eventListener.EventCount);
                    Assert.Equal("MySource", eventListener.LastEvent.SourceName);
                    Assert.Equal("MyEvent", eventListener.LastEvent.EventName);
                    Assert.True(eventListener.LastEvent.Arguments.Count <= 3);
                    Assert.Equal("1", eventListener.LastEvent.Arguments["Number"]);
                    Assert.Equal("2", eventListener.LastEvent.Arguments["OtherNumber"]);
                    Assert.Equal("2", eventListener.LastEvent.Arguments["Count"]);
                }
            }).Dispose();
        }

        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
        public void ActivityObjectsAreInspectable()
        {
            RemoteExecutor.Invoke(() =>
            {
                using (var eventListener = new TestDiagnosticSourceEventListener())
                using (var diagnosticListener = new DiagnosticListener("MySource"))
                {
                    string activityProps =
                        "-DummyProp" +
                        ";ActivityId=*Activity.Id" +
                        ";ActivityStartTime=*Activity.StartTimeUtc.Ticks" +
                        ";ActivityDuration=*Activity.Duration.Ticks" +
                        ";ActivityOperationName=*Activity.OperationName" +
                        ";ActivityIdFormat=*Activity.IdFormat" +
                        ";ActivityParentId=*Activity.ParentId" +
                        ";ActivityTags=*Activity.Tags.*Enumerate" +
                        ";ActivityTraceId=*Activity.TraceId" +
                        ";ActivitySpanId=*Activity.SpanId" +
                        ";ActivityTraceStateString=*Activity.TraceStateString" +
                        ";ActivityParentSpanId=*Activity.ParentSpanId";
                    eventListener.Enable(
                        "MySource/TestActivity1.Start@Activity1Start:" + activityProps + "\r\n" +
                        "MySource/TestActivity1.Stop@Activity1Stop:" + activityProps + "\r\n" +
                        "MySource/TestActivity2.Start@Activity2Start:" + activityProps + "\r\n" +
                        "MySource/TestActivity2.Stop@Activity2Stop:" + activityProps + "\r\n"
                        );

                    Activity activity1 = new Activity("TestActivity1");
                    activity1.SetIdFormat(ActivityIdFormat.W3C);
                    activity1.TraceStateString = "hi_there";
                    activity1.AddTag("one", "1");
                    activity1.AddTag("two", "2");

                    diagnosticListener.StartActivity(activity1, new { DummyProp = "val" });
                    Assert.Equal(1, eventListener.EventCount);
                    AssertActivityMatchesEvent(activity1, eventListener.LastEvent, isStart: true);

                    Activity activity2 = new Activity("TestActivity2");
                    diagnosticListener.StartActivity(activity2, new { DummyProp = "val" });
                    Assert.Equal(2, eventListener.EventCount);
                    AssertActivityMatchesEvent(activity2, eventListener.LastEvent, isStart: true);

                    diagnosticListener.StopActivity(activity2, new { DummyProp = "val" });
                    Assert.Equal(3, eventListener.EventCount);
                    AssertActivityMatchesEvent(activity2, eventListener.LastEvent, isStart: false);

                    diagnosticListener.StopActivity(activity1, new { DummyProp = "val" });
                    Assert.Equal(4, eventListener.EventCount);
                    AssertActivityMatchesEvent(activity1, eventListener.LastEvent, isStart: false);

                }
            }).Dispose();
        }

        private void AssertActivityMatchesEvent(Activity a, DiagnosticSourceEvent e, bool isStart)
        {
            Assert.Equal("MySource", e.SourceName);
            Assert.Equal(a.OperationName + (isStart ? ".Start" : ".Stop"), e.EventName);
            Assert.Equal("val", e.Arguments["DummyProp"]);
            Assert.Equal(a.Id, e.Arguments["ActivityId"]);
            Assert.Equal(a.StartTimeUtc.Ticks.ToString(), e.Arguments["ActivityStartTime"]);
            if (!isStart)
            {
                Assert.Equal(a.Duration.Ticks.ToString(), e.Arguments["ActivityDuration"]);
            }
            Assert.Equal(a.OperationName, e.Arguments["ActivityOperationName"]);
            if (a.ParentId == null)
            {
                Assert.True(!e.Arguments.ContainsKey("ActivityParentId"));
            }
            else
            {
                Assert.Equal(a.ParentId, e.Arguments["ActivityParentId"]);
            }
            Assert.Equal(a.IdFormat.ToString(), e.Arguments["ActivityIdFormat"]);
            if (a.IdFormat == ActivityIdFormat.W3C)
            {
                Assert.Equal(a.TraceId.ToString(), e.Arguments["ActivityTraceId"]);
                Assert.Equal(a.SpanId.ToString(), e.Arguments["ActivitySpanId"]);
                Assert.Equal(a.TraceStateString, e.Arguments["ActivityTraceStateString"]);
                if(a.ParentSpanId != default)
                {
                    Assert.Equal(a.ParentSpanId.ToString(), e.Arguments["ActivityParentSpanId"]);
                }
            }
            Assert.Equal(string.Join(',', a.Tags), e.Arguments["ActivityTags"]);
        }

        [Fact]
        [ActiveIssue("https://github.com/dotnet/runtime/issues/50924", TestPlatforms.Android)]
        public void NoExceptionThrownWhenProcessingStaticActivityProperties()
        {
            // Ensures that no exception is thrown when static properties on the Activity type are passed to EventListener.

            using (var eventListener = new TestDiagnosticSourceEventListener())
            using (var diagnosticListener = new DiagnosticListener("MySource"))
            {
                string activityProps =
                "-DummyProp" +
                ";ActivityEvents=*Activity.DefaultIdFormat" +
                ";ActivityBaggage=*Activity.Current" +
                ";ActivityContext=*Activity.ForceDefaultIdFormat";
                eventListener.Enable(
                    "MySource/TestActivity1.Start@Activity1Start:" + activityProps + "\r\n" +
                    "MySource/TestActivity1.Stop@Activity1Stop:" + activityProps + "\r\n" +
                    "MySource/TestActivity2.Start@Activity2Start:" + activityProps + "\r\n" +
                    "MySource/TestActivity2.Stop@Activity2Stop:" + activityProps + "\r\n"
                    );

                Activity activity1 = new Activity("TestActivity1");
                activity1.SetIdFormat(ActivityIdFormat.W3C);
                activity1.TraceStateString = "hi_there";
                activity1.AddTag("one", "1");
                activity1.AddTag("two", "2");

                var obj = new { DummyProp = "val" };

                diagnosticListener.StartActivity(activity1, obj);
                Assert.Equal(1, eventListener.EventCount);
            }
        }
    }

    /****************************************************************************/
    // classes to make up data for the tests.

    /// <summary>
    /// classes for test data.
    /// </summary>
    internal class MyClass
    {
        public string Url { get; set; }
        public MyPoint Point { get; set; }
    }

    internal class MyDerivedClass : MyClass
    {
        public string Url2 { get; set; }
        public string AnotherString { get; set; }
    }

    /// <summary>
    /// classes for test data.
    /// </summary>
    internal class MyPoint
    {
        public int X { get; set; }
        public int Y { get; set; }
    }

    /// <summary>
    /// classes for test data
    /// </summary>
    internal class MyEvent
    {
        public int Number { get; set; }
        public int OtherNumber { get; set; }
        public int Count => 2;
        public KeyValuePair<string, object> this[int index] => index switch
        {
            0 => new KeyValuePair<string, object>(nameof(Number), Number),
            1 => new KeyValuePair<string, object>(nameof(OtherNumber), OtherNumber),
            _ => throw new IndexOutOfRangeException()
        };
    }

    /****************************************************************************/
    // Harness infrastructure
    /// <summary>
    /// TestDiagnosticSourceEventListener installs a EventWritten callback that updates EventCount and LastEvent.
    /// </summary>
    internal sealed class TestDiagnosticSourceEventListener : DiagnosticSourceEventListener
    {
        public TestDiagnosticSourceEventListener()
        {
            EventWritten += UpdateLastEvent;
        }

        public int EventCount;
        public DiagnosticSourceEvent LastEvent;

        // Here just for debugging.  Lets you see the last 3 events that were sent.
        public DiagnosticSourceEvent SecondLast;
        public DiagnosticSourceEvent ThirdLast;

        /// <summary>
        /// Sets the EventCount to 0 and LastEvents to null
        /// </summary>
        public void ResetEventCountAndLastEvent()
        {
            EventCount = 0;
            LastEvent = null;
            SecondLast = null;
            ThirdLast = null;
        }

        /// <summary>
        /// If present, will ignore events that don't cause this filter predicate to return true.
        /// </summary>
        public Predicate<DiagnosticSourceEvent> Filter;

        #region private
        private void UpdateLastEvent(DiagnosticSourceEvent anEvent)
        {
            if (Filter != null && !Filter(anEvent))
                return;

            ThirdLast = SecondLast;
            SecondLast = LastEvent;

            EventCount++;
            LastEvent = anEvent;
        }
        #endregion
    }

    /// <summary>
    /// Represents a single DiagnosticSource event.
    /// </summary>
    internal sealed class DiagnosticSourceEvent
    {
        public string SourceName;
        public string EventName;
        public Dictionary<string, string> Arguments;

        // Not typically important.
        public string EventSourceEventName;    // This is the name of the EventSourceEvent that carried the data. Only important for activities.

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("{");
            sb.Append("  SourceName: \"").Append(SourceName ?? "").Append("\",").AppendLine();
            sb.Append("  EventName: \"").Append(EventName ?? "").Append("\",").AppendLine();
            sb.Append("  Arguments: ").Append("[").AppendLine();
            bool first = true;
            foreach (var keyValue in Arguments)
            {
                if (!first)
                    sb.Append(",").AppendLine();
                first = false;
                sb.Append("    ").Append(keyValue.Key).Append(": \"").Append(keyValue.Value).Append("\"");
            }
            sb.AppendLine().AppendLine("  ]");
            sb.AppendLine("}");
            return sb.ToString();
        }
    }

    /// <summary>
    /// A helper class that listens to Diagnostic sources and send events to the 'EventWritten'
    /// callback.   Standard use is to create the class set up the events of interested and
    /// use 'Enable'.  .
    /// </summary>
    internal class DiagnosticSourceEventListener : EventListener
    {
        public DiagnosticSourceEventListener() { }

        /// <summary>
        /// Will be called when a DiagnosticSource event is fired.
        /// </summary>
        public new event Action<DiagnosticSourceEvent> EventWritten;

        /// <summary>
        /// It is possible that there are other events besides those that are being forwarded from
        /// the DiagnosticSources.   These come here.
        /// </summary>
        public event Action<EventWrittenEventArgs> OtherEventWritten;

        public void Enable(string filterAndPayloadSpecs, EventKeywords keywords = EventKeywords.All)
        {
            var args = new Dictionary<string, string>();
            if (filterAndPayloadSpecs != null)
                args.Add("FilterAndPayloadSpecs", filterAndPayloadSpecs);
            EnableEvents(_diagnosticSourceEventSource, EventLevel.Verbose, keywords, args);
        }

        public void Disable()
        {
            DisableEvents(_diagnosticSourceEventSource);
        }

        /// <summary>
        /// Cleans this class up.  Among other things disables the DiagnosticSources being listened to.
        /// </summary>
        public override void Dispose()
        {
            if (_diagnosticSourceEventSource != null)
            {
                Disable();
                _diagnosticSourceEventSource = null;
            }
        }

        #region private
        protected override void OnEventWritten(EventWrittenEventArgs eventData)
        {
            bool wroteEvent = false;
            var eventWritten = EventWritten;
            if (eventWritten != null)
            {
                if (eventData.Payload.Count == 3 && (eventData.EventName == "Event" || eventData.EventName.Contains("Activity")))
                {
                    Debug.Assert(eventData.PayloadNames[0] == "SourceName");
                    Debug.Assert(eventData.PayloadNames[1] == "EventName" || eventData.PayloadNames[1] == "ActivityName");
                    Debug.Assert(eventData.PayloadNames[2] == "Arguments");

                    var anEvent = new DiagnosticSourceEvent();
                    anEvent.EventSourceEventName = eventData.EventName;
                    anEvent.SourceName = eventData.Payload[0].ToString();
                    anEvent.EventName = eventData.Payload[1].ToString();
                    anEvent.Arguments = new Dictionary<string, string>();

                    var asKeyValueList = eventData.Payload[2] as IEnumerable<object>;
                    if (asKeyValueList != null)
                    {
                        foreach (IDictionary<string, object> keyvalue in asKeyValueList)
                        {
                            object key;
                            keyvalue.TryGetValue("Key", out key);
                            object value;
                            keyvalue.TryGetValue("Value", out value);
                            if (key != null && value != null)
                                anEvent.Arguments[key.ToString()] = value.ToString();
                        }
                    }
                    eventWritten(anEvent);
                    wroteEvent = true;
                }
            }

            if (eventData.EventName == "EventSourceMessage" && 0 < eventData.Payload.Count)
                System.Diagnostics.Debug.WriteLine("EventSourceMessage: " + eventData.Payload[0].ToString());

            var otherEventWritten = OtherEventWritten;
            if (otherEventWritten != null && !wroteEvent)
                otherEventWritten(eventData);
        }

        protected override void OnEventSourceCreated(EventSource eventSource)
        {
            if (eventSource.Name == "Microsoft-Diagnostics-DiagnosticSource")
                _diagnosticSourceEventSource = eventSource;
        }

        EventSource _diagnosticSourceEventSource;
        #endregion
    }

    internal sealed class TurnOnAllEventListener : EventListener
    {
        protected override void OnEventSourceCreated(EventSource eventSource) => EnableEvents(eventSource, EventLevel.LogAlways);
        protected override void OnEventWritten(EventWrittenEventArgs eventData) { }
    }
}