public static IServiceCollection AddApplicationInsightsTelemetryReplication( this IServiceCollection services, Action <TelemetryProxyOptions> configure) { services.AddSingleton <ServiceProviderTelemetryReplicatorsFactory>(); services.AddSingleton(provider => { var options = new TelemetryProxyOptions(); configure?.Invoke(options); if (options.TelemetryReplicatorFactory == null) { options.TelemetryReplicatorFactory = provider.GetService <ServiceProviderTelemetryReplicatorsFactory>(); } if (string.IsNullOrEmpty(options.EndpointAddress)) { var telemetryConfiguration = provider.GetService <TelemetryConfiguration>(); options.EndpointAddress = TelemetryConfigurationHolder.OriginalEndpointAddress ?? telemetryConfiguration.TelemetryChannel.EndpointAddress; } return(options); }); services.AddSingleton(provider => { var options = provider.GetService <TelemetryProxyOptions>(); return(new TelemetryProxy(options)); }); return(services); }
/// <summary> /// Initializes new TelemetryProxy instance with given options. /// </summary> /// <param name="options">TelemetryProxy options.</param> public TelemetryProxy(TelemetryProxyOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (string.IsNullOrWhiteSpace(options.EndpointAddress)) { throw new ArgumentException($"{nameof(options.EndpointAddress)} is required."); } EndpointUri = new Uri(options.EndpointAddress, UriKind.RelativeOrAbsolute); if (false == EndpointUri.IsAbsoluteUri) { throw new ArgumentException($"{nameof(options.EndpointAddress)} must be an absolute uri."); } if (options.LoggerFactory == null) { logger = new Internal.Logger(GetType().FullName, LogLevel.Trace); } else { logger = options.LoggerFactory.CreateLogger <TelemetryProxy>(); } if (options.HttpClientFactory == null) { options.HttpClientFactory = CreateDefaultHttpClient; } this.options = options; httpClient = options.HttpClientFactory(); Replicators = options.TelemetryReplicatorFactory?.Create() ?? Enumerable.Empty <ITelemetryReplicator>(); }