-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attempt at adding support for .NET Framework 4.8 #116
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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,22 +25,22 @@ public HttpLokiTransport( | |
{ | ||
_lokiHttpClient = lokiHttpClient; | ||
_gzipLevel = gzipLevel; | ||
|
||
_jsonOptions = new JsonSerializerOptions(); | ||
_jsonOptions.Converters.Add(new LokiEventsSerializer(orderWrites)); | ||
_jsonOptions.Converters.Add(new LokiEventSerializer()); | ||
} | ||
|
||
public async Task WriteLogEventsAsync(IEnumerable<LokiEvent> 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. | ||
/// </summary> | ||
private HttpContent CreateContent<T>(T lokiEvent) | ||
private async Task<HttpContent> CreateContent<T>(T lokiEvent) | ||
{ | ||
var jsonContent = JsonContent.Create(lokiEvent, options: _jsonOptions); | ||
//var jsonContent = JsonContent.Create(lokiEvent, options: _jsonOptions); | ||
|
||
using var memStream = new MemoryStream(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a pooled memory stream to limit allocations. Though, I wonder if we really need to preallocate the json before sending the HTTP request. Is there a way to stream the json serialization and compression without having to pre-allocate the json? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't look like it's possible to write to the stream without pre-allocating the entirety of the json stream. The pooled memory stream should still come handy. |
||
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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks> | ||
<TargetFrameworks>net48;netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks> | ||
<LangVersion>10.0</LangVersion> | ||
<PackageId>NLog.Targets.Loki</PackageId> | ||
<Authors>Anton Gogolev, Corentin Altepe</Authors> | ||
|
@@ -27,7 +27,10 @@ | |
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Net.Http" Version="2.2.29" /> | ||
<PackageReference Include="NLog" Version="5.2.8" /> | ||
<PackageReference Include="System.Net.Http" Version="4.3.4" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Condition these new packages to .NET Framework 4.8 only |
||
<PackageReference Include="System.Text.Json" Version="8.0.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'"> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create a different, async method for .NET Framework 4.8.
.NET 6+ will remain sync.