PlanTempusApp/Core/Telemetry/SeqTelemetryChannel.cs

44 lines
1.5 KiB
C#

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Channel;
namespace PlanTempus.Core.Telemetry
{
public class SeqTelemetryChannel : InMemoryChannel, ITelemetryChannel
{
private readonly IMessageChannel<ITelemetry> _messageChannel;
private readonly TelemetryClient _telemetryClient;
public SeqTelemetryChannel(IMessageChannel<ITelemetry> messageChannel, TelemetryClient telemetryClient)
{
_messageChannel = messageChannel;
_telemetryClient = telemetryClient;
}
public new void Send(ITelemetry telemetry)
{
if (telemetry.Context.GlobalProperties.TryGetValue("OmitSeqTelemetryChannel", out string value))
if (value == "true")
{
base.Send(telemetry);
return;
}
try
{
var writeTask = _messageChannel.Writer.WriteAsync(telemetry).AsTask();
writeTask.ContinueWith(t =>
{
if (t.Exception != null)
{
throw t.Exception;
}
}, TaskContinuationOptions.OnlyOnFaulted);
}
catch (Exception e)
{
_telemetryClient.TrackException(e, new Dictionary<string, string> { { "OmitSeqTelemetryChannel", "true" } });
}
base.Send(telemetry);
}
}
}