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;
|
|
|
|
|
using Microsoft.DotNet.Configurer;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.MSBuild
|
|
|
|
|
{
|
|
|
|
|
public sealed class MSBuildLogger : Logger
|
|
|
|
|
{
|
2017-04-03 22:15:40 -07:00
|
|
|
|
private readonly INuGetCacheSentinel _sentinel = new NuGetCacheSentinel(new CliFallbackFolderPathCalculator());
|
2016-10-31 16:16:39 -07:00
|
|
|
|
private readonly ITelemetry _telemetry;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2016-11-01 09:29:19 -07:00
|
|
|
|
IEventSource2 eventSource2 = eventSource as IEventSource2;
|
|
|
|
|
|
|
|
|
|
if (eventSource2 != null)
|
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
|
|
|
|
}
|
|
|
|
|
}
|
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);
|
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
|
|
|
|
}
|