Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
15 changes: 14 additions & 1 deletion src/NLogTarget/ApplicationInsightsTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace Microsoft.ApplicationInsights.NLogTarget
public sealed class ApplicationInsightsTarget : TargetWithLayout
{
private TelemetryClient telemetryClient;
private DateTime lastLogEventTime;

/// <summary>
/// Initializers a new instance of ApplicationInsightsTarget type.
Expand Down Expand Up @@ -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);
Expand All @@ -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/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)
{
Expand Down