diff --git a/LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs b/LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs index d4ba9304a7..28cef263a7 100644 --- a/LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs +++ b/LOGGING/src/NLogTarget/ApplicationInsightsTarget.cs @@ -30,7 +30,6 @@ namespace Microsoft.ApplicationInsights.NLogTarget public sealed class ApplicationInsightsTarget : TargetWithLayout { private TelemetryClient telemetryClient; - private DateTime lastLogEventTime; private NLog.Layouts.Layout instrumentationKeyLayout = string.Empty; private NLog.Layouts.Layout connectionStringLayout = string.Empty; @@ -68,12 +67,9 @@ public string ConnectionString public IList ContextProperties { get; } = new List(); /// - /// Gets the logging controller we will be using. + /// Gets or sets the factory for creating TelemetryConfiguration, so unit-tests can override in-memory-channel. /// - internal TelemetryClient TelemetryClient - { - get { return this.telemetryClient; } - } + internal Func TelemetryConfigurationFactory { get; set; } internal void BuildPropertyBag(LogEventInfo logEvent, ITelemetry trace) { @@ -132,7 +128,7 @@ protected override void InitializeTarget() // configure new telemetryclient with the connectionstring otherwise using legacy instrumentationkey. if (!string.IsNullOrWhiteSpace(connectionString)) { - var telemetryConfiguration = TelemetryConfiguration.CreateDefault(); + var telemetryConfiguration = this.TelemetryConfigurationFactory?.Invoke() ?? TelemetryConfiguration.CreateDefault(); telemetryConfiguration.ConnectionString = connectionString; this.telemetryClient = new TelemetryClient(telemetryConfiguration); } @@ -162,8 +158,6 @@ protected override void Write(LogEventInfo logEvent) throw new ArgumentNullException(nameof(logEvent)); } - this.lastLogEventTime = DateTime.UtcNow; - if (logEvent.Exception != null) { this.SendException(logEvent); @@ -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) { @@ -197,14 +191,11 @@ protected override void FlushAsync(AsyncContinuation asyncContinuation) private static void LoadLogEventProperties(LogEventInfo logEvent, IDictionary propertyBag) { - if (logEvent.Properties?.Count > 0) + foreach (var keyValuePair in logEvent.Properties) { - foreach (var keyValuePair in logEvent.Properties) - { - string key = keyValuePair.Key.ToString(); - object valueObj = keyValuePair.Value; - PopulatePropertyBag(propertyBag, key, valueObj); - } + string key = keyValuePair.Key.ToString(); + object valueObj = keyValuePair.Value; + PopulatePropertyBag(propertyBag, key, valueObj); } } diff --git a/LOGGING/test/NLogTarget.Tests/NLogTargetTests.cs b/LOGGING/test/NLogTarget.Tests/NLogTargetTests.cs index bab41820f9..1ce476bc81 100644 --- a/LOGGING/test/NLogTarget.Tests/NLogTargetTests.cs +++ b/LOGGING/test/NLogTarget.Tests/NLogTargetTests.cs @@ -455,15 +455,13 @@ public void NLogTargetFlushesTelemetryClient() NLog.Common.AsyncContinuation asyncContinuation = (ex) => { flushException = ex; flushEvent.Set(); }; aiLogger.Factory.Flush(asyncContinuation, 5000); Assert.IsTrue(flushEvent.WaitOne(5000)); - Assert.IsNotNull(flushException); - Assert.AreEqual("Flush called", flushException.Message); } [TestMethod] [TestCategory("NLogTarget")] public void NLogInfoIsSentAsInformationTraceItemWithAIConnectionString() { - var aiLogger = this.CreateTargetWithGivenConnectionString("Your_ApplicationInsights_ConnectionString"); + var aiLogger = this.CreateTargetWithGivenConnectionString(); aiLogger.Info("Info message"); var telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.First(); @@ -474,7 +472,7 @@ public void NLogInfoIsSentAsInformationTraceItemWithAIConnectionString() [TestCategory("NLogTarget")] public void NLogTraceIsSentAsVerboseTraceItemWithAIConnectionString() { - var aiLogger = this.CreateTargetWithGivenConnectionString("Your_ApplicationInsights_ConnectionString"); + var aiLogger = this.CreateTargetWithGivenConnectionString(); aiLogger.Trace("Trace message"); var telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.FirstOrDefault(); @@ -485,7 +483,7 @@ public void NLogTraceIsSentAsVerboseTraceItemWithAIConnectionString() [TestCategory("NLogTarget")] public void NLogDebugIsSentAsVerboseTraceItemWithAIConnectionString() { - var aiLogger = this.CreateTargetWithGivenConnectionString("Your_ApplicationInsights_ConnectionString"); + var aiLogger = this.CreateTargetWithGivenConnectionString(); aiLogger.Debug("Debug Message"); var telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.FirstOrDefault(); @@ -496,7 +494,7 @@ public void NLogDebugIsSentAsVerboseTraceItemWithAIConnectionString() [TestCategory("NLogTarget")] public void NLogWarnIsSentAsWarningTraceItemWithAIConnectionString() { - var aiLogger = this.CreateTargetWithGivenConnectionString("Your_ApplicationInsights_ConnectionString"); + var aiLogger = this.CreateTargetWithGivenConnectionString(); aiLogger.Warn("Warn message"); @@ -508,7 +506,7 @@ public void NLogWarnIsSentAsWarningTraceItemWithAIConnectionString() [TestCategory("NLogTarget")] public void NLogErrorIsSentAsVerboseTraceItemWithAIConnectionString() { - var aiLogger = this.CreateTargetWithGivenConnectionString("InstrumentationKey=b91a8f48-c77c-4f12-80e2-f96bc1abb126;IngestionEndpoint=https://centralus-2.in.applicationinsights.azure.com/;LiveEndpoint=https://centralus.livediagnostics.monitor.azure.com/"); + var aiLogger = this.CreateTargetWithGivenConnectionString(); aiLogger.Error("Error Message"); var telemetry = (TraceTelemetry)this.adapterHelper.Channel.SentItems.FirstOrDefault(); @@ -562,19 +560,16 @@ private Logger CreateTargetWithGivenInstrumentationKey( } private Logger CreateTargetWithGivenConnectionString( - string connectionString = "Your_ApplicationInsights_ConnectionString", + string connectionString = "InstrumentationKey=b91a8f48-c77c-4f12-80e2-f96bc1abb126;IngestionEndpoint=https://localhost/;LiveEndpoint=https://localhost/", 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.TelemetryConfigurationFactory = () => new TelemetryConfiguration() { TelemetryChannel = this.adapterHelper.Channel }; + 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();