Skip to content

Commit

Permalink
Updates for UDP and TCP sinks (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
EEParker authored Apr 3, 2024
1 parent edc2e81 commit aaa9e96
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class EventCollectorSink : IBatchedLogEventSink
/// <param name="eventCollectorToken">The token to use when authenticating with the event collector</param>
/// <param name="formatProvider">The format provider used when rendering the message</param>
/// <param name="renderTemplate">Whether to render the message template</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter from output JSON message.</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter in output JSON message.</param>
/// <param name="subSecondPrecision">Timestamp sub-second precision. Splunk props.conf setup is required.</param>
public EventCollectorSink(
string splunkHost,
Expand Down Expand Up @@ -89,7 +89,7 @@ public EventCollectorSink(
/// <param name="sourceType">The source type of the event</param>
/// <param name="host">The host of the event</param>
/// <param name="messageHandler">The handler used to send HTTP requests</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter from output JSON message.</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter in output JSON message.</param>
/// <param name="subSecondPrecision">Timestamp sub-second precision. Splunk props.conf setup is required.</param>
public EventCollectorSink(
string splunkHost,
Expand Down Expand Up @@ -127,7 +127,7 @@ public EventCollectorSink(
/// <param name="sourceType">The source type of the event</param>
/// <param name="host">The host of the event</param>
/// <param name="messageHandler">The handler used to send HTTP requests</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter from output JSON message.</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter in output JSON message.</param>
/// <param name="subSecondPrecision">Timestamp sub-second precision. Splunk props.conf setup is required.</param>
public EventCollectorSink(
string splunkHost,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public SplunkJsonFormatter(
/// <param name="host">The host of the event</param>
/// <param name="customFields">Object that describes extra splunk fields that should be indexed with event see: http://dev.splunk.com/view/event-collector/SP-CAAAFB6 </param>
/// <param name="subSecondPrecision">Timestamp sub-second precision. Splunk props.conf setup is required.</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter from output JSON message.</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter in output JSON message.</param>
public SplunkJsonFormatter(
bool renderTemplate,
bool renderMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static class SplunkLoggingConfigurationExtensions
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="renderTemplate">If true, the message template will be rendered</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter from output JSON message.</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter in output JSON message.</param>
/// <param name="batchIntervalInSeconds">The interval in seconds that the queue should be instpected for batching</param>
/// <param name="batchSizeLimit">The size of the batch</param>
/// <param name="queueLimit">Maximum number of events in the queue</param>
Expand Down Expand Up @@ -92,6 +92,7 @@ public static LoggerConfiguration EventCollector(
formatProvider,
renderTemplate,
renderMessage,
messageHandler: messageHandler,
subSecondPrecision: subSecondPrecision);

var batchingSink = new PeriodicBatchingSink(eventCollectorSink, batchingOptions);
Expand Down Expand Up @@ -172,7 +173,7 @@ public static LoggerConfiguration EventCollector(
/// <param name="messageHandler">The handler used to send HTTP requests</param>
/// <param name="levelSwitch">A switch allowing the pass-through minimum level to be changed at runtime.</param>
/// <param name="fields">Customfields that will be indexed in splunk with this event</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter from output JSON message.</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter in output JSON message.</param>
/// <param name="subSecondPrecision">Timestamp sub-second precision. Splunk props.conf setup is required.</param>
/// <returns></returns>
public static LoggerConfiguration EventCollector(
Expand Down Expand Up @@ -228,4 +229,4 @@ public static LoggerConfiguration EventCollector(
return configuration.Sink(batchingSink, restrictedToMinimumLevel, levelSwitch);
}
}
}
}
9 changes: 5 additions & 4 deletions src/Serilog.Sinks.TCP/Sinks/Splunk/TcpSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ public class TcpSink : ILogEventSink, IDisposable
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
/// <param name="formatProvider">Optional format provider</param>
/// <param name="renderTemplate">If true, the message template will be rendered</param>
public TcpSink(SplunkTcpSinkConnectionInfo connectionInfo, IFormatProvider formatProvider = null, bool renderTemplate = true)
: this(connectionInfo, CreateDefaultFormatter(formatProvider, renderTemplate))
/// <param name="renderMessage">Include "RenderedMessage" parameter in output JSON message.</param>
public TcpSink(SplunkTcpSinkConnectionInfo connectionInfo, IFormatProvider formatProvider = null, bool renderTemplate = true, bool renderMessage = true)
: this(connectionInfo, CreateDefaultFormatter(formatProvider, renderTemplate, renderMessage))
{
}

Expand Down Expand Up @@ -94,9 +95,9 @@ private static TcpSocketWriter CreateSocketWriter(SplunkTcpSinkConnectionInfo co
return new TcpSocketWriter(connectionInfo.Host, connectionInfo.Port, reconnectionPolicy, connectionInfo.MaxQueueSize);
}

private static SplunkJsonFormatter CreateDefaultFormatter(IFormatProvider formatProvider, bool renderTemplate)
private static SplunkJsonFormatter CreateDefaultFormatter(IFormatProvider formatProvider, bool renderTemplate, bool renderMessage = true)
{
return new SplunkJsonFormatter(renderTemplate, formatProvider);
return new SplunkJsonFormatter(renderTemplate, renderMessage, formatProvider);
}
}
}
7 changes: 4 additions & 3 deletions src/Serilog.Sinks.TCP/SplunkLoggingConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ public static class LoggerConfigurationSplunkExtensions
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="renderTemplate">If true, the message template is rendered</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter in output JSON message.</param>
/// <returns></returns>
public static LoggerConfiguration SplunkViaTcp(this LoggerSinkConfiguration loggerConfiguration, SplunkTcpSinkConnectionInfo connectionInfo,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, IFormatProvider formatProvider = null, bool renderTemplate = true)
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, IFormatProvider formatProvider = null, bool renderTemplate = true, bool renderMessage = true)
{
var sink = new TcpSink(connectionInfo, formatProvider, renderTemplate);
var sink = new TcpSink(connectionInfo, formatProvider, renderTemplate, renderMessage);

return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
}
Expand All @@ -58,4 +59,4 @@ public static LoggerConfiguration SplunkViaTcp(this LoggerSinkConfiguration logg
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
}
}
}
}
25 changes: 13 additions & 12 deletions src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Serilog.Sinks.Splunk
/// <summary>
/// A sink that logs to Splunk over UDP
/// </summary>
public class UdpSink : PeriodicBatchingSink
public class UdpSink : IBatchedLogEventSink
{
private readonly SplunkUdpSinkConnectionInfo _connectionInfo;
private readonly ITextFormatter _formatter;
Expand All @@ -40,8 +40,9 @@ public class UdpSink : PeriodicBatchingSink
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
/// <param name="formatProvider">Optional format provider</param>
/// <param name="renderTemplate">If true, the message template will be rendered</param>
public UdpSink(SplunkUdpSinkConnectionInfo connectionInfo, IFormatProvider formatProvider = null, bool renderTemplate = true)
: this(connectionInfo, CreateDefaultFormatter(formatProvider, renderTemplate))
/// <param name="renderMessage">Include "RenderedMessage" parameter in output JSON message.</param>
public UdpSink(SplunkUdpSinkConnectionInfo connectionInfo, IFormatProvider formatProvider = null, bool renderTemplate = true, bool renderMessage = true)
: this(connectionInfo, CreateDefaultFormatter(formatProvider, renderTemplate, renderMessage))
{
}

Expand All @@ -51,7 +52,6 @@ public UdpSink(SplunkUdpSinkConnectionInfo connectionInfo, IFormatProvider forma
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
/// <param name="formatter">Custom formatter to use if you e.g. do not want to use the JsonFormatter.</param>
public UdpSink(SplunkUdpSinkConnectionInfo connectionInfo, ITextFormatter formatter)
: base(connectionInfo.BatchPostingLimit, connectionInfo.Period, connectionInfo.QueueSizeLimit)
{
_connectionInfo = connectionInfo;
_formatter = formatter;
Expand All @@ -74,11 +74,9 @@ private void Connect()
}


protected override void Dispose(bool disposing)
protected void Dispose(bool disposing)

Check warning on line 77 in src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'UdpSink.Dispose(bool)'

Check warning on line 77 in src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'UdpSink.Dispose(bool)'

Check warning on line 77 in src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'UdpSink.Dispose(bool)'

Check warning on line 77 in src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'UdpSink.Dispose(bool)'

Check warning on line 77 in src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'UdpSink.Dispose(bool)'

Check warning on line 77 in src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'UdpSink.Dispose(bool)'

Check warning on line 77 in src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'UdpSink.Dispose(bool)'
{
DisposeSocket();

base.Dispose(true);
}

private void DisposeSocket()
Expand All @@ -91,15 +89,15 @@ private void DisposeSocket()
/// <summary>
/// Emit a batch of log events, running to completion synchronously.
/// </summary>
/// <param name="events">The events to emit.</param>
/// <param name="batch">The events to emit.</param>
/// <remarks>
/// Override either <see cref="M:Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.EmitBatch(System.Collections.Generic.IEnumerable{Serilog.Events.LogEvent})" />
/// or <see cref="M:Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.EmitBatchAsync(System.Collections.Generic.IEnumerable{Serilog.Events.LogEvent})" />,
/// not both.
/// </remarks>
protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
public async Task EmitBatchAsync(IEnumerable<LogEvent> batch)

Check warning on line 98 in src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 98 in src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 98 in src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 98 in src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 98 in src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 98 in src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 98 in src/Serilog.Sinks.UDP/Sinks/Splunk/UdpSink.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
foreach (var logEvent in events)
foreach (var logEvent in batch)
{
byte[] data = Convert(logEvent);

Expand All @@ -118,10 +116,13 @@ protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
}


private static SplunkJsonFormatter CreateDefaultFormatter(IFormatProvider formatProvider, bool renderTemplate)
private static SplunkJsonFormatter CreateDefaultFormatter(IFormatProvider formatProvider, bool renderTemplate, bool renderMessage)
{
return new SplunkJsonFormatter(renderTemplate, formatProvider);
return new SplunkJsonFormatter(renderTemplate, renderMessage, formatProvider);
}

/// <inheritdoc />
public Task OnEmptyBatchAsync() => Task.CompletedTask;
}
}

35 changes: 29 additions & 6 deletions src/Serilog.Sinks.UDP/SplunkLoggingConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Serilog.Configuration;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Sinks.PeriodicBatching;
using Serilog.Sinks.Splunk;
using System;

Expand All @@ -33,17 +34,29 @@ public static class LoggerConfigurationSplunkExtensions
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="renderTemplate">If true, the message template is rendered</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter in output JSON message.</param>
/// <returns></returns>
public static LoggerConfiguration SplunkViaUdp(
this LoggerSinkConfiguration loggerConfiguration,
SplunkUdpSinkConnectionInfo connectionInfo,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IFormatProvider formatProvider = null,
bool renderTemplate = true)
bool renderTemplate = true,
bool renderMessage = true)
{
var sink = new UdpSink(connectionInfo, formatProvider, renderTemplate);
var batchingOptions = new PeriodicBatchingSinkOptions
{
BatchSizeLimit = connectionInfo.BatchPostingLimit,
Period = connectionInfo.Period,
EagerlyEmitFirstEvent = true,
QueueLimit = connectionInfo.QueueSizeLimit
};

return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
var sink = new UdpSink(connectionInfo, formatProvider, renderTemplate, renderMessage);

var batchingSink = new PeriodicBatchingSink(sink, batchingOptions);

return loggerConfiguration.Sink(batchingSink, restrictedToMinimumLevel);
}

/// <summary>
Expand All @@ -60,9 +73,19 @@ public static LoggerConfiguration SplunkViaUdp(
ITextFormatter formatter,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum)
{
var batchingOptions = new PeriodicBatchingSinkOptions
{
BatchSizeLimit = connectionInfo.BatchPostingLimit,
Period = connectionInfo.Period,
EagerlyEmitFirstEvent = true,
QueueLimit = connectionInfo.QueueSizeLimit
};

var sink = new UdpSink(connectionInfo, formatter);

return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);

var batchingSink = new PeriodicBatchingSink(sink, batchingOptions);

return loggerConfiguration.Sink(batchingSink, restrictedToMinimumLevel);
}
}
}
}

0 comments on commit aaa9e96

Please sign in to comment.