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

AspNetCore & FrameworkReference #2412

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
12f8a9b
saving work in progress
TimothyMothra Sep 15, 2021
f1c83e8
saving progress
TimothyMothra Sep 16, 2021
62e4a79
Merge branch 'develop' into tilee/aspnetcore_frameworkreference
TimothyMothra Sep 16, 2021
b8db290
merge develop
TimothyMothra Sep 16, 2021
d5f2355
undo
TimothyMothra Sep 16, 2021
a9e7133
fix merge conflict
TimothyMothra Sep 16, 2021
0089454
cleanup
TimothyMothra Sep 16, 2021
3ad8c25
public api
TimothyMothra Sep 16, 2021
e5127a4
fix public api
TimothyMothra Sep 16, 2021
9cf51c8
Merge branch 'develop' into tilee/aspnetcore_frameworkreference
TimothyMothra Sep 21, 2021
25920ac
update test frameworks
TimothyMothra Sep 21, 2021
a4aecbe
testing supporting both netstandard2.0 and netcoreapp3.1
TimothyMothra Sep 21, 2021
6db383a
Merge branch 'develop' into tilee/aspnetcore_frameworkreference
TimothyMothra Sep 22, 2021
e275537
undo changes to netstandard publicapi
TimothyMothra Sep 22, 2021
09c3b76
revert changes to netstandard publicapi
TimothyMothra Sep 22, 2021
8cbaa55
cleanup
TimothyMothra Sep 22, 2021
88eed3e
reset changes to publicapi
TimothyMothra Sep 22, 2021
ba58779
delete netstandard2.0 public api files
TimothyMothra Sep 22, 2021
4a1f308
update initializer
TimothyMothra Sep 22, 2021
6b902f2
fix for .NET 6 implicit global using
TimothyMothra Sep 22, 2021
b5f7798
cleanup
TimothyMothra Sep 22, 2021
92787ad
merge develop
TimothyMothra Sep 22, 2021
5c8b65e
fix merge conflicts
TimothyMothra Sep 22, 2021
293bf93
revert changes to public api
TimothyMothra Sep 22, 2021
6ffa269
support for multilpe frameworks
TimothyMothra Sep 22, 2021
6a5bb4b
cleanup
TimothyMothra Sep 22, 2021
4bf8412
bug fix
TimothyMothra Sep 23, 2021
a6cc4df
more tests to confirm extra constructors work with Dependency Injection.
TimothyMothra Sep 24, 2021
50149c8
public api and fxcop
TimothyMothra Sep 24, 2021
bb91bc1
fxcop
TimothyMothra Sep 24, 2021
ee76edc
test fix
TimothyMothra Sep 24, 2021
83943de
fix conditions in csproj
TimothyMothra Sep 28, 2021
39ba8af
fix csproj
TimothyMothra Sep 28, 2021
b6f1839
add logging
TimothyMothra Sep 28, 2021
dd18b9f
merge develop
TimothyMothra Sep 28, 2021
c286e43
merge develop, fix conflicts
TimothyMothra Oct 1, 2021
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
@@ -0,0 +1,2 @@
Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers.AspNetCoreEnvironmentTelemetryInitializer.AspNetCoreEnvironmentTelemetryInitializer(Microsoft.Extensions.Hosting.IHostEnvironment hostEnvironment) -> void
Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers.AspNetCoreEnvironmentTelemetryInitializer.AspNetCoreEnvironmentTelemetryInitializer(Microsoft.Extensions.Hosting.IHostEnvironment hostEnvironment, Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment) -> void
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers.AspNetCoreEnvironmentTelemetryInitializer.AspNetCoreEnvironmentTelemetryInitializer(Microsoft.Extensions.Hosting.IHostEnvironment hostEnvironment) -> void
Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers.AspNetCoreEnvironmentTelemetryInitializer.AspNetCoreEnvironmentTelemetryInitializer(Microsoft.Extensions.Hosting.IHostEnvironment hostEnvironment, Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment) -> void
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
namespace Microsoft.AspNetCore.Hosting
{
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;

/// <summary>
Expand All @@ -14,33 +16,66 @@
/// </summary>
internal class DefaultApplicationInsightsServiceConfigureOptions : IConfigureOptions<ApplicationInsightsServiceOptions>
{
private readonly IHostingEnvironment hostingEnvironment;
private readonly IConfiguration userConfiguration;
private readonly string environmentName;
private readonly string contentRootPath;

#if NETCOREAPP3_0_OR_GREATER
/// <summary>
/// Initializes a new instance of the <see cref="DefaultApplicationInsightsServiceConfigureOptions"/> class.
/// </summary>
/// <param name="hostEnvironment"><see cref="IHostEnvironment"/> to use for retreiving ContentRootPath.</param>
/// <param name="configuration"><see cref="IConfiguration"/> from an application.</param>
public DefaultApplicationInsightsServiceConfigureOptions(IHostEnvironment hostEnvironment, IConfiguration configuration = null)
{
this.environmentName = hostEnvironment?.EnvironmentName;
this.contentRootPath = hostEnvironment?.ContentRootPath ?? Directory.GetCurrentDirectory();

this.userConfiguration = configuration;
}

[Obsolete("IHostingEnvironment is obsolete and will be removed in a future version. The recommended alternative is Microsoft.Extensions.Hosting.IHostEnvironment.", false)]
public DefaultApplicationInsightsServiceConfigureOptions(IHostEnvironment hostEnvironment, IHostingEnvironment hostingEnvironment, IConfiguration configuration = null)
{
this.environmentName = hostEnvironment?.EnvironmentName ?? hostingEnvironment?.EnvironmentName;
this.contentRootPath = hostEnvironment?.ContentRootPath ?? hostEnvironment?.ContentRootPath ?? Directory.GetCurrentDirectory();

this.userConfiguration = configuration;
}
#endif

/// <summary>
/// Initializes a new instance of the <see cref="DefaultApplicationInsightsServiceConfigureOptions"/> class.
/// </summary>
/// <param name="hostingEnvironment"><see cref="IHostingEnvironment"/> to use for retreiving ContentRootPath.</param>
/// <param name="configuration"><see cref="IConfiguration"/> from an application.</param>
[Obsolete("IHostingEnvironment is obsolete and will be removed in a future version. The recommended alternative is Microsoft.Extensions.Hosting.IHostEnvironment.", false)]
public DefaultApplicationInsightsServiceConfigureOptions(IHostingEnvironment hostingEnvironment, IConfiguration configuration = null)
{
this.hostingEnvironment = hostingEnvironment;
this.environmentName = hostingEnvironment?.EnvironmentName;
this.contentRootPath = hostingEnvironment?.ContentRootPath ?? Directory.GetCurrentDirectory();

this.userConfiguration = configuration;
}

/// <inheritdoc />
public void Configure(ApplicationInsightsServiceOptions options)
{
var configBuilder = new ConfigurationBuilder()
.SetBasePath(this.hostingEnvironment.ContentRootPath ?? Directory.GetCurrentDirectory());
var configBuilder = new ConfigurationBuilder().SetBasePath(this.contentRootPath);

if (this.userConfiguration != null)
{
configBuilder.AddConfiguration(this.userConfiguration);
}

configBuilder.AddJsonFile("appsettings.json", true)
.AddJsonFile(string.Format(CultureInfo.InvariantCulture, "appsettings.{0}.json", this.hostingEnvironment.EnvironmentName), true)
.AddEnvironmentVariables();
configBuilder.AddJsonFile("appsettings.json", true);

if (this.environmentName != null)
{
configBuilder.AddJsonFile(string.Format(CultureInfo.InvariantCulture, "appsettings.{0}.json", this.environmentName), true);
}

configBuilder.AddEnvironmentVariables();

ApplicationInsightsExtensions.AddTelemetryConfiguration(configBuilder.Build(), options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@
<ProjectReference Include="..\..\..\WEB\Src\WindowsServer\WindowsServer\WindowsServer.csproj" />
<ProjectReference Include="..\..\..\WEB\Src\EventCounterCollector\EventCounterCollector\EventCounterCollector.csproj" />
<ProjectReference Include="..\..\..\LOGGING\src\ILogger\ILogger.csproj" />

</ItemGroup>

<ItemGroup>
<ItemGroup Condition="'$(IsNetCore)' == 'True'">
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<!-- Will change to conditionally use FrameworkReference in follow up PR. -->
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
namespace Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers
{
using System;

using Microsoft.ApplicationInsights.AspNetCore.Extensibility.Implementation.Tracing;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

/// <summary>
/// <see cref="ITelemetryInitializer"/> implementation that stamps ASP.NET Core environment name
Expand All @@ -12,27 +15,79 @@
public class AspNetCoreEnvironmentTelemetryInitializer : ITelemetryInitializer
{
private const string AspNetCoreEnvironmentPropertyName = "AspNetCoreEnvironment";
private readonly IHostingEnvironment environment;
private readonly Func<string> getEnvironmentName;

/// <summary>
/// Initializes a new instance of the <see cref="AspNetCoreEnvironmentTelemetryInitializer"/> class.
/// </summary>
/// <remarks>
/// We don't cache the result of EnvironmentName because the environment can be changed while the app is running.
/// </remarks>
/// <param name="environment">HostingEnvironment to provide EnvironmentName to be added to telemetry properties.</param>
public AspNetCoreEnvironmentTelemetryInitializer(IHostingEnvironment environment)
[Obsolete("IHostingEnvironment is obsolete. The recommended alternative is Microsoft.Extensions.Hosting.IHostEnvironment.", false)]
public AspNetCoreEnvironmentTelemetryInitializer(Microsoft.AspNetCore.Hosting.IHostingEnvironment environment)
{
AspNetCoreEventSource.Instance.LogInformational($"{nameof(AspNetCoreEnvironmentTelemetryInitializer)} initialized via {nameof(Microsoft.AspNetCore.Hosting.IHostingEnvironment)}");

this.getEnvironmentName = () => environment?.EnvironmentName;
}

#if NETCOREAPP
/// <summary>
/// Initializes a new instance of the <see cref="AspNetCoreEnvironmentTelemetryInitializer"/> class.
/// </summary>
/// <remarks>
/// We don't cache the result of EnvironmentName because the environment can be changed while the app is running.
/// </remarks>
/// <param name="hostEnvironment">HostingEnvironment to provide EnvironmentName to be added to telemetry properties.</param>
public AspNetCoreEnvironmentTelemetryInitializer(IHostEnvironment hostEnvironment)
{
AspNetCoreEventSource.Instance.LogInformational($"{nameof(AspNetCoreEnvironmentTelemetryInitializer)} initialized via {nameof(IHostEnvironment)}");

this.getEnvironmentName = () => hostEnvironment?.EnvironmentName;
}

/// <summary>
/// Initializes a new instance of the <see cref="AspNetCoreEnvironmentTelemetryInitializer"/> class.
/// This constructor is provided for backwards compatibility where both <see cref="IHostEnvironment"/> and <see cref="IHostingEnvironment"/> have been added to Dependency Injection.
/// </summary>
/// <remarks>
/// We don't cache the result of EnvironmentName because the environment can be changed while the app is running.
/// </remarks>
/// <param name="hostEnvironment">IHostEnvironment to provide EnvironmentName to be added to telemetry properties.</param>
/// <param name="hostingEnvironment">IHostingEnvironment to provide EnvironmentName to be added to telemetry properties.</param>
[Obsolete("IHostingEnvironment is obsolete. The recommended alternative is Microsoft.Extensions.Hosting.IHostEnvironment.", false)]
public AspNetCoreEnvironmentTelemetryInitializer(IHostEnvironment hostEnvironment, Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment)
{
this.environment = environment;
AspNetCoreEventSource.Instance.LogInformational($"{nameof(AspNetCoreEnvironmentTelemetryInitializer)} initialized via both {nameof(Microsoft.AspNetCore.Hosting.IHostingEnvironment)} and {nameof(IHostEnvironment)}");

if (hostEnvironment != null)
{
this.getEnvironmentName = () => hostEnvironment.EnvironmentName;
}
else if (hostingEnvironment != null)
{
this.getEnvironmentName = () => hostingEnvironment.EnvironmentName;
}
else
{
this.getEnvironmentName = null;
}
}
#endif

/// <inheritdoc />
public void Initialize(ITelemetry telemetry)
{
if (this.environment != null)
if (this.getEnvironmentName != null)
{
if (telemetry is ISupportProperties telProperties && !telProperties.Properties.ContainsKey(AspNetCoreEnvironmentPropertyName))
var environmentName = this.getEnvironmentName();
if (environmentName != null)
{
telProperties.Properties.Add(
AspNetCoreEnvironmentPropertyName,
this.environment.EnvironmentName);
if (telemetry is ISupportProperties telProperties && !telProperties.Properties.ContainsKey(AspNetCoreEnvironmentPropertyName))
{
telProperties.Properties.Add(AspNetCoreEnvironmentPropertyName, environmentName);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;

#if NETCOREAPP
using IHostEnvironment = Microsoft.Extensions.Hosting.IHostEnvironment;
#endif

/// <summary>
/// The type <see cref="IHostingEnvironment"/> was marked Obsolete starting in NetCore3.
/// Here I'm abstracting it's use into a helper method to simplify some of the tests.
Expand All @@ -27,5 +31,15 @@ public static IHostingEnvironment GetIHostingEnvironment()
return mockEnvironment.Object;
}
#pragma warning restore CS0618 // Type or member is obsolete

#if NETCOREAPP
public static IHostEnvironment GetIHostEnvironment()
{
var mockEnvironment = new Mock<IHostEnvironment>();
mockEnvironment.Setup(x => x.EnvironmentName).Returns("UnitTest");
mockEnvironment.Setup(x => x.ContentRootPath).Returns(Directory.GetCurrentDirectory());
return mockEnvironment.Object;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace Microsoft.Extensions.DependencyInjection.Test
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;

public class AddApplicationInsightsTelemetryTests : BaseTestClass
Expand Down Expand Up @@ -772,7 +773,12 @@ public static void AddApplicationInsightsTelemetryProcessorWithImportingConstruc
// TP added via AddApplicationInsightsTelemetryProcessor is added to the default sink.
FakeTelemetryProcessorWithImportingConstructor telemetryProcessor = telemetryConfiguration.DefaultTelemetrySink.TelemetryProcessors.OfType<FakeTelemetryProcessorWithImportingConstructor>().FirstOrDefault();
Assert.NotNull(telemetryProcessor);
Assert.Same(serviceProvider.GetService<IHostingEnvironment>(), telemetryProcessor.HostingEnvironment);

#if NETCOREAPP
Assert.Same(serviceProvider.GetService<IHostEnvironment>(), telemetryProcessor.HostingEnvironment);
#else
Assert.Same(serviceProvider.GetService<Microsoft.AspNetCore.Hosting.IHostingEnvironment>(), telemetryProcessor.HostingEnvironment);
#endif
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ namespace Microsoft.Extensions.DependencyInjection.Test
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
using Microsoft.ApplicationInsights.AspNetCore.Tests;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.Extensions.Configuration;

using Xunit.Abstractions;

public abstract class BaseTestClass
{
internal const string TestInstrumentationKey = "11111111-2222-3333-4444-555555555555";
Expand Down Expand Up @@ -81,7 +81,11 @@ public static ServiceCollection CreateServicesAndAddApplicationinsightsTelemetry
public static ServiceCollection GetServiceCollectionWithContextAccessor()
{
var services = new ServiceCollection();
services.AddSingleton<IHostingEnvironment>(EnvironmentHelper.GetIHostingEnvironment());
#if NETCOREAPP
services.AddSingleton<Microsoft.Extensions.Hosting.IHostEnvironment>(EnvironmentHelper.GetIHostEnvironment());
#else
services.AddSingleton<AspNetCore.Hosting.IHostingEnvironment>(EnvironmentHelper.GetIHostingEnvironment());
#endif
services.AddSingleton<DiagnosticListener>(new DiagnosticListener("TestListener"));
return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector;
using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse;
using Microsoft.ApplicationInsights.WindowsServer;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Test;
Expand Down Expand Up @@ -76,7 +74,11 @@ private static ServiceCollection CreateServicesAndAddApplicationinsightsWorker(s
{
IConfigurationRoot config;
var services = new ServiceCollection()
.AddSingleton<IHostingEnvironment>(EnvironmentHelper.GetIHostingEnvironment())
#if NETCOREAPP
.AddSingleton<Microsoft.Extensions.Hosting.IHostEnvironment>(EnvironmentHelper.GetIHostEnvironment())
#else
.AddSingleton<Microsoft.AspNetCore.Hosting.IHostingEnvironment>(EnvironmentHelper.GetIHostingEnvironment())
#endif
.AddSingleton<DiagnosticListener>(new DiagnosticListener("TestListener"));

if (jsonPath != null)
Expand Down
Loading