diff --git a/RaygunCore.Test/RaygunClientTest.cs b/RaygunCore.Test/RaygunClientTest.cs index a2c9cdf..75c06dd 100644 --- a/RaygunCore.Test/RaygunClientTest.cs +++ b/RaygunCore.Test/RaygunClientTest.cs @@ -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; @@ -49,13 +51,20 @@ public async Task Index() new MainMessageProvider(Options.Create(raygunOptions)), new TestUserMessageProvider() }); - var raygunClient = new DefaultRaygunClient(httpClientFactory, raygunMessageBuilder, Enumerable.Empty(), Options.Create(raygunOptions)); + var raygunClient = new DefaultRaygunClient( + NullLoggerFactory.Instance.CreateLogger(), + httpClientFactory, + raygunMessageBuilder, + Enumerable.Empty(), + 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"); diff --git a/RaygunCore.props b/RaygunCore.props index 14278c4..252f61c 100644 --- a/RaygunCore.props +++ b/RaygunCore.props @@ -6,7 +6,7 @@ https://github.com/anfomin/rayguncore https://github.com/anfomin/rayguncore LICENSE - 2.1.0 + 2.2.0 $(VERSION_SUFFIX) $(NoWarn);1573;1591 true diff --git a/RaygunCore/Services/DefaultRaygunClient.cs b/RaygunCore/Services/DefaultRaygunClient.cs index a008faa..5671ca5 100644 --- a/RaygunCore/Services/DefaultRaygunClient.cs +++ b/RaygunCore/Services/DefaultRaygunClient.cs @@ -1,4 +1,5 @@ using System.Net.Http.Json; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using RaygunCore.Messages; @@ -9,17 +10,20 @@ namespace RaygunCore.Services; /// public class DefaultRaygunClient : IRaygunClient { + readonly ILogger _logger; readonly IHttpClientFactory _clientFactory; readonly IRaygunMessageBuilder _messageBuilder; readonly IEnumerable _validators; readonly RaygunOptions _options; public DefaultRaygunClient( + ILogger logger, IHttpClientFactory clientFactory, IRaygunMessageBuilder messageBuilder, IEnumerable validators, IOptions options) { + _logger = logger; _clientFactory = clientFactory ?? throw new ArgumentNullException(nameof(clientFactory)); _messageBuilder = messageBuilder ?? throw new ArgumentNullException(nameof(messageBuilder)); _validators = validators ?? throw new ArgumentNullException(nameof(validators));; @@ -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"); } } } \ No newline at end of file