Skip to content

Commit

Permalink
Add telemetry data points for .NET Core 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
William Li committed Jul 27, 2017
1 parent 48a3c4b commit f078aca
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ Copyright (c) .NET Foundation. All rights reserved.
FormatArguments="$([MSBuild]::Escape('$(TargetFramework)'))" />
</Target>

<Target Name="_CollectTargetFrameworkForTelemetry" AfterTargets="_CheckForUnsupportedTargetFramework">
<Telemetry EventName="TargetFramework" EventData="version=$([MSBuild]::Escape('$(TargetFrameworkMoniker)'))" />
</Target>

<!--
Don't leave TargetFrameworkVersion empty if it still hasn't been determined. We will trigger the error above,
but we need this to be a valid version so that our error message does not get pre-empted by failure to interpret
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using FluentAssertions;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using Microsoft.NET.TestFramework.ProjectConstruction;
using System;
using System.IO;
using Xunit;
using Xunit.Abstractions;
using System.Reflection;

namespace Microsoft.NET.Build.Tests
{
public class GivenThatWeWantToBuildANetCoreAppAndPassingALogger : SdkTest
{
public GivenThatWeWantToBuildANetCoreAppAndPassingALogger(ITestOutputHelper log) : base(log)
{
}

[CoreMSBuildOnlyFact]
public void It_collects_TargetFramework_version()
{
string targetFramework = "netcoreapp1.0";
var testProject = new TestProject()
{
Name = "FrameworkTargetTelemetryTest",
TargetFrameworks = targetFramework,
IsSdkProject = true,
};
Type loggerType = typeof(LogTelemetryToStdOutForTest);
var TelemetryTestLogger = new[]
{
$"/Logger:{loggerType.FullName},{loggerType.GetTypeInfo().Assembly.Location}"
};
var testAsset = _testAssetsManager.CreateTestProject(testProject)
.Restore(Log, testProject.Name, TelemetryTestLogger);

var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name));

buildCommand
.Execute(TelemetryTestLogger)
.StdOut.Should()
.Contain("{\"EventName\":\"TargetFramework\",\"Properties\":{\"version\":\".NETCoreApp,Version=v1.0\"}");
}

[CoreMSBuildOnlyFact]
public void It_collects_multi_TargetFramework_version()
{
string targetFramework = "net46;netcoreapp1.1";

var testProject = new TestProject()
{
Name = "MultitargetTelemetry",
TargetFrameworks = targetFramework,
IsSdkProject = true,
};
Type loggerType = typeof(LogTelemetryToStdOutForTest);
var TelemetryTestLogger = new[]
{
$"/Logger:{loggerType.FullName},{loggerType.GetTypeInfo().Assembly.Location}"
};
var testAsset = _testAssetsManager.CreateTestProject(testProject)
.Restore(Log, testProject.Name, TelemetryTestLogger);

var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name));

buildCommand
.Execute(TelemetryTestLogger)
.StdOut.Should()
.Contain("{\"EventName\":\"TargetFramework\",\"Properties\":{\"version\":\".NETFramework,Version=v4.6\"}")
.And
.Contain("{\"EventName\":\"TargetFramework\",\"Properties\":{\"version\":\".NETCoreApp,Version=v1.1\"}");
}
}
}
31 changes: 31 additions & 0 deletions test/Microsoft.NET.Build.Tests/LogTelemetryToStdOutForTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using NuGet.Protocol;

namespace Microsoft.NET.Build.Tests
{
public sealed class LogTelemetryToStdOutForTest : Logger
{

public LogTelemetryToStdOutForTest()
{
}

public override void Initialize(IEventSource eventSource)
{
if (eventSource is IEventSource2 eventSource2)
{
eventSource2.TelemetryLogged += OnTelemetryLogged;
}
}

private void OnTelemetryLogged(object sender, TelemetryEventArgs args)
{
Console.WriteLine(args.ToJson());
}
}
}

0 comments on commit f078aca

Please sign in to comment.