2016-10-31 16:16:39 -07:00
|
|
|
|
// 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 Microsoft.DotNet.Cli;
|
2017-06-05 20:51:58 -07:00
|
|
|
|
using Microsoft.DotNet.Cli.Telemetry;
|
2016-10-31 16:16:39 -07:00
|
|
|
|
using Microsoft.DotNet.Configurer;
|
2017-10-27 10:58:25 -07:00
|
|
|
|
using System.Collections.Generic;
|
2016-10-31 16:16:39 -07:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.MSBuild
|
|
|
|
|
{
|
|
|
|
|
public sealed class MSBuildLogger : Logger
|
|
|
|
|
{
|
2017-06-19 20:52:19 -07:00
|
|
|
|
private readonly IFirstTimeUseNoticeSentinel _sentinel =
|
2017-11-27 10:45:43 -08:00
|
|
|
|
new FirstTimeUseNoticeSentinel(new CliFolderPathCalculator());
|
2016-10-31 16:16:39 -07:00
|
|
|
|
private readonly ITelemetry _telemetry;
|
2017-10-27 10:58:25 -07:00
|
|
|
|
private const string NewEventName = "msbuild";
|
|
|
|
|
private const string TargetFrameworkTelemetryEventName = "targetframeworkeval";
|
|
|
|
|
private const string TargetFrameworkVersionTelemetryPropertyKey= "TargetFrameworkVersion";
|
2016-10-31 16:16:39 -07:00
|
|
|
|
|
|
|
|
|
public MSBuildLogger()
|
|
|
|
|
{
|
2016-11-01 09:29:19 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-03-02 20:35:20 -08:00
|
|
|
|
string sessionId =
|
2016-11-02 13:06:44 -07:00
|
|
|
|
Environment.GetEnvironmentVariable(MSBuildForwardingApp.TelemetrySessionIdEnvironmentVariableName);
|
2016-10-31 16:16:39 -07:00
|
|
|
|
|
2016-11-01 09:29:19 -07:00
|
|
|
|
if (sessionId != null)
|
|
|
|
|
{
|
|
|
|
|
_telemetry = new Telemetry(_sentinel, sessionId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
2016-10-31 16:16:39 -07:00
|
|
|
|
{
|
2016-11-01 09:29:19 -07:00
|
|
|
|
// Exceptions during telemetry shouldn't cause anything else to fail
|
2016-10-31 16:16:39 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Initialize(IEventSource eventSource)
|
|
|
|
|
{
|
2016-11-01 09:29:19 -07:00
|
|
|
|
try
|
2016-10-31 16:16:39 -07:00
|
|
|
|
{
|
2016-11-01 09:29:19 -07:00
|
|
|
|
if (_telemetry != null && _telemetry.Enabled)
|
2016-10-31 16:16:39 -07:00
|
|
|
|
{
|
2017-06-05 20:51:58 -07:00
|
|
|
|
if (eventSource is IEventSource2 eventSource2)
|
2016-10-31 16:16:39 -07:00
|
|
|
|
{
|
2016-11-01 09:29:19 -07:00
|
|
|
|
eventSource2.TelemetryLogged += OnTelemetryLogged;
|
|
|
|
|
}
|
2016-10-31 16:16:39 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-05 20:51:58 -07:00
|
|
|
|
catch (Exception)
|
2016-11-01 09:29:19 -07:00
|
|
|
|
{
|
|
|
|
|
// Exceptions during telemetry shouldn't cause anything else to fail
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-27 10:58:25 -07:00
|
|
|
|
internal static void FormatAndSend(ITelemetry telemetry, TelemetryEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (args.EventName == TargetFrameworkTelemetryEventName)
|
|
|
|
|
{
|
|
|
|
|
var newEventName = $"msbuild/{TargetFrameworkTelemetryEventName}";
|
|
|
|
|
Dictionary<string, string> maskedProperties = new Dictionary<string, string>();
|
|
|
|
|
if (args.Properties.TryGetValue(TargetFrameworkVersionTelemetryPropertyKey, out string value))
|
|
|
|
|
{
|
|
|
|
|
maskedProperties.Add(TargetFrameworkVersionTelemetryPropertyKey, Sha256Hasher.HashWithNormalizedCasing(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
telemetry.TrackEvent(newEventName, maskedProperties, measurements: null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 09:29:19 -07:00
|
|
|
|
private void OnTelemetryLogged(object sender, TelemetryEventArgs args)
|
|
|
|
|
{
|
2017-10-27 10:58:25 -07:00
|
|
|
|
FormatAndSend(_telemetry, args);
|
2016-10-31 16:16:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Shutdown()
|
|
|
|
|
{
|
2016-11-01 09:29:19 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_sentinel?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
// Exceptions during telemetry shouldn't cause anything else to fail
|
|
|
|
|
}
|
2016-10-31 16:16:39 -07:00
|
|
|
|
|
|
|
|
|
base.Shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-02 20:35:20 -08:00
|
|
|
|
}
|