dotnet-installer/src/dotnet/commands/dotnet-msbuild/MSBuildLogger.cs

101 lines
3.4 KiB
C#
Raw Normal View History

// 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;
using Microsoft.DotNet.Cli.Telemetry;
using Microsoft.DotNet.Configurer;
using System.Collections.Generic;
namespace Microsoft.DotNet.Tools.MSBuild
{
public sealed class MSBuildLogger : INodeLogger
{
private readonly IFirstTimeUseNoticeSentinel _sentinel =
new FirstTimeUseNoticeSentinel(new CliFolderPathCalculator());
private readonly ITelemetry _telemetry;
private const string NewEventName = "msbuild";
private const string TargetFrameworkTelemetryEventName = "targetframeworkeval";
private const string TargetFrameworkVersionTelemetryPropertyKey= "TargetFrameworkVersion";
public MSBuildLogger()
{
2016-11-01 09:29:19 -07:00
try
{
string sessionId =
2016-11-02 13:06:44 -07:00
Environment.GetEnvironmentVariable(MSBuildForwardingApp.TelemetrySessionIdEnvironmentVariableName);
2016-11-01 09:29:19 -07:00
if (sessionId != null)
{
_telemetry = new Telemetry(_sentinel, sessionId);
}
}
catch (Exception)
{
2016-11-01 09:29:19 -07:00
// Exceptions during telemetry shouldn't cause anything else to fail
}
}
public void Initialize(IEventSource eventSource, int nodeCount)
{
Initialize(eventSource);
}
public void Initialize(IEventSource eventSource)
{
2016-11-01 09:29:19 -07:00
try
{
2016-11-01 09:29:19 -07:00
if (_telemetry != null && _telemetry.Enabled)
{
if (eventSource is IEventSource2 eventSource2)
{
2016-11-01 09:29:19 -07:00
eventSource2.TelemetryLogged += OnTelemetryLogged;
}
}
}
catch (Exception)
2016-11-01 09:29:19 -07:00
{
// Exceptions during telemetry shouldn't cause anything else to fail
}
}
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)
{
FormatAndSend(_telemetry, args);
}
public 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
}
}
public LoggerVerbosity Verbosity { get; set; }
public string Parameters { get; set; }
}
}