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

76 lines
2.2 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.Configurer;
namespace Microsoft.DotNet.Tools.MSBuild
{
public sealed class MSBuildLogger : Logger
{
private readonly IFirstTimeUseNoticeSentinel _sentinel =
new FirstTimeUseNoticeSentinel(new CliFallbackFolderPathCalculator());
private readonly ITelemetry _telemetry;
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 override 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)
{
2016-11-01 09:29:19 -07:00
IEventSource2 eventSource2 = eventSource as IEventSource2;
if (eventSource2 != null)
{
2016-11-01 09:29:19 -07:00
eventSource2.TelemetryLogged += OnTelemetryLogged;
}
}
}
2016-11-01 09:29:19 -07:00
catch(Exception)
{
// Exceptions during telemetry shouldn't cause anything else to fail
}
}
private void OnTelemetryLogged(object sender, TelemetryEventArgs args)
{
_telemetry.TrackEvent(args.EventName, args.Properties, measurements: null);
}
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
}
base.Shutdown();
}
}
}