Skip to content

Commit

Permalink
Application Insights ASP.NET Core SDK by default reads application's …
Browse files Browse the repository at this point in the history
…IConfiguration and all properties of ApplicationInsightsServiceOptions from configuration (#1850)

* Reads configuration from all defined sources from app along with all fields of ApplicationInsightsServiceOptions

* Added test cases and changed configuration binding.

* Modified test appSettings.json

* Added support to NET46 test case.

* Modified test names

* Modified ChangeLog

* Added test for WorkerService

* Updated Changelog

Co-authored-by: Timothy Mothra <tilee@microsoft.com>
Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
  • Loading branch information
3 people authored May 29, 2020
1 parent d331966 commit e6635a2
Show file tree
Hide file tree
Showing 24 changed files with 1,685 additions and 173 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Changelog

## VNext
- [Read all properties of ApplicationInsightsServiceOptions from IConfiguration](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1882)
- [End support for NetStandard 1.x, Add support for NetStandard 2.0](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1160)
- [Add support for SourceLink.Github to all SDKs.](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1760)


## Version 2.15.0-beta1
- [WorkerService package is modified to depend on 2.1.1 on Microsoft.Extensions.DependencyInjection so that it can be used in .NET Core 2.1 projects without nuget errors.](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1677)
- [Adding a flag to EventCounterCollector to enable/disable storing the EventSource name in the MetricNamespace and simplify the metric name](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1341)
- [New: EventCounter to track Ingestion Endpoint Response Time] (https://github.com/microsoft/ApplicationInsights-dotnet/pull/1796)
- [New: EventCounter to track Ingestion Endpoint Response Time](https://github.com/microsoft/ApplicationInsights-dotnet/pull/1796)

## Version 2.14.0
- no changes since beta.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,34 @@
internal class DefaultApplicationInsightsServiceConfigureOptions : IConfigureOptions<ApplicationInsightsServiceOptions>
{
private readonly IHostingEnvironment hostingEnvironment;
private readonly IConfiguration userConfiguration;

/// <summary>
/// Initializes a new instance of the <see cref="DefaultApplicationInsightsServiceConfigureOptions"/> class.
/// </summary>
/// <param name="hostingEnvironment"><see cref="IHostingEnvironment"/> to use for retreiving ContentRootPath.</param>
public DefaultApplicationInsightsServiceConfigureOptions(IHostingEnvironment hostingEnvironment)
/// <param name="configuration"><see cref="IConfiguration"/> from an application.</param>
public DefaultApplicationInsightsServiceConfigureOptions(IHostingEnvironment hostingEnvironment, IConfiguration configuration = null)
{
this.hostingEnvironment = hostingEnvironment;
this.userConfiguration = configuration;
}

/// <inheritdoc />
public void Configure(ApplicationInsightsServiceOptions options)
{
var configBuilder = new ConfigurationBuilder()
.SetBasePath(this.hostingEnvironment.ContentRootPath ?? Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", true)
.AddJsonFile(string.Format(CultureInfo.InvariantCulture, "appsettings.{0}.json", this.hostingEnvironment.EnvironmentName), true)
.AddEnvironmentVariables();
.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();

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

if (Debugger.IsAttached)
Expand Down
33 changes: 13 additions & 20 deletions NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ 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 @@ -224,26 +229,9 @@ public static IConfigurationBuilder AddApplicationInsightsSettings(
}

/// <summary>
/// Read from configuration
/// Config.json will look like this:
/// <para>
/// "ApplicationInsights": {
/// "InstrumentationKey": "11111111-2222-3333-4444-555555555555",
/// "TelemetryChannel": {
/// "EndpointAddress": "http://dc.services.visualstudio.com/v2/track",
/// "DeveloperMode": true
/// }
/// }.
/// </para>
/// Or.
/// <para>
/// "ApplicationInsights": {
/// "ConnectionString" : "InstrumentationKey=11111111-2222-3333-4444-555555555555;IngestionEndpoint=http://dc.services.visualstudio.com"
/// "TelemetryChannel": {
/// "DeveloperMode": true
/// }
/// }.
/// </para>
/// Read configuration from appSettings.json, appsettings.{env.EnvironmentName}.json,
/// IConfiguation used in an application and EnvironmentVariables.
/// Bind configuration to ApplicationInsightsServiceOptions.
/// Values can also be read from environment variables to support azure web sites configuration.
/// </summary>
/// <param name="config">Configuration to read variables from.</param>
Expand All @@ -254,6 +242,11 @@ 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))
{
serviceOptions.ConnectionString = connectionStringValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ internal void CopyPropertiesTo(ApplicationInsightsServiceOptions target)
target.InstrumentationKey = this.InstrumentationKey;
}

target.ConnectionString = this.ConnectionString;
if (!string.IsNullOrEmpty(this.ConnectionString))
{
target.ConnectionString = this.ConnectionString;
}

target.ApplicationVersion = this.ApplicationVersion;
target.EnableAdaptiveSampling = this.EnableAdaptiveSampling;
target.EnableDebugLogger = this.EnableDebugLogger;
Expand Down
Loading

0 comments on commit e6635a2

Please sign in to comment.