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

Application Insights ASP.NET Core SDK by default reads application's IConfiguration and all properties of ApplicationInsightsServiceOptions from configuration #1850

Merged
merged 15 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from 14 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Changelog

## VNext
- [Application Insights ASP.NET Core SDK by default reads application's IConfiguration and all properties of ApplicationInsightsServiceOptions from configuration](https://github.com/microsoft/ApplicationInsights-dotnet/pull/1850)
- [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";
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved

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