Skip to content

Commit

Permalink
Introduced MaxConnectionsPerServer property (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored and AlanBarber committed Mar 19, 2019
1 parent 86a15d8 commit 6d84d4e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
13 changes: 9 additions & 4 deletions src/NLog.Targets.Splunk/NLog.Targets.Splunk.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net45;net472;netstandard2.0</TargetFrameworks>
<Title>NLog.Targets.Splunk</Title>
<Description>A NLog target for Splunk's Http Event Collector (HEC) Sender</Description>
<Authors>Alan Barber</Authors>
Expand Down Expand Up @@ -41,16 +41,21 @@ Verison 0.0.1
<!--.NET Standard 2.0 config -->
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<AssemblyTitle>NLog.Targets.Splunk .NET Standard 2.0</AssemblyTitle>
<DefineConstants>NETCORE;NETSTANDARD;NETSTANDARD2_0</DefineConstants>
<DefineConstants>NETCORE;NETSTANDARD</DefineConstants>
</PropertyGroup>

<!-- .NET 4.5 config -->
<PropertyGroup Condition=" '$(TargetFramework)'=='net45' ">
<AssemblyTitle>NLog.Targets.Splunk .NET 4.5</AssemblyTitle>
<DefineConstants>NET45;NETFULL</DefineConstants>
<DefineConstants>NETFULL</DefineConstants>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<PropertyGroup Condition=" '$(TargetFramework)'=='net472' ">
<AssemblyTitle>NLog.Targets.Splunk .NET 4.7.2</AssemblyTitle>
<DefineConstants>NETFULL</DefineConstants>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' or '$(TargetFramework)' == 'net472' ">
<Reference Include="mscorlib" />
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ public HttpEventCollectorSender(
int batchSizeBytes,
int batchSizeCount,
bool ignoreSslErrors,
int maxConnectionsPerServer,
HttpEventCollectorMiddleware middleware,
HttpEventCollectorFormatter formatter = null)
{
Expand Down Expand Up @@ -215,7 +216,7 @@ public HttpEventCollectorSender(
// setup HTTP client
try
{
var httpMessageHandler = ignoreSslErrors ? BuildHttpMessageHandler(ignoreSslErrors) : null;
var httpMessageHandler = ignoreSslErrors ? BuildHttpMessageHandler(ignoreSslErrors, maxConnectionsPerServer) : null;
httpClient = httpMessageHandler != null ? new HttpClient(httpMessageHandler) : new HttpClient();
}
catch
Expand All @@ -239,9 +240,10 @@ public HttpEventCollectorSender(
/// </summary>
/// <param name="ignoreSslErrors">if set to <c>true</c> [ignore SSL errors].</param>
/// <returns></returns>
private HttpMessageHandler BuildHttpMessageHandler(bool ignoreSslErrors)
private HttpMessageHandler BuildHttpMessageHandler(bool ignoreSslErrors, int maxConnectionsPerServer)
{
#if NET45

var httpMessageHandler = new WebRequestHandler();
if (ignoreSslErrors)
{
Expand All @@ -253,6 +255,10 @@ private HttpMessageHandler BuildHttpMessageHandler(bool ignoreSslErrors)
{
httpMessageHandler.ServerCertificateCustomValidationCallback = (msg, cert, chain, errors) => IgnoreServerCertificateCallback(msg, cert, chain, errors);
}
if (maxConnectionsPerServer > 0)
{
httpMessageHandler.MaxConnectionsPerServer = maxConnectionsPerServer;
}
#endif
return httpMessageHandler;
}
Expand Down
9 changes: 8 additions & 1 deletion src/NLog.Targets.Splunk/SplunkHttpEventCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ public sealed class SplunkHttpEventCollector : TargetWithContext
/// <value>
/// <c>true</c> if [ignore SSL errors]; otherwise, <c>false</c>.
/// </value>
public bool IgnoreSslErrors { get; set; }
public bool IgnoreSslErrors { get; set; }

/// <summary>
/// Gets or sets the maximum number of concurrent connections (per server endpoint) allowed when making requests
/// </summary>
/// <value>0 = Use default limit. Default = 10</value>
public int MaxConnectionsPerServer { get; set; } = 10;

/// <summary>
/// Configuration of additional properties to include with each LogEvent (Ex. ${logger}, ${machinename}, ${threadid} etc.)
Expand Down Expand Up @@ -136,6 +142,7 @@ protected override void InitializeTarget()
BatchSizeBytes, // BatchSizeBytes - Set to 0 to disable
BatchSizeCount, // BatchSizeCount - Set to 0 to disable
IgnoreSslErrors, // Enable Ssl Error ignore for self singed certs *BOOL*
MaxConnectionsPerServer,
new HttpEventCollectorResendMiddleware(RetriesOnError).Plugin // Resend Middleware with retry
);
_hecSender.OnError += (e) => { InternalLogger.Error(e, "SplunkHttpEventCollector(Name={0}): Failed to send LogEvents", Name); };
Expand Down

0 comments on commit 6d84d4e

Please sign in to comment.