Clean up some MS.AspNetCore.Razor.Common code

This commit is contained in:
Dustin Campbell 2022-11-10 12:31:56 -08:00
Родитель 4a89c23c78
Коммит a5fd66558f
6 изменённых файлов: 64 добавлений и 70 удалений

Просмотреть файл

@ -86,6 +86,7 @@
<PropertyGroup Label="Manual">
<!-- dotnet/runtime packages -->
<MicrosoftExtensionsPackageVersion>6.0.0</MicrosoftExtensionsPackageVersion>
<SystemComponentModelPackageVersion>6.0.0</SystemComponentModelPackageVersion>
<SystemCollectionsImmutablePackageVersion>6.0.0</SystemCollectionsImmutablePackageVersion>
<SystemDiagnosticsDiagnosticSourcePackageVersion>6.0.0</SystemDiagnosticsDiagnosticSourcePackageVersion>
<SystemResourcesExtensionsPackageVersion>6.0.0</SystemResourcesExtensionsPackageVersion>

Просмотреть файл

@ -1,9 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Text;
namespace Microsoft.AspNetCore.Razor.Common;
@ -13,8 +11,9 @@ internal static class KeyValuePairExtensions
/// Deconstructs a <see cref="KeyValuePair{TKey, TValue}"/> into out variables. Provides support
/// for assignment like
/// <code>
/// var (k,v) = kvp;
/// var (k,v) = kvp;
/// </code>
/// </summary>
public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> kvp, out TKey key, out TValue value) => (key, value) = (kvp.Key, kvp.Value);
public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> kvp, out TKey key, out TValue value)
=> (key, value) = (kvp.Key, kvp.Value);
}

Просмотреть файл

@ -21,7 +21,7 @@
<PackageReference Include="Microsoft.VisualStudio.Telemetry" Version="$(MicrosoftVisualStudioTelemetryVersion)" />
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="$(MicrosoftVisualStudioThreadingPackageVersion)" />
<PackageReference Include="System.Collections.Immutable" Version="$(SystemCollectionsImmutablePackageVersion)" />
<PackageReference Include="System.Composition.AttributedModel" Version="6.0.0" />
<PackageReference Include="System.Composition.AttributedModel" Version="$(SystemComponentModelPackageVersion)" />
</ItemGroup>
<ItemGroup>

Просмотреть файл

@ -4,11 +4,10 @@
using System.Collections.Immutable;
using Microsoft.VisualStudio.Telemetry;
namespace Microsoft.AspNetCore.Razor.Common.Telemetry
namespace Microsoft.AspNetCore.Razor.Common.Telemetry;
internal interface ITelemetryReporter
{
internal interface ITelemetryReporter
{
void ReportEvent(string name, TelemetrySeverity severity);
void ReportEvent<T>(string name, TelemetrySeverity severity, ImmutableDictionary<string, T> values);
}
void ReportEvent(string name, TelemetrySeverity severity);
void ReportEvent<T>(string name, TelemetrySeverity severity, ImmutableDictionary<string, T> values);
}

Просмотреть файл

@ -3,78 +3,74 @@
using System;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Razor.Common;
using Microsoft.AspNetCore.Razor.Common.Telemetry;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.Telemetry;
using System.Composition;
namespace Microsoft.AspNetCore.Razor.LanguageServer.Telemetry
namespace Microsoft.AspNetCore.Razor.Common.Telemetry;
[Shared]
[Export(typeof(ITelemetryReporter))]
internal class TelemetryReporter : ITelemetryReporter
{
[Shared]
[Export(typeof(ITelemetryReporter))]
internal class TelemetryReporter : ITelemetryReporter
private readonly ImmutableArray<TelemetrySession> _telemetrySessions;
private readonly ILogger? _logger;
[ImportingConstructor]
public TelemetryReporter([Import(AllowDefault = true)] ILoggerFactory? loggerFactory = null)
: this(ImmutableArray.Create(TelemetryService.DefaultSession), loggerFactory)
{
private readonly ImmutableArray<TelemetrySession> _telemetrySessions;
private readonly ILogger? _logger;
}
[ImportingConstructor]
public TelemetryReporter([Import(AllowDefault = true)]ILoggerFactory? loggerFactory = null)
: this(ImmutableArray.Create(TelemetryService.DefaultSession), loggerFactory)
public TelemetryReporter(ImmutableArray<TelemetrySession> telemetrySessions, ILoggerFactory? loggerFactory)
{
_telemetrySessions = telemetrySessions;
_logger = loggerFactory?.CreateLogger<TelemetryReporter>();
}
public void ReportEvent(string name, TelemetrySeverity severity)
{
var telemetryEvent = new TelemetryEvent(GetTelemetryName(name), severity);
Report(telemetryEvent);
}
public void ReportEvent<T>(string name, TelemetrySeverity severity, ImmutableDictionary<string, T> values)
{
var telemetryEvent = new TelemetryEvent(GetTelemetryName(name), severity);
foreach (var (propertyName, propertyValue) in values)
{
telemetryEvent.Properties.Add(GetPropertyName(propertyName), new TelemetryComplexProperty(propertyValue));
}
public TelemetryReporter(ImmutableArray<TelemetrySession> telemetrySessions, ILoggerFactory? loggerFactory)
Report(telemetryEvent);
}
private static string GetTelemetryName(string name) => "razor/" + name;
private static string GetPropertyName(string name) => "razor." + name;
private void Report(TelemetryEvent telemetryEvent)
{
try
{
_telemetrySessions = telemetrySessions;
_logger = loggerFactory?.CreateLogger<TelemetryReporter>();
}
public void ReportEvent(string name, TelemetrySeverity severity)
{
var telemetryEvent = new TelemetryEvent(GetTelemetryName(name), severity);
Report(telemetryEvent);
}
public void ReportEvent<T>(string name, TelemetrySeverity severity, ImmutableDictionary<string, T> values)
{
var telemetryEvent = new TelemetryEvent(GetTelemetryName(name), severity);
foreach (var (propertyName, propertyValue) in values)
{
telemetryEvent.Properties.Add(GetPropertyName(propertyName), new TelemetryComplexProperty(propertyValue));
}
Report(telemetryEvent);
}
private static string GetTelemetryName(string name) => "razor/" + name;
private static string GetPropertyName(string name) => "razor." + name;
private void Report(TelemetryEvent telemetryEvent)
{
try
{
#if !DEBUG
foreach (var session in _telemetrySessions)
{
session.PostEvent(telemetryEvent);
}
#else
// In debug we only log to normal logging. This makes it much easier to add and debug telemetry events
// before we're ready to send them to the cloud
var name = telemetryEvent.Name;
var propertyString = string.Join(",", telemetryEvent.Properties.Select(kvp => $"[ {kvp.Key}:{kvp.Value} ]"));
_logger?.LogTrace("Telemetry Event: {name} \n Properties: {propertyString}\n", name, propertyString);
#endif
}
catch (Exception e)
foreach (var session in _telemetrySessions)
{
// No need to do anything here. We failed to report telemetry
// which isn't good, but not catastrophic for a user
_logger?.LogError(e, "Failed logging telemetry event");
session.PostEvent(telemetryEvent);
}
#else
// In debug we only log to normal logging. This makes it much easier to add and debug telemetry events
// before we're ready to send them to the cloud
var name = telemetryEvent.Name;
var propertyString = string.Join(",", telemetryEvent.Properties.Select(kvp => $"[ {kvp.Key}:{kvp.Value} ]"));
_logger?.LogTrace("Telemetry Event: {name} \n Properties: {propertyString}\n", name, propertyString);
#endif
}
catch (Exception e)
{
// No need to do anything here. We failed to report telemetry
// which isn't good, but not catastrophic for a user
_logger?.LogError(e, "Failed logging telemetry event");
}
}
}

Просмотреть файл

@ -17,7 +17,6 @@ using Microsoft.AspNetCore.Razor.LanguageServer.Implementation;
using Microsoft.AspNetCore.Razor.LanguageServer.LinkedEditingRange;
using Microsoft.AspNetCore.Razor.LanguageServer.Refactoring;
using Microsoft.AspNetCore.Razor.LanguageServer.SignatureHelp;
using Microsoft.AspNetCore.Razor.LanguageServer.Telemetry;
using Microsoft.AspNetCore.Razor.LanguageServer.WrapWithTag;
using Microsoft.CodeAnalysis.Razor;
using Microsoft.CodeAnalysis.Razor.Workspaces;