Skip to content
This repository was archived by the owner on Dec 21, 2024. It is now read-only.

Commit db4ce14

Browse files
authored
Add Telemetry support. Bump to MSbuild 17+ (#15)
* Add Telemetry support. Bump to MSbuild 17+ IBuildEngine5 added support for sending Telemetry data https://learn.microsoft.com/en-us/dotnet/api/microsoft.build.framework.ibuildengine5.logtelemetry?view=msbuild-17-netcore. We need to expose a helper method in the AsyncTask so that tasks using it can log telemetry. We have to use the same pattern as we do for other logging event, the data should be queue'd and then disapatched on the main UI thread so that we can prevent locking.
1 parent 166fbe8 commit db4ce14

File tree

6 files changed

+47
-13
lines changed

6 files changed

+47
-13
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ If you're creating an inline task that wants to inherit from AsyncTask, use the
2323
as a template:
2424

2525
```xml
26-
<UsingTask TaskName="MyAsyncTask" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
26+
<UsingTask TaskName="MyAsyncTask" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
2727
<ParameterGroup>
2828
<!-- TODO: your task parameters -->
2929
</ParameterGroup>
3030
<Task>
3131
<Reference Include="$(AsyncTask)" />
32-
<Reference Include="System.Threading.Tasks"/>
32+
<Reference Include="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll"/>
33+
<Reference Include="$(MSBuildToolsPath)\Microsoft.Build.Utilities.Core.dll"/>
3334
<Code Type="Class" Language="cs">
3435
<![CDATA[
3536
public class MyAsyncTask : Xamarin.Build.AsyncTask

Xamarin.Build.AsyncTask/AsyncTask.cs

+27
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Build.Framework;
55
using System.Threading;
66
using System.Collections;
7+
using System.Collections.Generic;
78
using 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
}

Xamarin.Build.AsyncTask/Readme.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ If you're creating a custom task library, just inherit from Xamarin.Build.AsyncT
55
If you're creating an inline task that wants to inherit from AsyncTask, use the following
66
as a template:
77

8-
<UsingTask TaskName="MyAsyncTask" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
8+
<UsingTask TaskName="MyAsyncTask" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
99
<ParameterGroup>
1010
<!-- TODO: your task parameters -->
1111
</ParameterGroup>
1212
<Task>
1313
<Reference Include="$(AsyncTask)" />
14-
<Reference Include="System.Threading.Tasks"/>
14+
<Reference Include="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll"/>
15+
<Reference Include="$(MSBuildToolsPath)\Microsoft.Build.Utilities.Core.dll"/>
1516
<Code Type="Class" Language="cs">
1617
<![CDATA[
1718
public class MyAsyncTask : Xamarin.Build.AsyncTask

Xamarin.Build.AsyncTask/Test.targets

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99
<AsyncMessage Text="Hello Async World!" />
1010
</Target>
1111

12-
<UsingTask TaskName="AsyncMessage" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
12+
<UsingTask TaskName="AsyncMessage" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)/Microsoft.Build.Tasks.Core.dll">
1313
<ParameterGroup>
1414
<Text Required="true" />
1515
</ParameterGroup>
1616
<Task>
1717
<Reference Include="$(OutputPath)$(AssemblyName).dll" />
18-
<Reference Include="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll"/>
19-
<Reference Include="$(MSBuildToolsPath)\Microsoft.Build.Utilities.Core.dll"/>
20-
<Reference Include="System.Threading.Tasks"/>
18+
<Reference Include="$(MSBuildToolsPath)/Microsoft.Build.Tasks.Core.dll"/>
19+
<Reference Include="$(MSBuildToolsPath)/Microsoft.Build.Utilities.Core.dll"/>
2120
<Code Type="Class" Language="cs">
2221
<![CDATA[
2322
public class AsyncMessage : Xamarin.Build.AsyncTask
@@ -31,7 +30,9 @@
3130
await System.Threading.Tasks.Task.Delay(5000);
3231
LogMessage(Text);
3332
Complete();
34-
});
33+
});
34+
35+
LogTelemetry("Test", new System.Collections.Generic.Dictionary<string, string> () {{"Property", "Value"}});
3536
3637
return base.Execute();
3738
}

Xamarin.Build.AsyncTask/Xamarin.Build.AsyncTask.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<Title>$(PackageId)</Title>
1313
<Description>$(PackageId)</Description>
14-
<Summary>Supports MSBuild 14+.</Summary>
14+
<Summary>Supports MSBuild 17+.</Summary>
1515
<Authors>Microsoft</Authors>
1616
<Owners>microsoft xamarin</Owners>
1717
<PackageReleaseNotes>$([System.IO.File]::ReadAllText('$(MSBuildThisFileDirectory)Readme.txt'))</PackageReleaseNotes>
@@ -29,8 +29,8 @@
2929
</PropertyGroup>
3030

3131
<ItemGroup>
32-
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="15.6.82" />
33-
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.6.82" />
32+
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="17.9.5" />
33+
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.9.5" />
3434

3535
<PackageReference Include="ThisAssembly.Metadata" Version="1.4.0">
3636
<PrivateAssets>all</PrivateAssets>
@@ -47,7 +47,7 @@
4747
<PackageFile Include="$(OutputPath)$(AssemblyName).pdb" Kind="lib" />
4848
<PackageFile Include="$(OutputPath)$(AssemblyName).xml" Kind="lib" />
4949
<PackageFile Include="Readme.txt" />
50-
<PackageFile Include="Microsoft.Build.Tasks.Core;Microsoft.Build.Utilities.Core" Version="14.3.0" Kind="Dependency" Visible="false" />
50+
<PackageFile Include="Microsoft.Build.Tasks.Core;Microsoft.Build.Utilities.Core" Version="17.0.0" Kind="Dependency" Visible="false" />
5151
</ItemGroup>
5252

5353
<Import Project="Version.targets" />

azure-pipelines.yml

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ extends:
6969
& dotnet build Xamarin.Build.AsyncTask\Xamarin.Build.AsyncTask.csproj -c Release -bl:$(LogDirectory)\Release.binlog
7070
displayName: build libraries
7171
errorActionPreference: stop
72+
- powershell: |
73+
& dotnet build Xamarin.Build.AsyncTask\Xamarin.Build.AsyncTask.csproj -c Release -t:Test -bl:$(LogDirectory)\Test.binlog
74+
displayName: test libraries
75+
errorActionPreference: stop
7276
- powershell: |
7377
& dotnet pack Xamarin.Build.AsyncTask\Xamarin.Build.AsyncTask.csproj -c Release -bl:$(LogDirectory)\PackRelease.binlog
7478
displayName: pack NuGet

0 commit comments

Comments
 (0)