From aee203a28f8ac6ea0f4dfbdf5012ed7b5d4dd81d Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Wed, 18 Apr 2018 20:38:34 +0200 Subject: [PATCH 1/3] NLog ApplicationInsightsTarget Flush with async delay when something to flush --- src/NLogTarget/ApplicationInsightsTarget.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/NLogTarget/ApplicationInsightsTarget.cs b/src/NLogTarget/ApplicationInsightsTarget.cs index ca71ee9f..9cf1a554 100644 --- a/src/NLogTarget/ApplicationInsightsTarget.cs +++ b/src/NLogTarget/ApplicationInsightsTarget.cs @@ -28,6 +28,7 @@ namespace Microsoft.ApplicationInsights.NLogTarget public sealed class ApplicationInsightsTarget : TargetWithLayout { private TelemetryClient telemetryClient; + private DateTime lastLogEventTime; /// /// Initializers a new instance of ApplicationInsightsTarget type. @@ -108,6 +109,8 @@ protected override void Write(LogEventInfo logEvent) throw new ArgumentNullException("logEvent"); } + this.lastLogEventTime = DateTime.UtcNow; + if (logEvent.Exception != null) { this.SendException(logEvent); @@ -127,7 +130,17 @@ protected override void FlushAsync(AsyncContinuation asyncContinuation) try { this.TelemetryClient.Flush(); - asyncContinuation(null); + if (DateTime.UtcNow.AddSeconds(-30) > this.lastLogEventTime) + { + // Nothing has been written, so nothing to wait for + asyncContinuation(null); + } + else + { + // Documentation says it is important to wait after flush, else nothing will happen + // https://docs.microsoft.com/en-us/azure/application-insights/app-insights-api-custom-events-metrics#flushing-data + System.Threading.Tasks.Task.Delay(500).ContinueWith((task) => asyncContinuation(null)); + } } catch (Exception ex) { From 7f6089838759b0aca285051b7ebf43e3f7a4831a Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Wed, 18 Apr 2018 21:09:15 +0200 Subject: [PATCH 2/3] NLog ApplicationInsightsTarget Flush with async delay when something to flush (changelog) --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffed61f1..550074df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +### Version 2.6.0 +- [NLog Flush should include async delay](https://github.com/Microsoft/ApplicationInsights-dotnet-logging/pull/176) + ### Version 2.6.0-beta3 - [NetStandard Support for TraceListener](https://github.com/Microsoft/ApplicationInsights-dotnet-logging/pull/166) - [NetStandard Support for NLog and log4net](https://github.com/Microsoft/ApplicationInsights-dotnet-logging/pull/167) From e74909ec1768083a547e54ddaa21814d0eaa8459 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Thu, 19 Apr 2018 01:03:43 +0200 Subject: [PATCH 3/3] NLog ApplicationInsightsTarget Flush with async delay when something to flush (code review) --- src/NLogTarget/ApplicationInsightsTarget.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NLogTarget/ApplicationInsightsTarget.cs b/src/NLogTarget/ApplicationInsightsTarget.cs index 9cf1a554..7233d815 100644 --- a/src/NLogTarget/ApplicationInsightsTarget.cs +++ b/src/NLogTarget/ApplicationInsightsTarget.cs @@ -138,8 +138,8 @@ protected override void FlushAsync(AsyncContinuation asyncContinuation) else { // Documentation says it is important to wait after flush, else nothing will happen - // https://docs.microsoft.com/en-us/azure/application-insights/app-insights-api-custom-events-metrics#flushing-data - System.Threading.Tasks.Task.Delay(500).ContinueWith((task) => asyncContinuation(null)); + // https://docs.microsoft.com/azure/application-insights/app-insights-api-custom-events-metrics#flushing-data + System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(500)).ContinueWith((task) => asyncContinuation(null)); } } catch (Exception ex)