Skip to content

Commit 49763ee

Browse files
authored
Merge pull request #60 from vtex/feature/performance_improvement
Feature/performance improvement
2 parents 023025d + f94aac7 commit 49763ee

17 files changed

+195
-191
lines changed

src/SampleWebAPI/Startup.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,20 @@ public void ConfigureServices(IServiceCollection services)
3535
/// <param name="loggerFactory">Logger factory.</param>
3636
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
3737
{
38-
3938
/******************************** Define Your Logger *********************************/
4039
/* */
41-
// Get Configuration to be used at Logger //
42-
var splunkLoggerConfiguration = GetSplunkLoggerConfiguration(app);
40+
// First get configuration to be used at Logger //
41+
var splunkLoggerConfiguration = GetSplunkLoggerConfiguration(app); //
4342
// //
44-
// Choose one or more of those loggers //
43+
// Choose one of those loggers //
4544
// //
4645
loggerFactory.AddHECRawSplunkLogger(splunkLoggerConfiguration); //
4746
// //
47+
//loggerFactory.AddHECJsonSplunkLogger(splunkLoggerConfiguration); //
4848
// //
49-
//loggerFactory.AddHECJsonSplunkLogger(splunkConfiguration); //
49+
//loggerFactory.AddTcpSplunkLogger(splunkLoggerConfiguration); //
5050
// //
51-
//loggerFactory.AddTcpSplunkLogger(splunkConfiguration); //
52-
// //
53-
//loggerFactory.AddUdpSplunkLogger(splunkConfiguration); //
51+
//loggerFactory.AddUdpSplunkLogger(splunkLoggerConfiguration); //
5452
/* */
5553
/******************************** Define Your Logger *********************************/
5654

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace Splunk
5+
{
6+
public class BasicLoggerFormatter : ILoggerFormatter
7+
{
8+
public string Format<T>(string categoryName, LogLevel logLevel, EventId eventId, T state, Exception exception)
9+
{
10+
return string.Format("{0}: [{1}] [{2}:{3}] {4} {5}",
11+
categoryName,
12+
logLevel.ToString(),
13+
eventId.Id,
14+
eventId.Name,
15+
state != null ? state.ToString() : string.Empty,
16+
exception != null ? exception.ToString() : string.Empty);
17+
}
18+
19+
public SplunkJSONEntry FormatJson<T>(string categoryName, LogLevel logLevel, EventId eventId, T state, Exception exception)
20+
{
21+
string eventText = Format(categoryName, logLevel, eventId, state, exception);
22+
return new SplunkJSONEntry(eventText);
23+
}
24+
}
25+
}

src/SplunkLogger/BatchManager.cs

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
4+
using System.Threading;
45
using System.Threading.Tasks;
5-
using System.Timers;
66

77
namespace Splunk
88
{
@@ -18,7 +18,6 @@ public class BatchManager
1818
{
1919
readonly ConcurrentBag<object> events;
2020
readonly uint batchSizeCount;
21-
readonly uint batchIntervalInMilliseconds;
2221
readonly Timer timer;
2322
readonly Action<List<object>> emitAction;
2423

@@ -31,57 +30,44 @@ public class BatchManager
3130
/// <param name="batchSizeCount">Batch size count.</param>
3231
/// <param name="batchIntervalInMilliseconds">Batch interval in milliseconds.</param>
3332
/// <param name="emitAction">Emit action to be invoked at Emit process.</param>
34-
public BatchManager(uint batchSizeCount, uint batchIntervalInMilliseconds, Action<List<object>> emitAction)
33+
public BatchManager(uint batchSizeCount, int batchIntervalInMilliseconds, Action<List<object>> emitAction)
3534
{
3635
events = new ConcurrentBag<object>();
3736
this.batchSizeCount = batchSizeCount;
38-
this.batchIntervalInMilliseconds = batchIntervalInMilliseconds;
3937

4038
if (batchIntervalInMilliseconds > 0)
41-
{
42-
timer = new Timer(batchIntervalInMilliseconds);
43-
timer.AutoReset = false;
44-
timer.Enabled = true;
45-
timer.Elapsed += TimerTick;
46-
timer.Start();
47-
}
39+
timer = new Timer(EmitTimeChek, null, 0, batchIntervalInMilliseconds);
4840

4941
this.emitAction = emitAction;
5042
}
5143

52-
void TimerTick(object sender, ElapsedEventArgs e)
44+
void EmitTimeChek(object state)
5345
{
54-
Task
55-
.Factory
56-
.StartNew(() =>
57-
{
58-
if (events.Count > 0)
59-
Emit();
60-
}).ContinueWith(task =>
61-
{
62-
timer?.Start();
63-
});
46+
if (events.Count > 0)
47+
Emit();
6448
}
6549

6650
void Emit()
6751
{
68-
bool continueExtraction = true;
69-
List<object> emitEvents = new List<object>();
70-
while (continueExtraction)
71-
{
72-
if (events.Count == 0)
73-
continueExtraction = false;
74-
else
52+
Task.Factory.StartNew(() => {
53+
bool continueExtraction = true;
54+
List<object> emitEvents = new List<object>();
55+
while (continueExtraction)
7556
{
76-
events.TryTake(out object item);
77-
if (item != null)
78-
emitEvents.Add(item);
79-
if (events.Count == 0 || emitEvents.Count >= batchSizeCount)
57+
if (events.Count == 0)
8058
continueExtraction = false;
59+
else
60+
{
61+
events.TryTake(out object item);
62+
if (item != null)
63+
emitEvents.Add(item);
64+
if (events.Count == 0 || emitEvents.Count >= batchSizeCount)
65+
continueExtraction = false;
66+
}
8167
}
82-
}
83-
if (emitEvents.Count > 0)
84-
emitAction?.Invoke(emitEvents);
68+
if (emitEvents.Count > 0)
69+
emitAction?.Invoke(emitEvents);
70+
});
8571
}
8672

8773
/// <summary>

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/LoggerFactoryExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace Splunk
99
/// </summary>
1010
public static class LoggerFactoryExtensions
1111
{
12+
static readonly ILoggerFormatter DefaultLoggerFormatter = new BasicLoggerFormatter();
13+
1214
/// <summary>
1315
/// Add <see cref="T:Splunk.Providers.SplunkHECRawLoggerProvider"/> as provider to logger factory.
1416
/// </summary>
@@ -17,6 +19,8 @@ public static class LoggerFactoryExtensions
1719
/// <param name="formatter">Custom text formatter.</param>
1820
public static ILoggerFactory AddHECRawSplunkLogger(this ILoggerFactory loggerFactory, SplunkLoggerConfiguration configuration, ILoggerFormatter formatter = null)
1921
{
22+
if (formatter == null)
23+
formatter = DefaultLoggerFormatter;
2024
loggerFactory.AddProvider(new SplunkHECRawLoggerProvider(configuration, formatter));
2125
return loggerFactory;
2226
}
@@ -29,6 +33,8 @@ public static ILoggerFactory AddHECRawSplunkLogger(this ILoggerFactory loggerFac
2933
/// <param name="formatter">Custom text formatter.</param>
3034
public static ILoggerFactory AddHECJsonSplunkLogger(this ILoggerFactory loggerFactory, SplunkLoggerConfiguration configuration, ILoggerFormatter formatter = null)
3135
{
36+
if (formatter == null)
37+
formatter = DefaultLoggerFormatter;
3238
loggerFactory.AddProvider(new SplunkHECJsonLoggerProvider(configuration, formatter));
3339
return loggerFactory;
3440
}
@@ -41,6 +47,8 @@ public static ILoggerFactory AddHECJsonSplunkLogger(this ILoggerFactory loggerFa
4147
/// <param name="formatter">Custom text formatter.</param>
4248
public static ILoggerFactory AddTcpSplunkLogger(this ILoggerFactory loggerFactory, SplunkLoggerConfiguration configuration, ILoggerFormatter formatter = null)
4349
{
50+
if (formatter == null)
51+
formatter = DefaultLoggerFormatter;
4452
loggerFactory.AddProvider(new SplunkTcpLoggerProvider(configuration, formatter));
4553
return loggerFactory;
4654
}
@@ -53,6 +61,8 @@ public static ILoggerFactory AddTcpSplunkLogger(this ILoggerFactory loggerFactor
5361
/// <param name="formatter">Custom text formatter.</param>
5462
public static ILoggerFactory AddUdpSplunkLogger(this ILoggerFactory loggerFactory, SplunkLoggerConfiguration configuration, ILoggerFormatter formatter = null)
5563
{
64+
if (formatter == null)
65+
formatter = DefaultLoggerFormatter;
5666
loggerFactory.AddProvider(new SplunkUdpLoggerProvider(configuration, formatter));
5767
return loggerFactory;
5868
}

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/HECBaseLogger.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Net.Http;
2-
using Microsoft.Extensions.Logging;
32

43
namespace Splunk.Loggers
54
{

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

0 commit comments

Comments
 (0)