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