Skip to content

Commit b6356ad

Browse files
committed
Created base HEC provider to simplify http instance setup for JSON and HEC providers
#37
1 parent 1595234 commit b6356ad

File tree

3 files changed

+97
-50
lines changed

3 files changed

+97
-50
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Net.Http.Headers;
4+
using Microsoft.Extensions.Logging;
5+
using Splunk.Configurations;
6+
7+
namespace Splunk.Providers
8+
{
9+
/// <summary>
10+
/// Splunk HECB ase provider.
11+
/// </summary>
12+
public abstract class SplunkHECBaseProvider : ILoggerProvider
13+
{
14+
protected HttpClient httpClient;
15+
16+
/// <summary>
17+
/// Get a <see cref="T:Splunk.Loggers.HECJsonLogger"/> instance to the category name provided.
18+
/// </summary>
19+
/// <returns><see cref="T:Splunk.Loggers.HECJsonLogger"/> instance.</returns>
20+
/// <param name="categoryName">Category name.</param>
21+
public abstract ILogger CreateLogger(string categoryName);
22+
23+
/// <summary>
24+
/// Releases all resource used by the <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> object.
25+
/// </summary>
26+
/// <remarks>Call <see cref="Dispose"/> when you are finished using the
27+
/// <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/>. The <see cref="Dispose"/> method leaves the
28+
/// <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> in an unusable state. After calling
29+
/// <see cref="Dispose"/>, you must release all references to the
30+
/// <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> so the garbage collector can reclaim the memory
31+
/// that the <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> was occupying.</remarks>
32+
public abstract void Dispose();
33+
34+
/// <summary>
35+
/// Create a <see cref="T:Splunk.Loggers.HECJsonLogger"/> instance to the category name provided.
36+
/// </summary>
37+
/// <returns>The logger instance.</returns>
38+
/// <param name="categoryName">Category name.</param>
39+
public abstract ILogger CreateLoggerInstance(string categoryName);
40+
41+
protected void SetupHttpClient(SplunkLoggerConfiguration configuration, string endPointCustomization)
42+
{
43+
httpClient = new HttpClient
44+
{
45+
BaseAddress = GetSplunkCollectorUrl(configuration, endPointCustomization),
46+
Timeout = TimeSpan.FromMilliseconds(configuration.HecConfiguration.DefaultTimeoutInMiliseconds)
47+
};
48+
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Splunk", configuration.HecConfiguration.Token);
49+
if (configuration.HecConfiguration.ChannelIdType == HECConfiguration.ChannelIdOption.RequestHeader)
50+
httpClient.DefaultRequestHeaders.Add("x-splunk-request-channel", Guid.NewGuid().ToString());
51+
}
52+
53+
Uri GetSplunkCollectorUrl(SplunkLoggerConfiguration configuration, string endPointCustomization)
54+
{
55+
var splunkCollectorUrl = configuration.HecConfiguration.SplunkCollectorUrl;
56+
if (!splunkCollectorUrl.EndsWith("/", StringComparison.InvariantCulture))
57+
splunkCollectorUrl = splunkCollectorUrl + "/";
58+
59+
if(!string.IsNullOrWhiteSpace(endPointCustomization))
60+
splunkCollectorUrl = splunkCollectorUrl + endPointCustomization;
61+
62+
if (configuration.HecConfiguration.ChannelIdType == HECConfiguration.ChannelIdOption.QueryString)
63+
splunkCollectorUrl = splunkCollectorUrl + "?channel=" + Guid.NewGuid().ToString();
64+
65+
return new Uri(splunkCollectorUrl);
66+
}
67+
}
68+
}

src/SplunkLogger/Providers/SplunkHECJsonLoggerProvider.cs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ namespace Splunk.Providers
1616
/// <summary>
1717
/// This class is used to provide a Splunk HEC Json logger for each categoryName.
1818
/// </summary>
19-
public class SplunkHECJsonLoggerProvider : ILoggerProvider
19+
public class SplunkHECJsonLoggerProvider : SplunkHECBaseProvider, ILoggerProvider
2020
{
2121
readonly BatchManager batchController;
22-
readonly HttpClient httpClient;
2322
readonly LogLevel threshold;
2423
readonly ILoggerFormatter loggerFormatter;
2524
readonly ConcurrentDictionary<string, ILogger> loggers;
@@ -37,34 +36,17 @@ public SplunkHECJsonLoggerProvider(SplunkLoggerConfiguration configuration, ILog
3736

3837
this.loggerFormatter = loggerFormatter;
3938

40-
httpClient = new HttpClient();
41-
42-
var splunkCollectorUrl = configuration.HecConfiguration.SplunkCollectorUrl;
43-
if (!splunkCollectorUrl.EndsWith("/", StringComparison.InvariantCulture))
44-
splunkCollectorUrl += "/";
45-
46-
var baseAddress = new Uri(splunkCollectorUrl + "event");
47-
httpClient.BaseAddress = baseAddress;
48-
49-
httpClient.Timeout = TimeSpan.FromMilliseconds(configuration.HecConfiguration.DefaultTimeoutInMiliseconds);
50-
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Splunk", configuration.HecConfiguration.Token);
51-
if (configuration.HecConfiguration.ChannelIdType == HECConfiguration.ChannelIdOption.RequestHeader)
52-
httpClient.DefaultRequestHeaders.Add("x-splunk-request-channel", Guid.NewGuid().ToString());
39+
SetupHttpClient(configuration, "event");
5340

5441
batchController = new BatchManager(configuration.HecConfiguration.BatchSizeCount, configuration.HecConfiguration.BatchIntervalInMiliseconds, Emit);
5542
}
5643

57-
ILogger CreateLoggerInstance(string categoryName)
58-
{
59-
return new HECJsonLogger(categoryName, threshold, httpClient, batchController, loggerFormatter);
60-
}
61-
6244
/// <summary>
63-
/// Create a <see cref="T:Splunk.Loggers.HECJsonLogger"/> instance to the category name provided.
45+
/// Get a <see cref="T:Splunk.Loggers.HECJsonLogger"/> instance to the category name provided.
6446
/// </summary>
6547
/// <returns><see cref="T:Splunk.Loggers.HECJsonLogger"/> instance.</returns>
6648
/// <param name="categoryName">Category name.</param>
67-
public ILogger CreateLogger(string categoryName)
49+
public override ILogger CreateLogger(string categoryName)
6850
{
6951
return loggers.GetOrAdd(categoryName, CreateLoggerInstance(categoryName));
7052
}
@@ -78,11 +60,21 @@ public ILogger CreateLogger(string categoryName)
7860
/// <see cref="Dispose"/>, you must release all references to the
7961
/// <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> so the garbage collector can reclaim the memory
8062
/// that the <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> was occupying.</remarks>
81-
public void Dispose()
63+
public override void Dispose()
8264
{
8365
loggers.Clear();
8466
}
8567

68+
/// <summary>
69+
/// Create a <see cref="T:Splunk.Loggers.HECJsonLogger"/> instance to the category name provided.
70+
/// </summary>
71+
/// <returns>The logger instance.</returns>
72+
/// <param name="categoryName">Category name.</param>
73+
public override ILogger CreateLoggerInstance(string categoryName)
74+
{
75+
return new HECJsonLogger(categoryName, threshold, httpClient, batchController, loggerFormatter);
76+
}
77+
8678
/// <summary>
8779
/// Method used to emit batched events.
8880
/// </summary>

src/SplunkLogger/Providers/SplunkHECRawLoggerProvider.cs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Diagnostics;
55
using System.Linq;
66
using System.Net.Http;
7-
using System.Net.Http.Headers;
87
using Microsoft.Extensions.Logging;
98
using Splunk.Configurations;
109
using Splunk.Loggers;
@@ -14,10 +13,9 @@ namespace Splunk.Providers
1413
/// <summary>
1514
/// This class is used to provide a Splunk HEC Raw logger for each categoryName.
1615
/// </summary>
17-
public class SplunkHECRawLoggerProvider : ILoggerProvider
16+
public class SplunkHECRawLoggerProvider : SplunkHECBaseProvider, ILoggerProvider
1817
{
1918
readonly BatchManager batchManager;
20-
readonly HttpClient httpClient;
2119
readonly LogLevel threshold;
2220
readonly ILoggerFormatter loggerFormatter;
2321
readonly ConcurrentDictionary<string, ILogger> loggers;
@@ -35,38 +33,17 @@ public SplunkHECRawLoggerProvider(SplunkLoggerConfiguration configuration, ILogg
3533

3634
this.loggerFormatter = loggerFormatter;
3735

38-
httpClient = new HttpClient();
39-
40-
var splunkCollectorUrl = configuration.HecConfiguration.SplunkCollectorUrl;
41-
if (!splunkCollectorUrl.EndsWith("/", StringComparison.InvariantCulture))
42-
splunkCollectorUrl += "/";
43-
44-
if(configuration.HecConfiguration.ChannelIdType == HECConfiguration.ChannelIdOption.QueryString)
45-
splunkCollectorUrl = splunkCollectorUrl + "raw?channel=" + Guid.NewGuid().ToString();
46-
47-
httpClient.BaseAddress = new Uri(splunkCollectorUrl);
48-
49-
httpClient.Timeout = TimeSpan.FromMilliseconds(configuration.HecConfiguration.DefaultTimeoutInMiliseconds);
50-
51-
if(!configuration.HecConfiguration.UseTokenAsQueryString)
52-
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Splunk", configuration.HecConfiguration.Token);
53-
if (configuration.HecConfiguration.ChannelIdType == HECConfiguration.ChannelIdOption.RequestHeader)
54-
httpClient.DefaultRequestHeaders.Add("x-splunk-request-channel", Guid.NewGuid().ToString());
36+
SetupHttpClient(configuration, "raw");
5537

5638
batchManager = new BatchManager(configuration.HecConfiguration.BatchSizeCount, configuration.HecConfiguration.BatchIntervalInMiliseconds, Emit);
5739
}
5840

59-
ILogger CreateLoggerInstance(string categoryName)
60-
{
61-
return new HECRawLogger(categoryName, threshold, httpClient, batchManager, loggerFormatter);
62-
}
63-
6441
/// <summary>
6542
/// Create a <see cref="T:Splunk.Loggers.HECRawLogger"/> instance to the category name provided.
6643
/// </summary>
6744
/// <returns><see cref="T:Splunk.Loggers.HECRawLogger"/> instance.</returns>
6845
/// <param name="categoryName">Category name.</param>
69-
public ILogger CreateLogger(string categoryName)
46+
public override ILogger CreateLogger(string categoryName)
7047
{
7148
return loggers.GetOrAdd(categoryName, CreateLoggerInstance(categoryName));
7249
}
@@ -80,11 +57,21 @@ public ILogger CreateLogger(string categoryName)
8057
/// <see cref="Dispose"/>, you must release all references to the
8158
/// <see cref="T:Splunk.Providers.SplunkHECRawLoggerProvider"/> so the garbage collector can reclaim the memory
8259
/// that the <see cref="T:Splunk.Providers.SplunkHECRawLoggerProvider"/> was occupying.</remarks>
83-
public void Dispose()
60+
public override void Dispose()
8461
{
8562
loggers.Clear();
8663
}
8764

65+
/// <summary>
66+
/// Create a <see cref="T:Splunk.Loggers.HECRawLogger"/> instance to the category name provided.
67+
/// </summary>
68+
/// <returns><see cref="T:Splunk.Loggers.HECRawLogger"/> instance.</returns>
69+
/// <param name="categoryName">Category name.</param>
70+
public override ILogger CreateLoggerInstance(string categoryName)
71+
{
72+
return new HECRawLogger(categoryName, threshold, httpClient, batchManager, loggerFormatter);
73+
}
74+
8875
/// <summary>
8976
/// Method used to emit batched events.
9077
/// </summary>

0 commit comments

Comments
 (0)