diff --git a/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/net5.0/PublicAPI.Unshipped.txt b/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/net5.0/PublicAPI.Unshipped.txt
index e69de29bb2..03e69410d4 100644
--- a/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/net5.0/PublicAPI.Unshipped.txt
+++ b/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/net5.0/PublicAPI.Unshipped.txt
@@ -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
\ No newline at end of file
diff --git a/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/netcoreapp3.1/PublicAPI.Unshipped.txt b/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/netcoreapp3.1/PublicAPI.Unshipped.txt
index e69de29bb2..03e69410d4 100644
--- a/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/netcoreapp3.1/PublicAPI.Unshipped.txt
+++ b/.publicApi/Microsoft.ApplicationInsights.AspNetCore.dll/netcoreapp3.1/PublicAPI.Unshipped.txt
@@ -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
\ No newline at end of file
diff --git a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/DefaultApplicationInsightsServiceConfigureOptions.cs b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/DefaultApplicationInsightsServiceConfigureOptions.cs
index d312dcce7e..ad222d9734 100644
--- a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/DefaultApplicationInsightsServiceConfigureOptions.cs
+++ b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/DefaultApplicationInsightsServiceConfigureOptions.cs
@@ -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;
///
@@ -14,33 +16,66 @@
///
internal class DefaultApplicationInsightsServiceConfigureOptions : IConfigureOptions
{
- private readonly IHostingEnvironment hostingEnvironment;
private readonly IConfiguration userConfiguration;
+ private readonly string environmentName;
+ private readonly string contentRootPath;
+
+#if NETCOREAPP3_0_OR_GREATER
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// to use for retreiving ContentRootPath.
+ /// from an application.
+ 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
///
/// Initializes a new instance of the class.
///
/// to use for retreiving ContentRootPath.
/// from an application.
+ [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;
}
///
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);
diff --git a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Microsoft.ApplicationInsights.AspNetCore.csproj b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Microsoft.ApplicationInsights.AspNetCore.csproj
index edfa29199b..047de9efc2 100644
--- a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Microsoft.ApplicationInsights.AspNetCore.csproj
+++ b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/Microsoft.ApplicationInsights.AspNetCore.csproj
@@ -32,10 +32,13 @@
-
-
+
+
+
+
+
diff --git a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/TelemetryInitializers/AspNetCoreEnvironmentTelemetryInitializer.cs b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/TelemetryInitializers/AspNetCoreEnvironmentTelemetryInitializer.cs
index 1fe343cf99..4d2e327f89 100644
--- a/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/TelemetryInitializers/AspNetCoreEnvironmentTelemetryInitializer.cs
+++ b/NETCORE/src/Microsoft.ApplicationInsights.AspNetCore/TelemetryInitializers/AspNetCoreEnvironmentTelemetryInitializer.cs
@@ -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;
///
/// implementation that stamps ASP.NET Core environment name
@@ -12,27 +15,79 @@
public class AspNetCoreEnvironmentTelemetryInitializer : ITelemetryInitializer
{
private const string AspNetCoreEnvironmentPropertyName = "AspNetCoreEnvironment";
- private readonly IHostingEnvironment environment;
+ private readonly Func getEnvironmentName;
///
/// Initializes a new instance of the class.
///
+ ///
+ /// We don't cache the result of EnvironmentName because the environment can be changed while the app is running.
+ ///
/// HostingEnvironment to provide EnvironmentName to be added to telemetry properties.
- 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
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ ///
+ /// We don't cache the result of EnvironmentName because the environment can be changed while the app is running.
+ ///
+ /// HostingEnvironment to provide EnvironmentName to be added to telemetry properties.
+ public AspNetCoreEnvironmentTelemetryInitializer(IHostEnvironment hostEnvironment)
+ {
+ AspNetCoreEventSource.Instance.LogInformational($"{nameof(AspNetCoreEnvironmentTelemetryInitializer)} initialized via {nameof(IHostEnvironment)}");
+
+ this.getEnvironmentName = () => hostEnvironment?.EnvironmentName;
+ }
+
+ ///
+ /// Initializes a new instance of the class.
+ /// This constructor is provided for backwards compatibility where both and have been added to Dependency Injection.
+ ///
+ ///
+ /// We don't cache the result of EnvironmentName because the environment can be changed while the app is running.
+ ///
+ /// IHostEnvironment to provide EnvironmentName to be added to telemetry properties.
+ /// IHostingEnvironment to provide EnvironmentName to be added to telemetry properties.
+ [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
///
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);
+ }
}
}
}
diff --git a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/EnvironmentHelper.cs b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/EnvironmentHelper.cs
index 911a515362..a523b37dd1 100644
--- a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/EnvironmentHelper.cs
+++ b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/EnvironmentHelper.cs
@@ -6,6 +6,10 @@
using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;
+#if NETCOREAPP
+ using IHostEnvironment = Microsoft.Extensions.Hosting.IHostEnvironment;
+#endif
+
///
/// The type was marked Obsolete starting in NetCore3.
/// Here I'm abstracting it's use into a helper method to simplify some of the tests.
@@ -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();
+ mockEnvironment.Setup(x => x.EnvironmentName).Returns("UnitTest");
+ mockEnvironment.Setup(x => x.ContentRootPath).Returns(Directory.GetCurrentDirectory());
+ return mockEnvironment.Object;
+ }
+#endif
}
}
diff --git a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests/AddApplicationInsightsTelemetryTests.cs b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests/AddApplicationInsightsTelemetryTests.cs
index 3282ffa5e3..b1f48ad740 100644
--- a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests/AddApplicationInsightsTelemetryTests.cs
+++ b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests/AddApplicationInsightsTelemetryTests.cs
@@ -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
@@ -772,7 +773,12 @@ public static void AddApplicationInsightsTelemetryProcessorWithImportingConstruc
// TP added via AddApplicationInsightsTelemetryProcessor is added to the default sink.
FakeTelemetryProcessorWithImportingConstructor telemetryProcessor = telemetryConfiguration.DefaultTelemetrySink.TelemetryProcessors.OfType().FirstOrDefault();
Assert.NotNull(telemetryProcessor);
- Assert.Same(serviceProvider.GetService(), telemetryProcessor.HostingEnvironment);
+
+#if NETCOREAPP
+ Assert.Same(serviceProvider.GetService(), telemetryProcessor.HostingEnvironment);
+#else
+ Assert.Same(serviceProvider.GetService(), telemetryProcessor.HostingEnvironment);
+#endif
}
[Fact]
diff --git a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests/BaseTestClass.cs b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests/BaseTestClass.cs
index cbcf156f88..8a593df838 100644
--- a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests/BaseTestClass.cs
+++ b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests/BaseTestClass.cs
@@ -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";
@@ -81,7 +81,11 @@ public static ServiceCollection CreateServicesAndAddApplicationinsightsTelemetry
public static ServiceCollection GetServiceCollectionWithContextAccessor()
{
var services = new ServiceCollection();
- services.AddSingleton(EnvironmentHelper.GetIHostingEnvironment());
+#if NETCOREAPP
+ services.AddSingleton(EnvironmentHelper.GetIHostEnvironment());
+#else
+ services.AddSingleton(EnvironmentHelper.GetIHostingEnvironment());
+#endif
services.AddSingleton(new DiagnosticListener("TestListener"));
return services;
}
diff --git a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsServiceOptionsTests.cs b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsServiceOptionsTests.cs
index 50aaa23a63..ee7689cdce 100644
--- a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsServiceOptionsTests.cs
+++ b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsServiceOptionsTests.cs
@@ -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;
@@ -76,7 +74,11 @@ private static ServiceCollection CreateServicesAndAddApplicationinsightsWorker(s
{
IConfigurationRoot config;
var services = new ServiceCollection()
- .AddSingleton(EnvironmentHelper.GetIHostingEnvironment())
+#if NETCOREAPP
+ .AddSingleton(EnvironmentHelper.GetIHostEnvironment())
+#else
+ .AddSingleton(EnvironmentHelper.GetIHostingEnvironment())
+#endif
.AddSingleton(new DiagnosticListener("TestListener"));
if (jsonPath != null)
diff --git a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/DefaultApplicationInsightsServiceConfigureOptionsTests.cs b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/DefaultApplicationInsightsServiceConfigureOptionsTests.cs
new file mode 100644
index 0000000000..8ea311ed44
--- /dev/null
+++ b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/DefaultApplicationInsightsServiceConfigureOptionsTests.cs
@@ -0,0 +1,87 @@
+namespace Microsoft.ApplicationInsights.AspNetCore.Tests.Extensions
+{
+ using System;
+ using System.Collections.Generic;
+ using System.IO;
+ using System.Linq;
+ using System.Text;
+ using System.Threading.Tasks;
+
+ using Microsoft.ApplicationInsights.AspNetCore.Extensions;
+ using Microsoft.AspNetCore.Hosting;
+ using Microsoft.Extensions.Configuration;
+ using Microsoft.Extensions.DependencyInjection;
+ using Microsoft.Extensions.DependencyInjection.Extensions;
+ using Microsoft.Extensions.Options;
+
+ using Xunit;
+
+ ///
+ /// Because now has two constructors,
+ /// these tests verify that dependency injection works as expected.
+ ///
+ public class DefaultApplicationInsightsServiceConfigureOptionsTests
+ {
+ [Fact]
+ public void Verify_IHostingEnvironment()
+ {
+#pragma warning disable CS0618 // Type or member is obsolete
+ var mockHostingEnvironment = new Moq.Mock();
+
+ mockHostingEnvironment.Setup(x => x.ContentRootPath).Returns(Directory.GetCurrentDirectory());
+
+ var services = new ServiceCollection()
+ .AddSingleton(mockHostingEnvironment.Object);
+
+ services.TryAddSingleton, DefaultApplicationInsightsServiceConfigureOptions>();
+
+ IServiceProvider serviceProvider = services.BuildServiceProvider();
+ var test = serviceProvider.GetService>();
+
+ Assert.NotNull(test);
+#pragma warning restore CS0618 // Type or member is obsolete
+ }
+
+#if NETCOREAPP
+ [Fact]
+ public void Verify_IHostEnvironment()
+ {
+ var mockHostEnvironment = new Moq.Mock();
+ mockHostEnvironment.Setup(x => x.ContentRootPath).Returns(Directory.GetCurrentDirectory());
+
+ var services = new ServiceCollection()
+ .AddSingleton(mockHostEnvironment.Object);
+
+ services.TryAddSingleton, DefaultApplicationInsightsServiceConfigureOptions>();
+
+ IServiceProvider serviceProvider = services.BuildServiceProvider();
+ var test = serviceProvider.GetService>();
+
+ Assert.NotNull(test);
+ }
+
+ [Fact]
+ public void Verify_Both()
+ {
+#pragma warning disable CS0618 // Type or member is obsolete
+ var mockHostingEnvironment = new Moq.Mock();
+ mockHostingEnvironment.Setup(x => x.ContentRootPath).Returns(Directory.GetCurrentDirectory());
+
+ var mockHostEnvironment = new Moq.Mock();
+ mockHostEnvironment.Setup(x => x.ContentRootPath).Returns(Directory.GetCurrentDirectory());
+
+ var services = new ServiceCollection()
+ .AddSingleton(mockHostingEnvironment.Object)
+ .AddSingleton(mockHostEnvironment.Object);
+
+ services.TryAddSingleton, DefaultApplicationInsightsServiceConfigureOptions>();
+
+ IServiceProvider serviceProvider = services.BuildServiceProvider();
+ var test = serviceProvider.GetService>();
+
+ Assert.NotNull(test);
+#pragma warning restore CS0618 // Type or member is obsolete
+ }
+#endif
+ }
+}
diff --git a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/FakeTelemetryProcessorWithImportingConstructor.cs b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/FakeTelemetryProcessorWithImportingConstructor.cs
index 6a514dd99c..a818f2c1e9 100644
--- a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/FakeTelemetryProcessorWithImportingConstructor.cs
+++ b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/FakeTelemetryProcessorWithImportingConstructor.cs
@@ -3,12 +3,14 @@
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Hosting;
+ using Microsoft.Extensions.Hosting;
internal class FakeTelemetryProcessorWithImportingConstructor : ITelemetryProcessor
{
private readonly ITelemetryProcessor nextProcessor;
- public IHostingEnvironment HostingEnvironment { get; }
+#if NETCOREAPP
+ public IHostEnvironment HostingEnvironment { get; }
///
/// Constructs an instance of the telemetry processor.
@@ -16,11 +18,26 @@ internal class FakeTelemetryProcessorWithImportingConstructor : ITelemetryProces
///
/// The next procesor in the chain.
/// The hosting environment. This parameter will be injected by the DI framework.
- public FakeTelemetryProcessorWithImportingConstructor(ITelemetryProcessor next, IHostingEnvironment hostingEnvironment)
+ public FakeTelemetryProcessorWithImportingConstructor(ITelemetryProcessor next, IHostEnvironment hostingEnvironment)
{
this.nextProcessor = next;
this.HostingEnvironment = hostingEnvironment;
}
+#else
+ public Microsoft.AspNetCore.Hosting.IHostingEnvironment HostingEnvironment { get; }
+
+ ///
+ /// Constructs an instance of the telemetry processor.
+ /// This constructor is designed to be called from a DI framework.
+ ///
+ /// The next procesor in the chain.
+ /// The hosting environment. This parameter will be injected by the DI framework.
+ public FakeTelemetryProcessorWithImportingConstructor(ITelemetryProcessor next, Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment)
+ {
+ this.nextProcessor = next;
+ this.HostingEnvironment = hostingEnvironment;
+ }
+#endif
public void Process(ITelemetry item)
{
diff --git a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Microsoft.ApplicationInsights.AspNetCore.Tests.csproj b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Microsoft.ApplicationInsights.AspNetCore.Tests.csproj
index 5666ded167..435f8bd0d2 100644
--- a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Microsoft.ApplicationInsights.AspNetCore.Tests.csproj
+++ b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Microsoft.ApplicationInsights.AspNetCore.Tests.csproj
@@ -24,12 +24,7 @@
-
-
-
-
+
diff --git a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/TelemetryInitializers/AspNetCoreEnvironmentTelemetryInitializerTests.cs b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/TelemetryInitializers/AspNetCoreEnvironmentTelemetryInitializerTests.cs
index c7522dca80..6550f44ab7 100644
--- a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/TelemetryInitializers/AspNetCoreEnvironmentTelemetryInitializerTests.cs
+++ b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/TelemetryInitializers/AspNetCoreEnvironmentTelemetryInitializerTests.cs
@@ -2,22 +2,25 @@
{
using Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers;
using Microsoft.ApplicationInsights.DataContracts;
- using Microsoft.AspNetCore.Hosting.Internal;
using Xunit;
public class AspNetCoreEnvironmentTelemetryInitializerTests
{
[Fact]
- public void InitializeDoesNotThrowIfHostingEnvironmentIsNull()
+ public void InitializeDoesNotThrowIfHostingEnvironmentIsNull_IHostingEnvironment()
{
- var initializer = new AspNetCoreEnvironmentTelemetryInitializer(environment: null);
- initializer.Initialize(new RequestTelemetry());
+#pragma warning disable CS0618 // Type or member is obsolete
+ var initializer1 = new AspNetCoreEnvironmentTelemetryInitializer(environment: null);
+#pragma warning restore CS0618 // Type or member is obsolete
+ initializer1.Initialize(new RequestTelemetry());
}
[Fact]
- public void InitializeDoesNotOverrideExistingProperty()
+ public void InitializeDoesNotOverrideExistingProperty_IHostingEnvironment()
{
+#pragma warning disable CS0618 // Type or member is obsolete
var initializer = new AspNetCoreEnvironmentTelemetryInitializer(environment: EnvironmentHelper.GetIHostingEnvironment());
+#pragma warning restore CS0618 // Type or member is obsolete
var telemetry = new RequestTelemetry();
telemetry.Context.GlobalProperties.Add("AspNetCoreEnvironment", "Development");
initializer.Initialize(telemetry);
@@ -27,13 +30,46 @@ public void InitializeDoesNotOverrideExistingProperty()
}
[Fact]
- public void InitializeSetsCurrentEnvironmentNameToProperty()
+ public void InitializeSetsCurrentEnvironmentNameToProperty_IHostingEnvironment()
{
+#pragma warning disable CS0618 // Type or member is obsolete
var initializer = new AspNetCoreEnvironmentTelemetryInitializer(environment: EnvironmentHelper.GetIHostingEnvironment());
+#pragma warning restore CS0618 // Type or member is obsolete
var telemetry = new RequestTelemetry();
initializer.Initialize(telemetry);
Assert.Equal("UnitTest", telemetry.Properties["AspNetCoreEnvironment"]);
}
+
+#if NETCOREAPP
+ [Fact]
+ public void InitializeDoesNotThrowIfHostingEnvironmentIsNull_IHostEnvironment()
+ {
+ var initializer2 = new AspNetCoreEnvironmentTelemetryInitializer(hostEnvironment: null);
+ initializer2.Initialize(new RequestTelemetry());
+ }
+
+ [Fact]
+ public void InitializeDoesNotOverrideExistingProperty_IHostEnvironment()
+ {
+ var initializer = new AspNetCoreEnvironmentTelemetryInitializer(hostEnvironment: EnvironmentHelper.GetIHostEnvironment());
+ var telemetry = new RequestTelemetry();
+ telemetry.Context.GlobalProperties.Add("AspNetCoreEnvironment", "Development");
+ initializer.Initialize(telemetry);
+
+ Assert.Equal("Development", telemetry.Context.GlobalProperties["AspNetCoreEnvironment"]);
+ Assert.Equal("UnitTest", telemetry.Properties["AspNetCoreEnvironment"]);
+ }
+
+ [Fact]
+ public void InitializeSetsCurrentEnvironmentNameToProperty_IHostEnvironment()
+ {
+ var initializer = new AspNetCoreEnvironmentTelemetryInitializer(hostEnvironment: EnvironmentHelper.GetIHostEnvironment());
+ var telemetry = new RequestTelemetry();
+ initializer.Initialize(telemetry);
+
+ Assert.Equal("UnitTest", telemetry.Properties["AspNetCoreEnvironment"]);
+ }
+#endif
}
}