diff --git a/LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs b/LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs index d4ba9304a7..bfa565fb55 100644 --- a/LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs +++ b/LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs @@ -67,13 +67,7 @@ public string ConnectionString [ArrayParameter(typeof(TargetPropertyWithContext), "contextproperty")] public IList ContextProperties { get; } = new List(); - /// - /// Gets the logging controller we will be using. - /// - internal TelemetryClient TelemetryClient - { - get { return this.telemetryClient; } - } + internal Func TelemetryClientFactory { get; set; } = new Func((cfg) => new TelemetryClient(cfg)); internal void BuildPropertyBag(LogEventInfo logEvent, ITelemetry trace) { @@ -134,7 +128,7 @@ protected override void InitializeTarget() { var telemetryConfiguration = TelemetryConfiguration.CreateDefault(); telemetryConfiguration.ConnectionString = connectionString; - this.telemetryClient = new TelemetryClient(telemetryConfiguration); + this.telemetryClient = TelemetryClientFactory(telemetryConfiguration); } else { @@ -187,7 +181,7 @@ protected override void FlushAsync(AsyncContinuation asyncContinuation) try { - this.TelemetryClient.FlushAsync(System.Threading.CancellationToken.None).ContinueWith(t => asyncContinuation(t.Exception)); + this.telemetryClient.FlushAsync(System.Threading.CancellationToken.None).ContinueWith(t => asyncContinuation(t.Exception)); } catch (Exception ex) { diff --git a/LOGGING/test/NLogTarget.Tests/NLogTargetTests.cs b/LOGGING/test/NLogTarget.Tests/NLogTargetTests.cs index bab41820f9..7942dc04cb 100644 --- a/LOGGING/test/NLogTarget.Tests/NLogTargetTests.cs +++ b/LOGGING/test/NLogTarget.Tests/NLogTargetTests.cs @@ -565,16 +565,13 @@ private Logger CreateTargetWithGivenConnectionString( string connectionString = "Your_ApplicationInsights_ConnectionString", Action loggerAction = null) { - // Mock channel to validate that our appender is trying to send logs -#pragma warning disable CS0618 // Type or member is obsolete - TelemetryConfiguration.Active.TelemetryChannel = this.adapterHelper.Channel; -#pragma warning restore CS0618 // Type or member is obsolete - ApplicationInsightsTarget target = new ApplicationInsightsTarget { ConnectionString = connectionString }; + target.TelemetryClientFactory = (cfg) => { cfg.TelemetryChannel = this.adapterHelper.Channel; return new TelemetryClient(cfg); }; + LoggingRule rule = new LoggingRule("*", LogLevel.Trace, target); LoggingConfiguration config = new LoggingConfiguration(); config.AddTarget("AITarget", target); diff --git a/LOGGING/test/Shared/AdapterHelper.cs b/LOGGING/test/Shared/AdapterHelper.cs index 9106afb3f8..9b926f8599 100644 --- a/LOGGING/test/Shared/AdapterHelper.cs +++ b/LOGGING/test/Shared/AdapterHelper.cs @@ -19,8 +19,6 @@ public class AdapterHelper : IDisposable { public string InstrumentationKey { get; } - public string ConnectionString { get; } - #if NET452 || NET46 private static readonly string ApplicationInsightsConfigFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ApplicationInsights.config"); @@ -29,19 +27,16 @@ public class AdapterHelper : IDisposable Path.Combine(Path.GetDirectoryName(typeof(AdapterHelper).GetTypeInfo().Assembly.Location), "ApplicationInsights.config"); #endif - public AdapterHelper(string instrumentationKey = "F8474271-D231-45B6-8DD4-D344C309AE69" - , string connectionString = "Your_ApplicationInsights_ConnectionString") + public AdapterHelper(string instrumentationKey = "F8474271-D231-45B6-8DD4-D344C309AE69") { this.InstrumentationKey = instrumentationKey; - this.ConnectionString = connectionString; string configuration = string.Format(InvariantCulture, @" {0} ", - instrumentationKey, - connectionString); + instrumentationKey); File.WriteAllText(ApplicationInsightsConfigFilePath, configuration); this.Channel = new CustomTelemetryChannel();