From adc0adfb5039c39efa73c4d8864a586e238a7516 Mon Sep 17 00:00:00 2001 From: Corentin Altepe Date: Sat, 9 Mar 2024 10:38:54 +0100 Subject: [PATCH] Attempt at adding support for .NET Framework 4.8 This is only an idea of adding support for .NET Framework 4.8, following issue #114. This proof of concept is not ready to be merged. * The added dependencies must be conditioned to only .NET Framework 4.8. * The Json serliazing logic must be conditioned to .NET Framework 4.8 to avoid performance degradation for .NET 6+ users. * Documentation and build, test, publish pipelines must be updated accordingly --- src/NLog.Loki/HttpLokiTransport.cs | 24 +++++++++++++++--------- src/NLog.Loki/LokiTarget.cs | 1 - src/NLog.Loki/NLog.Loki.csproj | 5 ++++- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/NLog.Loki/HttpLokiTransport.cs b/src/NLog.Loki/HttpLokiTransport.cs index c62bac1..d36d356 100644 --- a/src/NLog.Loki/HttpLokiTransport.cs +++ b/src/NLog.Loki/HttpLokiTransport.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; +using System.IO; using System.IO.Compression; using System.Net.Http; -using System.Net.Http.Json; using System.Text.Json; using System.Threading.Tasks; using NLog.Common; @@ -25,7 +25,7 @@ public HttpLokiTransport( { _lokiHttpClient = lokiHttpClient; _gzipLevel = gzipLevel; - + _jsonOptions = new JsonSerializerOptions(); _jsonOptions.Converters.Add(new LokiEventsSerializer(orderWrites)); _jsonOptions.Converters.Add(new LokiEventSerializer()); @@ -33,14 +33,14 @@ public HttpLokiTransport( public async Task WriteLogEventsAsync(IEnumerable lokiEvents) { - using var jsonStreamContent = CreateContent(lokiEvents); + using var jsonStreamContent = await CreateContent(lokiEvents); using var response = await _lokiHttpClient.PostAsync("loki/api/v1/push", jsonStreamContent).ConfigureAwait(false); await ValidateHttpResponse(response).ConfigureAwait(false); } public async Task WriteLogEventsAsync(LokiEvent lokiEvent) { - using var jsonStreamContent = CreateContent(lokiEvent); + using var jsonStreamContent = await CreateContent(lokiEvent); using var response = await _lokiHttpClient.PostAsync("loki/api/v1/push", jsonStreamContent).ConfigureAwait(false); await ValidateHttpResponse(response).ConfigureAwait(false); } @@ -49,19 +49,25 @@ public async Task WriteLogEventsAsync(LokiEvent lokiEvent) /// Prepares the HttpContent for the loki event(s). /// If gzip compression is enabled, prepares a gzip stream with the appropriate headers. /// - private HttpContent CreateContent(T lokiEvent) + private async Task CreateContent(T lokiEvent) { - var jsonContent = JsonContent.Create(lokiEvent, options: _jsonOptions); + //var jsonContent = JsonContent.Create(lokiEvent, options: _jsonOptions); + + using var memStream = new MemoryStream(); + await JsonSerializer.SerializeAsync(memStream, lokiEvent, options: _jsonOptions); + memStream.Position = 0; + using var con = new StreamContent(memStream); + con.Headers.ContentType.MediaType = "application/json"; // Some old loki versions support only 'application/json', // not 'application/json; charset=utf-8' content-Type header - jsonContent.Headers.ContentType.CharSet = string.Empty; + con.Headers.ContentType.CharSet = string.Empty; // If no compression required if(_gzipLevel == CompressionLevel.NoCompression) - return jsonContent; + return con; - return new CompressedContent(jsonContent, _gzipLevel); + return new CompressedContent(con, _gzipLevel); } private static async ValueTask ValidateHttpResponse(HttpResponseMessage response) diff --git a/src/NLog.Loki/LokiTarget.cs b/src/NLog.Loki/LokiTarget.cs index 310747d..577ae5b 100644 --- a/src/NLog.Loki/LokiTarget.cs +++ b/src/NLog.Loki/LokiTarget.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO.Compression; -using System.Linq; using System.Net; using System.Net.Http; using System.Text; diff --git a/src/NLog.Loki/NLog.Loki.csproj b/src/NLog.Loki/NLog.Loki.csproj index 15cfb43..e414e93 100644 --- a/src/NLog.Loki/NLog.Loki.csproj +++ b/src/NLog.Loki/NLog.Loki.csproj @@ -1,7 +1,7 @@ - netstandard2.0;net6.0;net7.0;net8.0 + net48;netstandard2.0;net6.0;net7.0;net8.0 10.0 NLog.Targets.Loki Anton Gogolev, Corentin Altepe @@ -27,7 +27,10 @@ + + +