Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup frameworks in AspNetCore #2278

Merged
merged 2 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ public static IServiceCollection AddApplicationInsightsTelemetry(this IServiceCo

// NetStandard2.0 has a package reference to Microsoft.Extensions.Logging.ApplicationInsights, and
// enables ApplicationInsightsLoggerProvider by default.
#if NETSTANDARD2_0 || NET461
AddApplicationInsightsLoggerProvider(services);
#endif
}

return services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ public void Configure(ApplicationInsightsServiceOptions options)
{
var configBuilder = new ConfigurationBuilder()
.SetBasePath(this.hostingEnvironment.ContentRootPath ?? Directory.GetCurrentDirectory());
#if NETSTANDARD2_0 || NET461
if (this.userConfiguration != null)
{
configBuilder.AddConfiguration(this.userConfiguration);
}
#endif

configBuilder.AddJsonFile("appsettings.json", true)
.AddJsonFile(string.Format(CultureInfo.InvariantCulture, "appsettings.{0}.json", this.hostingEnvironment.EnvironmentName), true)
.AddEnvironmentVariables();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ public RequestCollectionOptions()

// In NetStandard20, ApplicationInsightsLoggerProvider is enabled by default,
// which captures Exceptions. Disabling it in RequestCollectionModule to avoid duplication.
#if NETSTANDARD2_0 || NET461
this.TrackExceptions = false;
#else
this.TrackExceptions = true;
#endif
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
[SuppressMessage("Documentation Rules", "SA1614:ElementParameterDocumentationMustHaveText", Justification = "This class is obsolete and will not be completely documented.")]
internal class ApplicationInsightsLogger : ILogger
{
#if NET461
#if NETFRAMEWORK
/// <summary>
/// SDK Version Prefix.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
/// <summary>
/// <see cref="ILoggerProvider"/> implementation that creates returns instances of <see cref="ApplicationInsightsLogger"/>.
/// </summary>
#if !NETSTANDARD2_0 && !NET461
// For NETSTANDARD2.0 and NET461 We take dependency on Microsoft.Extensions.Logging.ApplicationInsights which has ApplicationInsightsProvider having the same ProviderAlias and don't want to clash with this ProviderAlias.
[ProviderAlias("ApplicationInsights")]
#endif
/// <remarks>
/// THIS CLASS IS OBSOLETE.
/// For NETSTANDARD2.0 and NET461 We take dependency on Microsoft.Extensions.Logging.ApplicationInsights which has ApplicationInsightsProvider having the same ProviderAlias and don't want to clash with this ProviderAlias.
/// </remarks>
[SuppressMessage("Documentation Rules", "SA1614:ElementParameterDocumentationMustHaveText", Justification = "This class is obsolete and will not be completely documented.")]
[Obsolete]
internal class ApplicationInsightsLoggerProvider : ILoggerProvider
{
private readonly TelemetryClient telemetryClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'net461' ">
<ItemGroup>
<!--
Microsoft.AspNetCore.Http has a vulnerability https://msrc.microsoft.com/update-guide/vulnerability/CVE-2020-1045
System.Text.Encodings.Web has a vulnerability https://msrc.microsoft.com/update-guide/vulnerability/CVE-2021-26701
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/// </summary>
internal class SdkVersionUtils
{
#if NET461
#if NETFRAMEWORK
/// <summary>
/// SDK Version Prefix.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ public static partial class ApplicationInsightsExtensions
private const string DeveloperModeForWebSites = "APPINSIGHTS_DEVELOPER_MODE";
private const string EndpointAddressForWebSites = "APPINSIGHTS_ENDPOINTADDRESS";

#if NETSTANDARD2_0 || NET461
private const string ApplicationInsightsSectionFromConfig = "ApplicationInsights";
private const string TelemetryChannelSectionFromConfig = "ApplicationInsights:TelemetryChannel";
#endif

[SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Justification = "Used in NetStandard2.0 build.")]
private const string EventSourceNameForSystemRuntime = "System.Runtime";
Expand Down Expand Up @@ -242,10 +240,8 @@ internal static void AddTelemetryConfiguration(
{
try
{
#if NETSTANDARD2_0 || NET461
config.GetSection(ApplicationInsightsSectionFromConfig).Bind(serviceOptions);
config.GetSection(TelemetryChannelSectionFromConfig).Bind(serviceOptions);
#endif

if (config.TryGetValue(primaryKey: ConnectionStringEnvironmentVariable, backupKey: ConnectionStringFromConfig, value: out string connectionStringValue))
{
Expand Down Expand Up @@ -405,7 +401,6 @@ private static void AddAndConfigureDependencyTracking(IServiceCollection service
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", Justification = "services parameter is used in only NetStandard 2.0 build.")]
private static void AddApplicationInsightsLoggerProvider(IServiceCollection services)
{
#if NETSTANDARD2_0 || NET461
services.AddLogging(loggingBuilder =>
{
loggingBuilder.AddApplicationInsights();
Expand Down Expand Up @@ -435,7 +430,6 @@ private static void AddApplicationInsightsLoggerProvider(IServiceCollection serv
LogLevel.Warning,
null)));
});
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private string GetMachineName()
string hostName = Dns.GetHostName();

// Issue #61: For dnxcore machine name does not have domain name like in full framework
#if NET461
#if NETFRAMEWORK
string domainName = IPGlobalProperties.GetIPGlobalProperties().DomainName;
if (!hostName.EndsWith(domainName, StringComparison.OrdinalIgnoreCase))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@ public static void RegistersTelemetryConfigurationFactoryMethodThatCreatesDefaul
/// Else, it invokes services.AddApplicationInsightsTelemetry(configuration) where IConfiguration object is supplied by caller.
/// </param>
[Theory]
#if !NET46
[InlineData(true)]
#endif
[InlineData(false)]
public static void RegistersTelemetryConfigurationFactoryMethodThatReadsInstrumentationKeyFromConfiguration(bool useDefaultConfig)
{
Expand Down Expand Up @@ -214,9 +211,6 @@ public static void RegistersTelemetryConfigurationFactoryMethodThatReadsDevelope
/// Else, it invokes services.AddApplicationInsightsTelemetry(configuration) where IConfiguration object is supplied by caller.
/// </param>
[Theory]
#if !NET46
[InlineData(true)]
#endif
[InlineData(false)]
public static void RegistersTelemetryConfigurationFactoryMethodThatReadsEndpointAddressFromConfiguration(bool useDefaultConfig)
{
Expand Down Expand Up @@ -697,12 +691,10 @@ public static void RegistersTelemetryConfigurationFactoryMethodThatPopulatesDepe
/// </param>
/// <param name="isEnable">Sets the value for property EnableLegacyCorrelationHeadersInjection.</param>
[Theory]
#if !NET46
[InlineData("DefaultConfiguration", true)]
[InlineData("DefaultConfiguration", false)]
[InlineData("SuppliedConfiguration", true)]
[InlineData("SuppliedConfiguration", false)]
#endif
[InlineData("Code", true)]
[InlineData("Code", false)]
public static void RegistersTelemetryConfigurationFactoryMethodThatPopulatesDependencyCollectorWithCustomValues(string configType, bool isEnable)
Expand Down Expand Up @@ -1020,11 +1012,7 @@ public static void ConfigureRequestTrackingTelemetryDefaultOptions()
== typeof(RequestTrackingTelemetryModule));

Assert.True(requestTrackingModule.CollectionOptions.InjectResponseHeaders);
#if NETCOREAPP || NET461
Assert.False(requestTrackingModule.CollectionOptions.TrackExceptions);
#else
Assert.True(requestTrackingModule.CollectionOptions.TrackExceptions);
#endif
}

/// <summary>
Expand All @@ -1039,12 +1027,10 @@ public static void ConfigureRequestTrackingTelemetryDefaultOptions()
/// </param>
/// <param name="isEnable">Sets the value for property InjectResponseHeaders, TrackExceptions and EnableW3CDistributedTracing.</param>
[Theory]
#if !NET46
[InlineData("DefaultConfiguration", true)]
[InlineData("DefaultConfiguration", false)]
[InlineData("SuppliedConfiguration", true)]
[InlineData("SuppliedConfiguration", false)]
#endif
[InlineData("Code", true)]
[InlineData("Code", false)]
public static void ConfigureRequestTrackingTelemetryCustomOptions(string configType, bool isEnable)
Expand Down Expand Up @@ -1141,12 +1127,10 @@ public static void AddsAddaptiveSamplingServiceToTheConfigurationByDefault()
/// </param>
/// <param name="isEnable">Sets the value for property EnableAdaptiveSampling.</param>
[Theory]
#if !NET46
[InlineData("DefaultConfiguration", true)]
[InlineData("DefaultConfiguration", false)]
[InlineData("SuppliedConfiguration", true)]
[InlineData("SuppliedConfiguration", false)]
#endif
[InlineData("Code", true)]
[InlineData("Code", false)]
public static void DoesNotAddSamplingToConfigurationIfExplicitlyControlledThroughParameter(string configType, bool isEnable)
Expand Down Expand Up @@ -1357,12 +1341,10 @@ public static void AddsAutoCollectedMetricsExtractorProcessorToTheConfigurationB
/// </param>
/// <param name="isEnable">Sets the value for property AddAutoCollectedMetricExtractor.</param>
[Theory]
#if !NET46
[InlineData("DefaultConfiguration", true)]
[InlineData("DefaultConfiguration", false)]
[InlineData("SuppliedConfiguration", true)]
[InlineData("SuppliedConfiguration", false)]
#endif
[InlineData("Code", true)]
[InlineData("Code", false)]
public static void DoesNotAddAutoCollectedMetricsExtractorToConfigurationIfExplicitlyControlledThroughParameter(string configType, bool isEnable)
Expand Down Expand Up @@ -1410,12 +1392,10 @@ public static void DoesNotAddQuickPulseProcessorToConfigurationIfExplicitlyContr
/// </param>
/// <param name="isEnable">Sets the value for property EnableAuthenticationTrackingJavaScript.</param>
[Theory]
#if !NET46
[InlineData("DefaultConfiguration", true)]
[InlineData("DefaultConfiguration", false)]
[InlineData("SuppliedConfiguration", true)]
[InlineData("SuppliedConfiguration", false)]
#endif
[InlineData("Code", true)]
[InlineData("Code", false)]
public static void UserCanEnableAndDisableAuthenticationTrackingJavaScript(string configType, bool isEnable)
Expand Down Expand Up @@ -1530,8 +1510,6 @@ public static void NullLoggerCallbackAlowed()
loggerProvider.AddApplicationInsights(serviceProvider, (s, level) => true, null);
}

#if NETCOREAPP || NET461

/// <summary>
/// Creates two copies of ApplicationInsightsServiceOptions. First object is created by calling services.AddApplicationInsightsTelemetry() or services.AddApplicationInsightsTelemetry(config).
/// Second object is created directly from configuration file without using any of SDK functionality.
Expand Down Expand Up @@ -1723,6 +1701,5 @@ public static void ReadsFromAppSettingsIfNoSettingsFoundInDefaultConfiguration()

Assert.Equal(appSettingsConfig["ApplicationInsights:InstrumentationKey"], telemetryConfiguration.InstrumentationKey);
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ public static ServiceCollection CreateServicesAndAddApplicationinsightsTelemetry
config = new ConfigurationBuilder().Build();
}

#if NET46
// In NET46, we don't read from default configuration or bind configuration.
services.AddApplicationInsightsTelemetry(config);
#else

if (useDefaultConfig)
{
services.AddSingleton<IConfiguration>(config);
Expand All @@ -72,7 +69,7 @@ public static ServiceCollection CreateServicesAndAddApplicationinsightsTelemetry
{
services.AddApplicationInsightsTelemetry(config);
}
#endif

if (serviceOptions != null)
{
services.Configure(serviceOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ private static ServiceCollection CreateServicesAndAddApplicationinsightsWorker(s
config = configBuilder.Build();
}

#if NET46
// In NET46, we don't read from default configuration or bind configuration.
services.AddApplicationInsightsTelemetry(config);
#else
if (useDefaultConfig)
{
services.AddSingleton<IConfiguration>(config);
Expand All @@ -113,7 +109,6 @@ private static ServiceCollection CreateServicesAndAddApplicationinsightsWorker(s
{
services.AddApplicationInsightsTelemetry(config);
}
#endif

servicesConfig?.Invoke(services);

Expand All @@ -135,12 +130,11 @@ private static ServiceCollection CreateServicesAndAddApplicationinsightsWorker(s
/// Enabling this will copy the AspNetCore config to the TC.Active static instance.
/// </summary>
[Theory]
#if !NET46

[InlineData("DefaultConfiguration", true)]
[InlineData("DefaultConfiguration", false)]
[InlineData("SuppliedConfiguration", true)]
[InlineData("SuppliedConfiguration", false)]
#endif
[InlineData("Code", true)]
[InlineData("Code", false)]
public static void UserCanEnableAndDisableTelemetryConfigurationActive(string configType, bool isEnable)
Expand Down Expand Up @@ -190,12 +184,10 @@ public static void UserCanEnableAndDisableTelemetryConfigurationActive(string co
/// User could enable or disable PerformanceCounterCollectionModule by setting EnablePerformanceCounterCollectionModule.
/// </summary>
[Theory]
#if !NET46
[InlineData("DefaultConfiguration", true)]
[InlineData("DefaultConfiguration", false)]
[InlineData("SuppliedConfiguration", true)]
[InlineData("SuppliedConfiguration", false)]
#endif
[InlineData("Code", true)]
[InlineData("Code", false)]
public static void UserCanEnableAndDisablePerfCollectorModule(string configType, bool isEnable)
Expand Down Expand Up @@ -234,12 +226,10 @@ public static void UserCanEnableAndDisableEventCounterCollectorModule(string con
/// User could enable or disable DependencyTrackingTelemetryModule by setting EnableDependencyTrackingTelemetryModule.
/// </summary>
[Theory]
#if !NET46
[InlineData("DefaultConfiguration", true)]
[InlineData("DefaultConfiguration", false)]
[InlineData("SuppliedConfiguration", true)]
[InlineData("SuppliedConfiguration", false)]
#endif
[InlineData("Code", true)]
[InlineData("Code", false)]
public static void UserCanEnableAndDisableDependencyCollectorModule(string configType, bool isEnable)
Expand All @@ -255,12 +245,10 @@ public static void UserCanEnableAndDisableDependencyCollectorModule(string confi
/// User could enable or disable QuickPulseCollectorModule by setting EnableQuickPulseMetricStream.
/// </summary>
[Theory]
#if !NET46
[InlineData("DefaultConfiguration", true)]
[InlineData("DefaultConfiguration", false)]
[InlineData("SuppliedConfiguration", true)]
[InlineData("SuppliedConfiguration", false)]
#endif
[InlineData("Code", true)]
[InlineData("Code", false)]
public static void UserCanEnableAndDisableQuickPulseCollectorModule(string configType, bool isEnable)
Expand All @@ -276,12 +264,10 @@ public static void UserCanEnableAndDisableQuickPulseCollectorModule(string confi
/// User could enable or disable AzureInstanceMetadataModule by setting EnableAzureInstanceMetadataTelemetryModule.
/// </summary>
[Theory]
#if !NET46
[InlineData("DefaultConfiguration", true)]
[InlineData("DefaultConfiguration", false)]
[InlineData("SuppliedConfiguration", true)]
[InlineData("SuppliedConfiguration", false)]
#endif
[InlineData("Code", true)]
[InlineData("Code", false)]
public static void UserCanEnableAndDisableAzureInstanceMetadataModule(string configType, bool isEnable)
Expand All @@ -297,12 +283,10 @@ public static void UserCanEnableAndDisableAzureInstanceMetadataModule(string con
/// User could enable or disable RequestTrackingTelemetryModule by setting EnableRequestTrackingTelemetryModule.
/// </summary>
[Theory]
#if !NET46
[InlineData("DefaultConfiguration", true)]
[InlineData("DefaultConfiguration", false)]
[InlineData("SuppliedConfiguration", true)]
[InlineData("SuppliedConfiguration", false)]
#endif
[InlineData("Code", true)]
[InlineData("Code", false)]
public static void UserCanEnableAndDisableRequestCounterCollectorModule(string configType, bool isEnable)
Expand All @@ -318,12 +302,10 @@ public static void UserCanEnableAndDisableRequestCounterCollectorModule(string c
/// User could enable or disable AppServiceHeartbeatModule by setting EnableAppServicesHeartbeatTelemetryModule.
/// </summary>
[Theory]
#if !NET46
[InlineData("DefaultConfiguration", true)]
[InlineData("DefaultConfiguration", false)]
[InlineData("SuppliedConfiguration", true)]
[InlineData("SuppliedConfiguration", false)]
#endif
[InlineData("Code", true)]
[InlineData("Code", false)]
public static void UserCanEnableAndDisableAppServiceHeartbeatModule(string configType, bool isEnable)
Expand All @@ -339,12 +321,10 @@ public static void UserCanEnableAndDisableAppServiceHeartbeatModule(string confi
/// User could enable or disable <see cref="DiagnosticsTelemetryModule"/> by setting <see cref="ApplicationInsightsServiceOptions.EnableDiagnosticsTelemetryModule"/>.
/// </summary>
[Theory]
#if !NET46
[InlineData("DefaultConfiguration", true)]
[InlineData("DefaultConfiguration", false)]
[InlineData("SuppliedConfiguration", true)]
[InlineData("SuppliedConfiguration", false)]
#endif
[InlineData("Code", true)]
[InlineData("Code", false)]
public static void UserCanEnableAndDisableDiagnosticsTelemetryModule(string configType, bool isEnable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>

<ItemGroup>
<None Update="content\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.ApplicationInsights.AspNetCore\Microsoft.ApplicationInsights.AspNetCore.csproj" />
</ItemGroup>
Expand Down
Loading