Working on BackgroundService for Seq Logging

This commit is contained in:
Janus C. H. Knudsen 2025-02-13 00:40:42 +01:00
parent 48578b216f
commit 7bcb7b0e66
9 changed files with 240 additions and 53 deletions

View file

@ -0,0 +1,42 @@
using System.Text;
using System.Text.Json;
namespace Core.Telemetry
{
public class SeqLogger
{
private readonly string _seqUrl;
private readonly string _apiKey; // Optional
HttpClient _httpClient;
public SeqLogger(HttpClient httpClient, string seqUrl, string apiKey = null)
{
_seqUrl = seqUrl;
_apiKey = apiKey;
_httpClient = httpClient;
}
public async Task LogToSeq(string message, string level, Dictionary<string, object> properties)
{
var seqEvent = new Dictionary<string, object>
{
{ "@t", DateTime.UtcNow.ToString("o") },
{ "@mt", message },
{ "@l", level } // "Information", "Warning", "Error", etc.
};
foreach (var prop in properties)
{
seqEvent.Add(prop.Key, prop.Value);
}
var content = new StringContent(
JsonSerializer.Serialize(seqEvent),
Encoding.UTF8,
"application/vnd.serilog.clef");
var response = await _httpClient.PostAsync("/ingest/clef?apiKey=Gt8hS9ClGNfOCAdswDlW", content);
response.EnsureSuccessStatusCode();
}
}
}