Skip to content

Commit

Permalink
feat: improve support for configuring http client (#125)
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 authored Aug 10, 2020
1 parent e9749ea commit 70c362b
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 13 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 for configuring 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,23 @@
#if !NETSTANDARD_2_0
// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.Configuration
{
/// <summary>
/// Represents a set of key/value application configuration properties.
/// </summary>
/// <remarks>
/// Polyfill for
/// <see href="https://docs.microsoft.com/dotnet/api/microsoft.extensions.configuration.iconfiguration">
/// Microsoft.Extensions.Configuration.IConfiguration</see>.
/// </remarks>
public interface IConfiguration
{
/// <summary>
/// Gets or sets a configuration value.
/// </summary>
/// <param name="key">The configuration key.</param>
/// <returns>The configuration value.</returns>
string this[string key] { get; set; }
}
}
#endif
37 changes: 32 additions & 5 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,12 @@ 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"/>. Parameter is either manually
/// specified when configuring the sink in source code or automatically passed in when
/// configuring the sink using
/// <see href="https://www.nuget.org/packages/Serilog.Settings.Configuration">Serilog.Settings.Configuration</see>.
/// </param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
public static LoggerConfiguration Http(
this LoggerSinkConfiguration sinkConfiguration,
Expand All @@ -74,7 +81,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 +91,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 All @@ -105,7 +114,8 @@ public static LoggerConfiguration DurableHttp(
ITextFormatter textFormatter = null,
IBatchFormatter batchFormatter = null,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IHttpClient httpClient = null)
IHttpClient httpClient = null,
IConfiguration configuration = null)
{
return DurableHttpUsingTimeRolledBuffers(
sinkConfiguration: sinkConfiguration,
Expand All @@ -119,7 +129,8 @@ public static LoggerConfiguration DurableHttp(
textFormatter: textFormatter,
batchFormatter: batchFormatter,
restrictedToMinimumLevel: restrictedToMinimumLevel,
httpClient: httpClient);
httpClient: httpClient,
configuration: configuration);
}

/// <summary>
Expand Down Expand Up @@ -175,6 +186,12 @@ 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"/>. Parameter is either manually
/// specified when configuring the sink in source code or automatically passed in when
/// configuring the sink using
/// <see href="https://www.nuget.org/packages/Serilog.Settings.Configuration">Serilog.Settings.Configuration</see>.
/// </param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
public static LoggerConfiguration DurableHttpUsingTimeRolledBuffers(
this LoggerSinkConfiguration sinkConfiguration,
Expand All @@ -188,7 +205,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 +215,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 +287,12 @@ 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"/>. Parameter is either manually
/// specified when configuring the sink in source code or automatically passed in when
/// configuring the sink using
/// <see href="https://www.nuget.org/packages/Serilog.Settings.Configuration">Serilog.Settings.Configuration</see>.
/// </param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
public static LoggerConfiguration DurableHttpUsingFileSizeRolledBuffers(
this LoggerSinkConfiguration sinkConfiguration,
Expand All @@ -281,7 +306,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 +316,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
20 changes: 17 additions & 3 deletions src/Serilog.Sinks.Http/Serilog.Sinks.Http.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<!-- GENERAL -->

<PropertyGroup>
<AssemblyName>Serilog.Sinks.Http</AssemblyName>
<Description>A Serilog sink sending log events over HTTP.</Description>
Expand Down Expand Up @@ -30,9 +32,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 +51,14 @@
<DefineConstants>$(DefineConstants);HRESULTS</DefineConstants>
</PropertyGroup>

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

<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>
7 changes: 7 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,12 @@ namespace Serilog.Sinks.Http
/// </summary>
public interface IHttpClient : IDisposable
{
/// <summary>
/// Configures the HTTP client.
/// </summary>
/// <param name="configuration">The application configuration properties.</param>
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ public DurableHttpSinkGivenAppSettingsShould()
Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();

Configuration = configuration;
}

protected override Logger Logger { get; }

protected override IConfiguration Configuration { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.Extensions.Configuration;
using Serilog.Core;
using Serilog.Sinks.Http.BatchFormatters;
using Serilog.Sinks.Http.TextFormatters;
Expand All @@ -11,6 +12,8 @@ public class DurableHttpSinkGivenCodeConfigurationShould : SinkFixture
public DurableHttpSinkGivenCodeConfigurationShould()
{
DeleteBufferFiles();

var configuration = new ConfigurationBuilder().Build();

#pragma warning disable CS0618 // Type or member is obsolete

Expand All @@ -23,12 +26,17 @@ public DurableHttpSinkGivenCodeConfigurationShould()
period: TimeSpan.FromMilliseconds(1),
textFormatter: new NormalRenderedTextFormatter(),
batchFormatter: new DefaultBatchFormatter(),
httpClient: new HttpClientMock())
httpClient: new HttpClientMock(),
configuration: configuration)
.CreateLogger();

#pragma warning restore CS0618 // Type or member is obsolete

Configuration = configuration;
}

protected override Logger Logger { get; }

protected override IConfiguration Configuration { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ public DurableHttpSinkUsingFileSizeRolledBuffersGivenAppSettingsShould()
Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();

Configuration = configuration;
}

protected override Logger Logger { get; }

protected override IConfiguration Configuration { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.Extensions.Configuration;
using Serilog.Core;
using Serilog.Sinks.Http.BatchFormatters;
using Serilog.Sinks.Http.TextFormatters;
Expand All @@ -12,6 +13,8 @@ public DurableHttpSinkUsingFileSizeRolledBuffersGivenCodeConfigurationShould()
{
DeleteBufferFiles();

var configuration = new ConfigurationBuilder().Build();

Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo
Expand All @@ -21,10 +24,15 @@ public DurableHttpSinkUsingFileSizeRolledBuffersGivenCodeConfigurationShould()
period: TimeSpan.FromMilliseconds(1),
textFormatter: new NormalRenderedTextFormatter(),
batchFormatter: new DefaultBatchFormatter(),
httpClient: new HttpClientMock())
httpClient: new HttpClientMock(),
configuration: configuration)
.CreateLogger();

Configuration = configuration;
}

protected override Logger Logger { get; }

protected override IConfiguration Configuration { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ public DurableHttpSinkUsingTimeRolledBuffersGivenAppSettingsShould()
Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();

Configuration = configuration;
}

protected override Logger Logger { get; }

protected override IConfiguration Configuration { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.Extensions.Configuration;
using Serilog.Core;
using Serilog.Sinks.Http.BatchFormatters;
using Serilog.Sinks.Http.TextFormatters;
Expand All @@ -12,6 +13,8 @@ public DurableHttpSinkUsingTimeRolledBuffersGivenCodeConfigurationShould()
{
DeleteBufferFiles();

var configuration = new ConfigurationBuilder().Build();

Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo
Expand All @@ -21,10 +24,15 @@ public DurableHttpSinkUsingTimeRolledBuffersGivenCodeConfigurationShould()
period: TimeSpan.FromMilliseconds(1),
textFormatter: new NormalRenderedTextFormatter(),
batchFormatter: new DefaultBatchFormatter(),
httpClient: new HttpClientMock())
httpClient: new HttpClientMock(),
configuration: configuration)
.CreateLogger();

Configuration = configuration;
}

protected override Logger Logger { get; }

protected override IConfiguration Configuration { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ public HttpSinkGivenAppSettingsShould()
Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();

Configuration = configuration;
}

protected override Logger Logger { get; }

protected override IConfiguration Configuration { get; }
}
}
Loading

0 comments on commit 70c362b

Please sign in to comment.