Skip to content

Commit

Permalink
Fixed "Content-Length" header on Raygun transmission
Browse files Browse the repository at this point in the history
  • Loading branch information
anfomin committed Mar 15, 2023
1 parent d19591f commit d0b6e54
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
11 changes: 10 additions & 1 deletion RaygunCore.Test/RaygunClientTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Net;
using System.Text;
using System.Text.Json;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using RaygunCore.Messages;
using RaygunCore.Services;
Expand Down Expand Up @@ -49,13 +51,20 @@ public async Task Index()
new MainMessageProvider(Options.Create(raygunOptions)),
new TestUserMessageProvider()
});
var raygunClient = new DefaultRaygunClient(httpClientFactory, raygunMessageBuilder, Enumerable.Empty<IRaygunValidator>(), Options.Create(raygunOptions));
var raygunClient = new DefaultRaygunClient(
NullLoggerFactory.Instance.CreateLogger<DefaultRaygunClient>(),
httpClientFactory,
raygunMessageBuilder,
Enumerable.Empty<IRaygunValidator>(),
Options.Create(raygunOptions)
);
await raygunClient.SendAsync(message, severity, tags);
server.Stop();

var result = await resultTask;
result.Method.Should().Be("POST");
result.Headers["X-ApiKey"].Should().Be(raygunOptions.ApiKey);
result.Headers["Content-Length"].Should().NotBeNull();
result.ContentType.Should().StartWith("application/json");
result.Content.Should().NotBeNullOrEmpty();
result.Content.Should().NotContain("\t");
Expand Down
2 changes: 1 addition & 1 deletion RaygunCore.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<RepositoryUrl>https://github.com/anfomin/rayguncore</RepositoryUrl>
<PackageProjectUrl>https://github.com/anfomin/rayguncore</PackageProjectUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<VersionPrefix>2.1.0</VersionPrefix>
<VersionPrefix>2.2.0</VersionPrefix>
<VersionSuffix Condition="'$(VERSION_SUFFIX)'!=''">$(VERSION_SUFFIX)</VersionSuffix>
<NoWarn>$(NoWarn);1573;1591</NoWarn>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
10 changes: 9 additions & 1 deletion RaygunCore/Services/DefaultRaygunClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Http.Json;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using RaygunCore.Messages;

Expand All @@ -9,17 +10,20 @@ namespace RaygunCore.Services;
/// </summary>
public class DefaultRaygunClient : IRaygunClient
{
readonly ILogger _logger;
readonly IHttpClientFactory _clientFactory;
readonly IRaygunMessageBuilder _messageBuilder;
readonly IEnumerable<IRaygunValidator> _validators;
readonly RaygunOptions _options;

public DefaultRaygunClient(
ILogger<DefaultRaygunClient> logger,
IHttpClientFactory clientFactory,
IRaygunMessageBuilder messageBuilder,
IEnumerable<IRaygunValidator> validators,
IOptions<RaygunOptions> options)
{
_logger = logger;
_clientFactory = clientFactory ?? throw new ArgumentNullException(nameof(clientFactory));
_messageBuilder = messageBuilder ?? throw new ArgumentNullException(nameof(messageBuilder));
_validators = validators ?? throw new ArgumentNullException(nameof(validators));;
Expand Down Expand Up @@ -77,13 +81,17 @@ protected async Task TransmitMessageAsync(RaygunMessage message)
{
try
{
var result = await CreateClient().PostAsync(_options.ApiEndpoint, JsonContent.Create(message));
var content = JsonContent.Create(message);
await content.LoadIntoBufferAsync();
var result = await CreateClient().PostAsync(_options.ApiEndpoint, content);
result.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
if (_options.ThrowOnError)
throw new RaygunException(ex);
else
_logger.LogError(ex, "Raygun transmission failed");
}
}
}

0 comments on commit d0b6e54

Please sign in to comment.