Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #176 from snakefoot/develop
Browse files Browse the repository at this point in the history
NLog ApplicationInsightsTarget Flush with async delay
  • Loading branch information
Sergey Kanzhelev committed Apr 19, 2018
2 parents b081414 + effb3df commit 388ff8e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
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

0 comments on commit 388ff8e

Please sign in to comment.