44using Microsoft . Build . Framework ;
55using System . Threading ;
66using System . Collections ;
7+ using System . Collections . Generic ;
78using System . Reflection ;
89
910[ assembly: AssemblyKeyFileAttribute ( "product.snk" ) ]
@@ -21,10 +22,12 @@ public class AsyncTask : Task, ICancelableTask
2122 readonly Queue warningMessageQueue = new Queue ( ) ;
2223 readonly Queue errorMessageQueue = new Queue ( ) ;
2324 readonly Queue customMessageQueue = new Queue ( ) ;
25+ readonly Queue telemetryMessageQueue = new Queue ( ) ;
2426 readonly ManualResetEvent logDataAvailable = new ManualResetEvent ( false ) ;
2527 readonly ManualResetEvent errorDataAvailable = new ManualResetEvent ( false ) ;
2628 readonly ManualResetEvent warningDataAvailable = new ManualResetEvent ( false ) ;
2729 readonly ManualResetEvent customDataAvailable = new ManualResetEvent ( false ) ;
30+ readonly ManualResetEvent telemetryDataAvailable = new ManualResetEvent ( false ) ;
2831 readonly ManualResetEvent taskCancelled = new ManualResetEvent ( false ) ;
2932 readonly ManualResetEvent completed = new ManualResetEvent ( false ) ;
3033 bool isRunning = true ;
@@ -96,6 +99,23 @@ public void LogDebugTaskItems(string message, ITaskItem[] items)
9699 LogDebugMessage ( " {0}" , item . ItemSpec ) ;
97100 }
98101
102+ public void LogTelemetry ( string eventName , IDictionary < string , string > properties )
103+ {
104+ if ( uiThreadId == Thread . CurrentThread . ManagedThreadId )
105+ {
106+ #pragma warning disable 618
107+ Log . LogTelemetry ( eventName , properties ) ;
108+ return ;
109+ #pragma warning restore 618
110+ }
111+
112+ var data = new TelemetryEventArgs ( ) {
113+ EventName = eventName ,
114+ Properties = properties
115+ } ;
116+ EnqueueMessage ( telemetryMessageQueue , data , telemetryDataAvailable ) ;
117+ }
118+
99119 public void LogMessage ( string message ) => LogMessage ( message , importance : MessageImportance . Normal ) ;
100120
101121 public void LogMessage ( string message , params object [ ] messageArgs ) => LogMessage ( string . Format ( message , messageArgs ) ) ;
@@ -277,6 +297,7 @@ protected void WaitForCompletion()
277297 errorDataAvailable ,
278298 warningDataAvailable ,
279299 customDataAvailable ,
300+ telemetryDataAvailable ,
280301 taskCancelled ,
281302 completed ,
282303 } ;
@@ -333,6 +354,11 @@ protected void WaitForCompletion()
333354 BuildEngine . LogCustomEvent ( e ) ;
334355 } , customDataAvailable ) ;
335356 break ;
357+ case WaitHandleIndex . TelemetryDataAvailable :
358+ LogInternal < TelemetryEventArgs > ( telemetryMessageQueue , ( e ) => {
359+ BuildEngine5 . LogTelemetry ( e . EventName , e . Properties ) ;
360+ } , telemetryDataAvailable ) ;
361+ break ;
336362 case WaitHandleIndex . TaskCancelled :
337363 Cancel ( ) ;
338364 cts . Cancel ( ) ;
@@ -357,6 +383,7 @@ private enum WaitHandleIndex
357383 ErrorDataAvailable ,
358384 WarningDataAvailable ,
359385 CustomDataAvailable ,
386+ TelemetryDataAvailable ,
360387 TaskCancelled ,
361388 Completed ,
362389 }
0 commit comments