MessageChannel work for Seq Logging
This commit is contained in:
parent
e777135d62
commit
bf50563ab7
6 changed files with 119 additions and 84 deletions
|
|
@ -1,64 +1,101 @@
|
|||
using Microsoft.ApplicationInsights;
|
||||
using Microsoft.ApplicationInsights.Channel;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
|
||||
namespace Core.Telemetry
|
||||
{
|
||||
public class SeqBackgroundService : BackgroundService
|
||||
{
|
||||
private readonly IMessageChannel _messageChannel;
|
||||
private readonly TelemetryClient _telemetryClient;
|
||||
private readonly HttpClient _httpClient;
|
||||
public class SeqBackgroundService : BackgroundService
|
||||
{
|
||||
private readonly IMessageChannel<ITelemetry> _messageChannel;
|
||||
private readonly TelemetryClient _telemetryClient;
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public SeqBackgroundService(
|
||||
TelemetryClient telemetryClient,
|
||||
IMessageChannel messageChannel,
|
||||
HttpClient httpClient)
|
||||
{
|
||||
_telemetryClient = telemetryClient;
|
||||
_messageChannel = messageChannel;
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
public SeqBackgroundService(TelemetryClient telemetryClient,
|
||||
IMessageChannel<ITelemetry> messageChannel,
|
||||
HttpClient httpClient)
|
||||
{
|
||||
_telemetryClient = telemetryClient;
|
||||
_messageChannel = messageChannel;
|
||||
_httpClient = httpClient;
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
await foreach (var message in _messageChannel.Reader.ReadAllAsync(stoppingToken))
|
||||
{
|
||||
_httpClient = new HttpClient()
|
||||
{
|
||||
BaseAddress = new Uri("http://localhost:5341"),
|
||||
Timeout = TimeSpan.FromSeconds(30)
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
//using var response = await _httpClient.SendAsync(message, stoppingToken);
|
||||
//if (!response.IsSuccessStatusCode)
|
||||
//{
|
||||
// _telemetryClient.TrackTrace($"HTTP kald fejlede med status {response.StatusCode}", Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Warning);
|
||||
// continue;
|
||||
//}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_telemetryClient.TrackException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (ex is not OperationCanceledException)
|
||||
{
|
||||
_telemetryClient.TrackException(ex);
|
||||
throw;
|
||||
}
|
||||
_httpClient.DefaultRequestHeaders.Accept.Clear();
|
||||
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
|
||||
_telemetryClient.TrackTrace("Service shutdown påbegyndt");
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
}
|
||||
|
||||
_messageChannel.Dispose();
|
||||
await base.StopAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
await foreach (var message in _messageChannel.Reader.ReadAllAsync(stoppingToken))
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
var eventTelemetry = message as Microsoft.ApplicationInsights.DataContracts.EventTelemetry;
|
||||
|
||||
var level = "Information";
|
||||
var seqEvent = new Dictionary<string, object>
|
||||
{
|
||||
{ "@t", DateTime.UtcNow.ToString("o") },
|
||||
{ "@mt", eventTelemetry.Name },
|
||||
{ "@l", level } // "Information", "Warning", "Error", etc.
|
||||
};
|
||||
|
||||
foreach (var prop in eventTelemetry.Context.GlobalProperties)
|
||||
{
|
||||
seqEvent.Add(prop.Key, prop.Value);
|
||||
}
|
||||
|
||||
var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(seqEvent), Encoding.UTF8, "application/vnd.serilog.clef");
|
||||
|
||||
var key = "4XhWFtY4jJ0NBgohBAFF"; ;
|
||||
//Gt8hS9ClGNfOCAdswDlW
|
||||
var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"/ingest/clef?apiKey={key}");
|
||||
requestMessage.Content = content;
|
||||
|
||||
var response = await _httpClient.SendAsync(requestMessage, stoppingToken);
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
//if (!response.IsSuccessStatusCode)
|
||||
//{
|
||||
// _telemetryClient.TrackTrace($"HTTP kald fejlede med status {response.StatusCode}", Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Warning);
|
||||
// continue;
|
||||
//}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//_telemetryClient.TrackException(ex); this is disabled for now, we need to think about the channel structure first
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (ex is not OperationCanceledException)
|
||||
{
|
||||
_telemetryClient.TrackException(ex);
|
||||
throw;
|
||||
}
|
||||
|
||||
_telemetryClient.TrackTrace("Service shutdown started");
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_messageChannel.Dispose();
|
||||
await base.StopAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue