1+ // Licensed to the .NET Foundation under one or more agreements.
2+ // The .NET Foundation licenses this file to you under the MIT license.
3+
4+ using System ;
5+ using System . Collections . Generic ;
6+ using System . Diagnostics ;
7+ using System . Diagnostics . Tracing ;
8+ using System . Threading ;
9+ using Tracing . UserEvents . Tests . Common ;
10+ using Microsoft . Diagnostics . Tracing ;
11+
12+ namespace Tracing . UserEvents . Tests . ManagedEvent
13+ {
14+ public class ManagedEvent
15+ {
16+ public static void ManagedEventTracee ( )
17+ {
18+ long startTimestamp = Stopwatch . GetTimestamp ( ) ;
19+ long targetTicks = Stopwatch . Frequency ; // 1s
20+
21+ while ( Stopwatch . GetTimestamp ( ) - startTimestamp < targetTicks )
22+ {
23+ ManagedUserEventSource . Log . SampleEvent ( "SampleWork" ) ;
24+ Thread . Sleep ( 100 ) ;
25+ }
26+ }
27+
28+ private static readonly Func < EventPipeEventSource , bool > s_traceValidator = source =>
29+ {
30+ bool sampleEventFound = false ;
31+
32+ source . Dynamic . All += ( TraceEvent e ) =>
33+ {
34+ if ( ! string . Equals ( e . ProviderName , "ManagedUserEvent" , StringComparison . OrdinalIgnoreCase ) )
35+ {
36+ return ;
37+ }
38+
39+ if ( e . EventName is null )
40+ {
41+ return ;
42+ }
43+
44+ sampleEventFound = true ;
45+ } ;
46+
47+ source . Process ( ) ;
48+
49+ if ( ! sampleEventFound )
50+ {
51+ Console . Error . WriteLine ( "The trace did not contain the expected managed event." ) ;
52+ }
53+
54+ return sampleEventFound ;
55+ } ;
56+
57+ public static int Main ( string [ ] args )
58+ {
59+ if ( args . Length > 0 && args [ 0 ] . Equals ( "tracee" , StringComparison . OrdinalIgnoreCase ) )
60+ {
61+ ManagedEventTracee ( ) ;
62+ return 0 ;
63+ }
64+
65+ return UserEventsTestRunner . Run ( "managedevent" , typeof ( ManagedEvent ) . Assembly . Location , s_traceValidator ) ;
66+ }
67+ }
68+
69+ [ EventSource ( Name = "ManagedUserEvent" ) ]
70+ internal sealed class ManagedUserEventSource : EventSource
71+ {
72+ public static readonly ManagedUserEventSource Log = new ManagedUserEventSource ( ) ;
73+
74+ [ Event ( 1 ) ]
75+ public void SampleEvent ( string requestName ) => WriteEvent ( 1 , requestName ) ;
76+ }
77+ }
0 commit comments