Skip to content
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

Add option to pass a HttpMessageHandler to the HttpClient #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/SplunkLogger/LoggerFactoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Splunk.Providers;
using Splunk.Configurations;
using System.Collections.Generic;
using System.Net.Http;

namespace Splunk
{
Expand All @@ -18,11 +19,12 @@ public static class LoggerFactoryExtensions
/// <param name="loggerFactory">Logger factory.</param>
/// <param name="configuration">Configuration.</param>
/// <param name="formatter">Custom text formatter.</param>
public static ILoggerFactory AddHECRawSplunkLogger(this ILoggerFactory loggerFactory, SplunkLoggerConfiguration configuration, ILoggerFormatter formatter = null)
/// <param name="httpMessageHandler">The HTTP handler stack to use for sending requests.</param>
public static ILoggerFactory AddHECRawSplunkLogger(this ILoggerFactory loggerFactory, SplunkLoggerConfiguration configuration, ILoggerFormatter formatter = null, HttpMessageHandler httpMessageHandler = null)
{
if (formatter == null)
formatter = DefaultLoggerFormatter;
loggerFactory.AddProvider(new SplunkHECRawLoggerProvider(configuration, formatter));
loggerFactory.AddProvider(new SplunkHECRawLoggerProvider(configuration, formatter, httpMessageHandler));
return loggerFactory;
}

Expand All @@ -32,11 +34,12 @@ public static ILoggerFactory AddHECRawSplunkLogger(this ILoggerFactory loggerFac
/// <param name="loggerFactory">Logger factory.</param>
/// <param name="configuration">Configuration.</param>
/// <param name="formatter">Custom text formatter.</param>
public static ILoggerFactory AddHECJsonSplunkLogger(this ILoggerFactory loggerFactory, SplunkLoggerConfiguration configuration, ILoggerFormatter formatter = null)
/// <param name="httpMessageHandler">The HTTP handler stack to use for sending requests.</param>
public static ILoggerFactory AddHECJsonSplunkLogger(this ILoggerFactory loggerFactory, SplunkLoggerConfiguration configuration, ILoggerFormatter formatter = null, HttpMessageHandler httpMessageHandler = null)
{
if (formatter == null)
formatter = DefaultLoggerFormatter;
loggerFactory.AddProvider(new SplunkHECJsonLoggerProvider(configuration, formatter));
loggerFactory.AddProvider(new SplunkHECJsonLoggerProvider(configuration, formatter, httpMessageHandler));
return loggerFactory;
}

Expand Down
8 changes: 4 additions & 4 deletions src/SplunkLogger/Providers/SplunkHECBaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public abstract class SplunkHECBaseProvider : ILoggerProvider
protected ILogger loggerInstance;
protected HttpClient httpClient;

public SplunkHECBaseProvider(SplunkLoggerConfiguration configuration, string endPointCustomization)
public SplunkHECBaseProvider(SplunkLoggerConfiguration configuration, string endPointCustomization, HttpMessageHandler httpMessageHandler = null)
{
SetupHttpClient(configuration, endPointCustomization);
SetupHttpClient(configuration, endPointCustomization, httpMessageHandler);
}

/// <summary>
Expand All @@ -41,9 +41,9 @@ public SplunkHECBaseProvider(SplunkLoggerConfiguration configuration, string end
/// that the <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> was occupying.</remarks>
public abstract void Dispose();

void SetupHttpClient(SplunkLoggerConfiguration configuration, string endPointCustomization)
void SetupHttpClient(SplunkLoggerConfiguration configuration, string endPointCustomization, HttpMessageHandler httpMessageHandler)
{
httpClient = new HttpClient
httpClient = new HttpClient(httpMessageHandler ?? new HttpClientHandler())
{
BaseAddress = GetSplunkCollectorUrl(configuration, endPointCustomization)
};
Expand Down
6 changes: 4 additions & 2 deletions src/SplunkLogger/Providers/SplunkHECJsonLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Splunk.Providers
/// <summary>
/// This class is used to provide a Splunk HEC Json logger for each categoryName.
/// </summary>
[ProviderAlias("Splunk")]
public class SplunkHECJsonLoggerProvider : SplunkHECBaseProvider, ILoggerProvider
{
readonly BatchManager batchManager;
Expand All @@ -24,8 +25,9 @@ public class SplunkHECJsonLoggerProvider : SplunkHECBaseProvider, ILoggerProvide
/// </summary>
/// <param name="configuration">Splunk configuration instance for HEC.</param>
/// <param name="loggerFormatter">Formatter instance.</param>
public SplunkHECJsonLoggerProvider(SplunkLoggerConfiguration configuration, ILoggerFormatter loggerFormatter = null)
: base(configuration, "event")
/// <param name="httpMessageHandler">The HTTP handler stack to use for sending requests.</param>
public SplunkHECJsonLoggerProvider(SplunkLoggerConfiguration configuration, ILoggerFormatter loggerFormatter = null, HttpMessageHandler httpMessageHandler = null)
: base(configuration, "event", httpMessageHandler)
{
this.loggerFormatter = loggerFormatter;
loggers = new ConcurrentDictionary<string, ILogger>();
Expand Down
6 changes: 4 additions & 2 deletions src/SplunkLogger/Providers/SplunkHECRawLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Splunk.Providers
/// <summary>
/// This class is used to provide a Splunk HEC Raw logger for each categoryName.
/// </summary>
[ProviderAlias("Splunk")]
public class SplunkHECRawLoggerProvider : SplunkHECBaseProvider, ILoggerProvider
{
readonly BatchManager batchManager;
Expand All @@ -23,8 +24,9 @@ public class SplunkHECRawLoggerProvider : SplunkHECBaseProvider, ILoggerProvider
/// </summary>
/// <param name="configuration">Splunk configuration instance for HEC.</param>
/// <param name="loggerFormatter">Formatter instance.</param>
public SplunkHECRawLoggerProvider(SplunkLoggerConfiguration configuration, ILoggerFormatter loggerFormatter = null)
:base(configuration, "raw")
/// <param name="httpMessageHandler">The HTTP handler stack to use for sending requests.</param>
public SplunkHECRawLoggerProvider(SplunkLoggerConfiguration configuration, ILoggerFormatter loggerFormatter = null, HttpMessageHandler httpMessageHandler = null)
:base(configuration, "raw", httpMessageHandler)
{
this.loggerFormatter = loggerFormatter;
loggers = new ConcurrentDictionary<string, ILogger>();
Expand Down
1 change: 1 addition & 0 deletions src/SplunkLogger/Providers/SplunkTcpLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Splunk.Providers
/// <summary>
/// This class is used to provide a Splunk Socket Tcp logger for each categoryName.
/// </summary>
[ProviderAlias("Splunk")]
public class SplunkTcpLoggerProvider : ILoggerProvider
{
readonly ILoggerFormatter loggerFormatter;
Expand Down
1 change: 1 addition & 0 deletions src/SplunkLogger/Providers/SplunkUdpLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Splunk.Providers
/// <summary>
/// This class is used to provide a Splunk Socket Udp logger for each categoryName.
/// </summary>
[ProviderAlias("Splunk")]
public class SplunkUdpLoggerProvider : ILoggerProvider
{
readonly ILoggerFormatter loggerFormatter;
Expand Down