Skip to content

Commit bcef281

Browse files
committed
Changed pipeline to use category name at log messages
#58
1 parent 916dc11 commit bcef281

12 files changed

+132
-146
lines changed

src/SplunkLogger/Configurations/HECConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public enum ChannelIdOption
1616
/// <summary>
1717
/// Gets or sets the batch interval in milliseconds.
1818
/// </summary>
19-
public uint BatchIntervalInMilliseconds { get; set; }
19+
public int BatchIntervalInMilliseconds { get; set; }
2020

2121
/// <summary>
2222
/// Gets or sets the batch size count.

src/SplunkLogger/ILoggerFormatter.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,24 @@ public interface ILoggerFormatter
1212
/// Format the specified logLevel, eventId, state and exception into log string entry.
1313
/// </summary>
1414
/// <returns>Formatted log string.</returns>
15+
/// <param name="categoryName">Log category name.</param>
1516
/// <param name="logLevel">Log level.</param>
1617
/// <param name="eventId">Event identifier.</param>
1718
/// <param name="state">Log object state.</param>
1819
/// <param name="exception">Log exception.</param>
1920
/// <typeparam name="T">Log entry.</typeparam>
20-
string Format<T>(LogLevel logLevel, EventId eventId, T state, Exception exception);
21+
string Format<T>(string categoryName, LogLevel logLevel, EventId eventId, T state, Exception exception);
2122

2223
/// <summary>
2324
/// Formats the specified logLevel, eventId, state and exception into json entry.
2425
/// </summary>
2526
/// <returns>The json.</returns>
27+
/// <param name="categoryName">Log category name.</param>
2628
/// <param name="logLevel">Log level.</param>
2729
/// <param name="eventId">Event identifier.</param>
2830
/// <param name="state">Log object state.</param>
2931
/// <param name="exception">Log exception.</param>
3032
/// <typeparam name="T">Log entry.</typeparam>
31-
SplunkJSONEntry FormatJson<T>(LogLevel logLevel, EventId eventId, T state, Exception exception);
33+
SplunkJSONEntry FormatJson<T>(string categoryName, LogLevel logLevel, EventId eventId, T state, Exception exception);
3234
}
3335
}

src/SplunkLogger/Loggers/BaseLogger.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ namespace Splunk.Loggers
99
public abstract class BaseLogger : ILogger
1010
{
1111
protected readonly ILoggerFormatter loggerFormatter;
12-
13-
readonly string categoryName;
12+
protected readonly string categoryName;
1413

1514
/// <summary>
1615
/// Initializes a new instance of the <see cref="T:Splunk.Loggers.BaseLogger"/> class.

src/SplunkLogger/Loggers/HECJsonLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public override void Log<T>(LogLevel logLevel, EventId eventId, T state, Excepti
3535
{
3636
SplunkJSONEntry formatedMessage = null;
3737
if (loggerFormatter != null)
38-
formatedMessage = loggerFormatter.FormatJson(logLevel, eventId, state, exception);
38+
formatedMessage = loggerFormatter.FormatJson(categoryName, logLevel, eventId, state, exception);
3939
else if (formatter != null)
4040
formatedMessage = new SplunkJSONEntry(formatter(state, exception));
4141
batchManager.Add(JObject.FromObject(formatedMessage));

src/SplunkLogger/Loggers/HECRawLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public override void Log<T>(LogLevel logLevel, EventId eventId, T state, Excepti
3434
{
3535
string formatedMessage = string.Empty;
3636
if (loggerFormatter != null)
37-
formatedMessage = loggerFormatter.Format(logLevel, eventId, state, exception);
37+
formatedMessage = loggerFormatter.Format(categoryName, logLevel, eventId, state, exception);
3838
else if (formatter != null)
3939
formatedMessage = formatter(state, exception);
4040

src/SplunkLogger/Loggers/TcpLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public override void Log<T>(LogLevel logLevel, EventId eventId, T state, Excepti
3737
{
3838
string formatedMessage = string.Empty;
3939
if (loggerFormatter != null)
40-
formatedMessage = loggerFormatter.Format(logLevel, eventId, state, exception);
40+
formatedMessage = loggerFormatter.Format(categoryName, logLevel, eventId, state, exception);
4141
else if (formatter != null)
4242
formatedMessage = formatter(state, exception);
4343

src/SplunkLogger/Loggers/UdpLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public override void Log<T>(LogLevel logLevel, EventId eventId, T state, Excepti
3737
{
3838
string formatedMessage = string.Empty;
3939
if (loggerFormatter != null)
40-
formatedMessage = loggerFormatter.Format(logLevel, eventId, state, exception);
40+
formatedMessage = loggerFormatter.Format(categoryName, logLevel, eventId, state, exception);
4141
else if (formatter != null)
4242
formatedMessage = formatter(state, exception);
4343

src/SplunkLogger/Providers/SplunkHECBaseProvider.cs

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@ namespace Splunk.Providers
1313
/// </summary>
1414
public abstract class SplunkHECBaseProvider : ILoggerProvider
1515
{
16+
protected ILogger loggerInstance;
1617
protected HttpClient httpClient;
1718

19+
public SplunkHECBaseProvider(SplunkLoggerConfiguration configuration, string endPointCustomization)
20+
{
21+
SetupHttpClient(configuration, endPointCustomization);
22+
}
23+
1824
/// <summary>
1925
/// Get a <see cref="T:Splunk.Loggers.HECJsonLogger"/> instance to the category name provided.
2026
/// </summary>
@@ -33,14 +39,7 @@ public abstract class SplunkHECBaseProvider : ILoggerProvider
3339
/// that the <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> was occupying.</remarks>
3440
public abstract void Dispose();
3541

36-
/// <summary>
37-
/// Create a <see cref="T:Splunk.Loggers.HECJsonLogger"/> instance to the category name provided.
38-
/// </summary>
39-
/// <returns>The logger instance.</returns>
40-
/// <param name="categoryName">Category name.</param>
41-
public abstract ILogger CreateLoggerInstance(string categoryName);
42-
43-
protected void SetupHttpClient(SplunkLoggerConfiguration configuration, string endPointCustomization)
42+
void SetupHttpClient(SplunkLoggerConfiguration configuration, string endPointCustomization)
4443
{
4544
httpClient = new HttpClient
4645
{
@@ -55,69 +54,69 @@ protected void SetupHttpClient(SplunkLoggerConfiguration configuration, string e
5554
httpClient.DefaultRequestHeaders.Add("x-splunk-request-channel", Guid.NewGuid().ToString());
5655
}
5756

58-
Uri GetSplunkCollectorUrl(SplunkLoggerConfiguration configuration, string endPointCustomization)
59-
{
60-
var splunkCollectorUrl = configuration.HecConfiguration.SplunkCollectorUrl;
61-
if (!splunkCollectorUrl.EndsWith("/", StringComparison.InvariantCulture))
62-
splunkCollectorUrl = splunkCollectorUrl + "/";
63-
64-
if(!string.IsNullOrWhiteSpace(endPointCustomization))
65-
splunkCollectorUrl = splunkCollectorUrl + endPointCustomization;
66-
67-
if (configuration.HecConfiguration.ChannelIdType == HECConfiguration.ChannelIdOption.QueryString)
68-
splunkCollectorUrl = splunkCollectorUrl + "?channel=" + Guid.NewGuid().ToString();
69-
70-
if(configuration.HecConfiguration.UseAuthTokenAsQueryString)
71-
{
72-
var tokenParameter = "token=" + configuration.HecConfiguration.Token;
73-
splunkCollectorUrl = string.Format("{0}{1}{2}", splunkCollectorUrl, splunkCollectorUrl.Contains("?") ? "&" : "?", tokenParameter);
74-
}
75-
76-
return new Uri(splunkCollectorUrl);
77-
}
78-
7957
protected void DebugSplunkResponse(Task<HttpResponseMessage> responseMessageTask, string loggerType)
8058
{
8159
if (responseMessageTask.IsCompletedSuccessfully)
82-
{
60+
{
8361
switch (responseMessageTask.Result.StatusCode)
8462
{
8563
case System.Net.HttpStatusCode.OK:
86-
Debug.WriteLine($"Splunk HEC {loggerType} Status: Request completed successfully.");
64+
Debug.WriteLine($"[DEBUG] Splunk HEC {loggerType} Status: Request completed successfully.");
8765
break;
8866
case System.Net.HttpStatusCode.Created:
89-
Debug.WriteLine($"Splunk HEC {loggerType} Status: Create request completed successfully.");
67+
Debug.WriteLine($"[DEBUG] Splunk HEC {loggerType} Status: Create request completed successfully.");
9068
break;
9169
case System.Net.HttpStatusCode.BadRequest:
92-
Debug.WriteLine($"Splunk HEC {loggerType} Status: Request error. See response body for details.");
70+
Debug.WriteLine($"[DEBUG] Splunk HEC {loggerType} Status: Request error. See response body for details.");
9371
break;
9472
case System.Net.HttpStatusCode.Unauthorized:
95-
Debug.WriteLine($"Splunk HEC {loggerType} Status: Authentication failure, invalid access credentials.");
73+
Debug.WriteLine($"[DEBUG] Splunk HEC {loggerType} Status: Authentication failure, invalid access credentials.");
9674
break;
9775
case System.Net.HttpStatusCode.PaymentRequired:
98-
Debug.WriteLine($"Splunk HEC {loggerType} Status: In-use Splunk Enterprise license disables this feature.");
76+
Debug.WriteLine($"[DEBUG] Splunk HEC {loggerType} Status: In-use Splunk Enterprise license disables this feature.");
9977
break;
10078
case System.Net.HttpStatusCode.Forbidden:
101-
Debug.WriteLine($"Splunk HEC {loggerType} Status: Insufficient permission.");
79+
Debug.WriteLine($"[DEBUG] Splunk HEC {loggerType} Status: Insufficient permission.");
10280
break;
10381
case System.Net.HttpStatusCode.NotFound:
104-
Debug.WriteLine($"Splunk HEC {loggerType} Status: Requested endpoint does not exist.");
82+
Debug.WriteLine($"[DEBUG] Splunk HEC {loggerType} Status: Requested endpoint does not exist.");
10583
break;
10684
case System.Net.HttpStatusCode.Conflict:
107-
Debug.WriteLine($"Splunk HEC {loggerType} Status: Invalid operation for this endpoint. See response body for details.");
85+
Debug.WriteLine($"[DEBUG] Splunk HEC {loggerType} Status: Invalid operation for this endpoint. See response body for details.");
10886
break;
10987
case System.Net.HttpStatusCode.InternalServerError:
110-
Debug.WriteLine($"Splunk HEC {loggerType} Status: Unspecified internal server error. See response body for details.");
88+
Debug.WriteLine($"[DEBUG] Splunk HEC {loggerType} Status: Unspecified internal server error. See response body for details.");
11189
break;
11290
case System.Net.HttpStatusCode.ServiceUnavailable:
113-
Debug.WriteLine($"Splunk HEC {loggerType} Status: Feature is disabled in configuration file.");
91+
Debug.WriteLine($"[DEBUG] Splunk HEC {loggerType} Status: Feature is disabled in configuration file.");
11492
break;
115-
}
93+
}
11694
}
11795
else if (responseMessageTask.IsCanceled)
118-
Debug.WriteLine($"Splunk HEC {loggerType} Status: Canceled");
96+
Debug.WriteLine($"[DEBUG] Splunk HEC {loggerType} Status: Canceled");
11997
else
120-
Debug.WriteLine($"Splunk HEC {loggerType} Status: Error " + responseMessageTask.Exception != null ? responseMessageTask.Exception.ToString() : "");
98+
Debug.WriteLine($"[DEBUG] Splunk HEC {loggerType} Status: Error " + responseMessageTask.Exception != null ? responseMessageTask.Exception.ToString() : "");
99+
}
100+
101+
Uri GetSplunkCollectorUrl(SplunkLoggerConfiguration configuration, string endPointCustomization)
102+
{
103+
var splunkCollectorUrl = configuration.HecConfiguration.SplunkCollectorUrl;
104+
if (!splunkCollectorUrl.EndsWith("/", StringComparison.InvariantCulture))
105+
splunkCollectorUrl = splunkCollectorUrl + "/";
106+
107+
if(!string.IsNullOrWhiteSpace(endPointCustomization))
108+
splunkCollectorUrl = splunkCollectorUrl + endPointCustomization;
109+
110+
if (configuration.HecConfiguration.ChannelIdType == HECConfiguration.ChannelIdOption.QueryString)
111+
splunkCollectorUrl = splunkCollectorUrl + "?channel=" + Guid.NewGuid().ToString();
112+
113+
if(configuration.HecConfiguration.UseAuthTokenAsQueryString)
114+
{
115+
var tokenParameter = "token=" + configuration.HecConfiguration.Token;
116+
splunkCollectorUrl = string.Format("{0}{1}{2}", splunkCollectorUrl, splunkCollectorUrl.Contains("?") ? "&" : "?", tokenParameter);
117+
}
118+
119+
return new Uri(splunkCollectorUrl);
121120
}
122121
}
123122
}
Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Concurrent;
1+
using System.Collections.Concurrent;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Net.Http;
@@ -15,7 +15,7 @@ namespace Splunk.Providers
1515
/// </summary>
1616
public class SplunkHECJsonLoggerProvider : SplunkHECBaseProvider, ILoggerProvider
1717
{
18-
readonly BatchManager batchController;
18+
readonly BatchManager batchManager;
1919
readonly ILoggerFormatter loggerFormatter;
2020
readonly ConcurrentDictionary<string, ILogger> loggers;
2121

@@ -25,49 +25,36 @@ public class SplunkHECJsonLoggerProvider : SplunkHECBaseProvider, ILoggerProvide
2525
/// <param name="configuration">Splunk configuration instance for HEC.</param>
2626
/// <param name="loggerFormatter">Formatter instance.</param>
2727
public SplunkHECJsonLoggerProvider(SplunkLoggerConfiguration configuration, ILoggerFormatter loggerFormatter = null)
28+
: base(configuration, "event")
2829
{
29-
loggers = new ConcurrentDictionary<string, ILogger>();
30-
3130
this.loggerFormatter = loggerFormatter;
32-
33-
SetupHttpClient(configuration, "event");
34-
35-
batchController = new BatchManager(configuration.HecConfiguration.BatchSizeCount, configuration.HecConfiguration.BatchIntervalInMilliseconds, Emit);
36-
}
37-
38-
/// <summary>
39-
/// Get a <see cref="T:Splunk.Loggers.HECJsonLogger"/> instance to the category name provided.
40-
/// </summary>
41-
/// <returns><see cref="T:Splunk.Loggers.HECJsonLogger"/> instance.</returns>
42-
/// <param name="categoryName">Category name.</param>
43-
public override ILogger CreateLogger(string categoryName)
44-
{
45-
return loggers.GetOrAdd(categoryName, CreateLoggerInstance(categoryName));
46-
}
47-
48-
/// <summary>
49-
/// Releases all resource used by the <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> object.
50-
/// </summary>
51-
/// <remarks>Call <see cref="Dispose"/> when you are finished using the
52-
/// <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/>. The <see cref="Dispose"/> method leaves the
53-
/// <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> in an unusable state. After calling
54-
/// <see cref="Dispose"/>, you must release all references to the
55-
/// <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> so the garbage collector can reclaim the memory
56-
/// that the <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> was occupying.</remarks>
57-
public override void Dispose()
58-
{
59-
loggers.Clear();
60-
}
61-
62-
/// <summary>
63-
/// Create a <see cref="T:Splunk.Loggers.HECJsonLogger"/> instance to the category name provided.
64-
/// </summary>
65-
/// <returns>The logger instance.</returns>
66-
/// <param name="categoryName">Category name.</param>
67-
public override ILogger CreateLoggerInstance(string categoryName)
68-
{
69-
return new HECJsonLogger(categoryName, httpClient, batchController, loggerFormatter);
31+
loggers = new ConcurrentDictionary<string, ILogger>();
32+
batchManager = new BatchManager(configuration.HecConfiguration.BatchSizeCount, configuration.HecConfiguration.BatchIntervalInMilliseconds, Emit);
7033
}
34+
35+
/// <summary>
36+
/// Get a <see cref="T:Splunk.Loggers.HECJsonLogger"/> instance to the category name provided.
37+
/// </summary>
38+
/// <returns><see cref="T:Splunk.Loggers.HECJsonLogger"/> instance.</returns>
39+
/// <param name="categoryName">Category name.</param>
40+
public override ILogger CreateLogger(string categoryName)
41+
{
42+
return loggers.GetOrAdd(categoryName, CreateLoggerInstance(categoryName));
43+
}
44+
45+
/// <summary>
46+
/// Releases all resource used by the <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> object.
47+
/// </summary>
48+
/// <remarks>Call <see cref="Dispose"/> when you are finished using the
49+
/// <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/>. The <see cref="Dispose"/> method leaves the
50+
/// <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> in an unusable state. After calling
51+
/// <see cref="Dispose"/>, you must release all references to the
52+
/// <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> so the garbage collector can reclaim the memory
53+
/// that the <see cref="T:Splunk.Providers.SplunkHECJsonLoggerProvider"/> was occupying.</remarks>
54+
public override void Dispose()
55+
{
56+
loggers.Clear();
57+
}
7158

7259
/// <summary>
7360
/// Method used to emit batched events.
@@ -80,6 +67,11 @@ public void Emit(List<object> events)
8067
var stringContent = new StringContent(formatedMessage, Encoding.UTF8, "application/json");
8168
httpClient.PostAsync(string.Empty, stringContent)
8269
.ContinueWith(task => DebugSplunkResponse(task, "json"));
70+
}
71+
72+
ILogger CreateLoggerInstance(string categoryName)
73+
{
74+
return new HECJsonLogger(categoryName, httpClient, batchManager, loggerFormatter);
8375
}
8476
}
8577
}

0 commit comments

Comments
 (0)