Custom logger for MSBuild to receive telemetry events (#4551)
* Upgrade MSBuild references to 15.1.0-preview-000370-00 * Custom logger for MSBuild to receive telemetry events Had to make the telemetry session ID a static variable so that the forwarding app could use it. I've tested this all manually and will be writing tests after everyone signs off on the implementation here.
This commit is contained in:
parent
d804502a8a
commit
595a768e30
19 changed files with 98 additions and 21 deletions
52
src/dotnet/commands/dotnet-msbuild/MSBuildLogger.cs
Normal file
52
src/dotnet/commands/dotnet-msbuild/MSBuildLogger.cs
Normal file
|
@ -0,0 +1,52 @@
|
|||
// 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.Utils;
|
||||
using Microsoft.DotNet.Configurer;
|
||||
|
||||
namespace Microsoft.DotNet.Tools.MSBuild
|
||||
{
|
||||
public sealed class MSBuildLogger : Logger
|
||||
{
|
||||
private readonly INuGetCacheSentinel _sentinel = new NuGetCacheSentinel();
|
||||
private readonly ITelemetry _telemetry;
|
||||
|
||||
public MSBuildLogger()
|
||||
{
|
||||
string sessionId = Environment.GetEnvironmentVariable(MSBuildForwardingApp.TelemetrySessionIdEnvironmentVariableName);
|
||||
|
||||
if (sessionId != null)
|
||||
{
|
||||
_telemetry = new Telemetry(_sentinel, sessionId);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Initialize(IEventSource eventSource)
|
||||
{
|
||||
if (_telemetry != null)
|
||||
{
|
||||
IEventSource2 eventSource2 = eventSource as IEventSource2;
|
||||
|
||||
if (eventSource2 != null)
|
||||
{
|
||||
eventSource2.TelemetryLogged += (sender, telemetryEventArgs) =>
|
||||
{
|
||||
Console.WriteLine($"Received telemetry event '{telemetryEventArgs.EventName}'");
|
||||
_telemetry.TrackEvent(telemetryEventArgs.EventName, telemetryEventArgs.Properties, measurements: null);
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
_sentinel?.Dispose();
|
||||
|
||||
base.Shutdown();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue