Skip to content

Commit

Permalink
feat: improve support for configuring http client
Browse files Browse the repository at this point in the history
Improve support to configure HTTP client when using
Serilog.Settings.Configuration.

Closes #49
Closes #123
  • Loading branch information
FantasticFiasco committed Aug 9, 2020
1 parent e9749ea commit 06d91b4
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ This project adheres to [Semantic Versioning](http://semver.org/) and is followi

## Unreleased

### :zap: Added

- [#49](https://github.com/FantasticFiasco/serilog-sinks-http/issues/49), [#123](https://github.com/FantasticFiasco/serilog-sinks-http/issues/123) [BREAKING CHANGE] Improve support to configure HTTP client when using [Serilog.Settings.Configuration](https://github.com/serilog/serilog-settings-configuration). See [Custom HTTP client in wiki](https://github.com/FantasticFiasco/serilog-sinks-http/wiki/Custom-HTTP-client) for more information. (discovered by [@brunorsantos](https://github.com/brunorsantos))

## [6.0.0] - 2020-05-13

### :zap: Added
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#if !NETSTANDARD_2_0
namespace Microsoft.Extensions.Configuration
{
/// <summary>
/// Polyfill for <see href="https://docs.microsoft.com/dotnet/api/microsoft.extensions.configuration.iconfiguration">
/// Microsoft.Extensions.Configuration.IConfiguration</see> in .NET Framework.
/// </summary>
public interface IConfiguration
{
}
}
#endif
22 changes: 19 additions & 3 deletions src/Serilog.Sinks.Http/LoggerSinkConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System;
using System.ComponentModel;
using System.Net.Http;
using Microsoft.Extensions.Configuration;
using Serilog.Configuration;
using Serilog.Events;
using Serilog.Formatting;
Expand Down Expand Up @@ -64,6 +65,9 @@ public static class LoggerSinkConfigurationExtensions
/// A custom <see cref="IHttpClient"/> implementation. Default value is
/// <see cref="HttpClient"/>.
/// </param>
/// <param name="configuration">
/// Configuration passed to <paramref name="httpClient"/> in .NET Standard 2.0 and above.
/// </param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
public static LoggerConfiguration Http(
this LoggerSinkConfiguration sinkConfiguration,
Expand All @@ -74,7 +78,8 @@ public static LoggerConfiguration Http(
ITextFormatter textFormatter = null,
IBatchFormatter batchFormatter = null,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IHttpClient httpClient = null)
IHttpClient httpClient = null,
IConfiguration configuration = null)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));

Expand All @@ -83,6 +88,7 @@ public static LoggerConfiguration Http(
textFormatter = textFormatter ?? new NormalRenderedTextFormatter();
batchFormatter = batchFormatter ?? new DefaultBatchFormatter();
httpClient = httpClient ?? new DefaultHttpClient();
httpClient.Configure(configuration);

var sink = queueLimit != null
? new HttpSink(requestUri, batchPostingLimit, queueLimit.Value, period.Value, textFormatter, batchFormatter, httpClient)
Expand Down Expand Up @@ -175,6 +181,9 @@ public static LoggerConfiguration DurableHttp(
/// A custom <see cref="IHttpClient"/> implementation. Default value is
/// <see cref="HttpClient"/>.
/// </param>
/// <param name="configuration">
/// Configuration passed to <paramref name="httpClient"/> in .NET Standard 2.0 and above.
/// </param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
public static LoggerConfiguration DurableHttpUsingTimeRolledBuffers(
this LoggerSinkConfiguration sinkConfiguration,
Expand All @@ -188,7 +197,8 @@ public static LoggerConfiguration DurableHttpUsingTimeRolledBuffers(
ITextFormatter textFormatter = null,
IBatchFormatter batchFormatter = null,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IHttpClient httpClient = null)
IHttpClient httpClient = null,
IConfiguration configuration = null)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));

Expand All @@ -197,6 +207,7 @@ public static LoggerConfiguration DurableHttpUsingTimeRolledBuffers(
textFormatter = textFormatter ?? new NormalRenderedTextFormatter();
batchFormatter = batchFormatter ?? new DefaultBatchFormatter();
httpClient = httpClient ?? new DefaultHttpClient();
httpClient.Configure(configuration);

var sink = new TimeRolledDurableHttpSink(
requestUri: requestUri,
Expand Down Expand Up @@ -268,6 +279,9 @@ public static LoggerConfiguration DurableHttpUsingTimeRolledBuffers(
/// A custom <see cref="IHttpClient"/> implementation. Default value is
/// <see cref="HttpClient"/>.
/// </param>
/// <param name="configuration">
/// Configuration passed to <paramref name="httpClient"/> in .NET Standard 2.0 and above.
/// </param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
public static LoggerConfiguration DurableHttpUsingFileSizeRolledBuffers(
this LoggerSinkConfiguration sinkConfiguration,
Expand All @@ -281,7 +295,8 @@ public static LoggerConfiguration DurableHttpUsingFileSizeRolledBuffers(
ITextFormatter textFormatter = null,
IBatchFormatter batchFormatter = null,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IHttpClient httpClient = null)
IHttpClient httpClient = null,
IConfiguration configuration = null)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));

Expand All @@ -290,6 +305,7 @@ public static LoggerConfiguration DurableHttpUsingFileSizeRolledBuffers(
textFormatter = textFormatter ?? new NormalRenderedTextFormatter();
batchFormatter = batchFormatter ?? new DefaultBatchFormatter();
httpClient = httpClient ?? new DefaultHttpClient();
httpClient.Configure(configuration);

var sink = new FileSizeRolledDurableHttpSink(
requestUri: requestUri,
Expand Down
21 changes: 18 additions & 3 deletions src/Serilog.Sinks.Http/Serilog.Sinks.Http.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<!-- GENERAL -->

<PropertyGroup>
<AssemblyName>Serilog.Sinks.Http</AssemblyName>
<Description>A Serilog sink sending log events over HTTP.</Description>
<TargetFrameworks>net45;net461;netstandard1.3;netstandard2.0</TargetFrameworks>
<!--<TargetFrameworks>netstandard2.0</TargetFrameworks>-->
<RootNamespace>Serilog</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<!-- Strong naming -->
Expand All @@ -30,9 +33,15 @@
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="2.3.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
<!-- SourceLink -->
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All"/>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<None Include="assets\serilog-sink-nuget.png" Pack="true" PackagePath="\" />
</ItemGroup>

<!-- .NET FRAMEWORK -->

<ItemGroup Condition="'$(TargetFramework)' == 'net45' Or '$(TargetFramework)' == 'net461'">
<Reference Include="System" />
<Reference Include="System.Net.Http" />
Expand All @@ -43,8 +52,14 @@
<DefineConstants>$(DefineConstants);HRESULTS</DefineConstants>
</PropertyGroup>

<ItemGroup>
<None Include="assets\serilog-sink-nuget.png" Pack="true" PackagePath="\"/>
<!-- .NET CORE -->

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.6" />
</ItemGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<DefineConstants>$(DefineConstants);NETSTANDARD_2_0</DefineConstants>
</PropertyGroup>

</Project>
3 changes: 3 additions & 0 deletions src/Serilog.Sinks.Http/Sinks/Http/IHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

namespace Serilog.Sinks.Http
{
Expand All @@ -23,6 +24,8 @@ namespace Serilog.Sinks.Http
/// </summary>
public interface IHttpClient : IDisposable
{
void Configure(IConfiguration configuration);

/// <summary>
/// Sends a POST request to the specified Uri as an asynchronous operation.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

namespace Serilog.Sinks.Http.Private.Network
{
Expand All @@ -24,6 +25,11 @@ public class DefaultHttpClient : IHttpClient
public DefaultHttpClient() =>
httpClient = new HttpClient();

public void Configure(IConfiguration configuration)
{
// The default HTTP client requires no configuration
}

public Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content) =>
httpClient.PostAsync(requestUri, content);

Expand Down
6 changes: 6 additions & 0 deletions test/Serilog.Sinks.HttpTests/Support/HttpClientMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Serilog.Sinks.Http;
using Serilog.Sinks.Http.BatchFormatters;
Expand Down Expand Up @@ -59,6 +60,11 @@ public async Task WaitAsync(int expectedLogEventCount)
public void SimulateNetworkFailure() =>
simulateNetworkFailure = true;


public void Configure(IConfiguration configuration)
{
}

public async Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content)
{
if (simulateNetworkFailure)
Expand Down

0 comments on commit 06d91b4

Please sign in to comment.