Skip to content
This repository was archived by the owner on Apr 18, 2022. It is now read-only.

Adds support for LogEntries DataHub local service #32

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public static class LoggerConfigurationLogentriesExtensions
// Logentries API server address.
const string LeApiUrl = "data.logentries.com";

// Port number for token logging on Logentries API server.
const int LeApiTokenPort = 80;

// Port number for TLS encrypted token logging on Logentries API server
const int LeApiTokenTlsPort = 443;

/// <summary>
/// Adds a sink that writes log events to the Logentries.com webservice.
/// Create a token TCP input for this on the logentries website.
Expand All @@ -48,7 +54,8 @@ public static class LoggerConfigurationLogentriesExtensions
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
public static LoggerConfiguration Logentries(
this LoggerSinkConfiguration loggerConfiguration,
string token, bool useSsl = true,
string token,
bool useSsl = true,
int batchPostingLimit = LogentriesSink.DefaultBatchPostingLimit,
TimeSpan? period = null,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
Expand All @@ -63,8 +70,14 @@ public static LoggerConfiguration Logentries(

var defaultedPeriod = period ?? LogentriesSink.DefaultPeriod;

int port;
if (!useSsl)
port = LeApiTokenPort;
else
port = LeApiTokenTlsPort;

return loggerConfiguration.Sink(
new LogentriesSink(outputTemplate, formatProvider, token, useSsl, batchPostingLimit, defaultedPeriod, url),
new LogentriesSink(outputTemplate, formatProvider, token, useSsl, batchPostingLimit, defaultedPeriod, url, port),
restrictedToMinimumLevel);
}

Expand All @@ -84,7 +97,7 @@ public static LoggerConfiguration Logentries(
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
public static LoggerConfiguration Logentries(
this LoggerSinkConfiguration loggerConfiguration,
string token,
string token,
ITextFormatter textFormatter,
bool useSsl = true,
int batchPostingLimit = LogentriesSink.DefaultBatchPostingLimit,
Expand All @@ -102,8 +115,87 @@ public static LoggerConfiguration Logentries(

var defaultedPeriod = period ?? LogentriesSink.DefaultPeriod;

int port;
if (!useSsl)
port = LeApiTokenPort;
else
port = LeApiTokenTlsPort;

return loggerConfiguration.Sink(
new LogentriesSink(textFormatter, token, useSsl, batchPostingLimit, defaultedPeriod, url, port),
restrictedToMinimumLevel);
}
/// <summary>
/// Adds a sink that writes log events to the LogEntries DataHub local service.
/// </summary>
/// <param name="loggerConfiguration">The logger configuration.</param>
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="dataHubServer">The local server name of the datahub service.</param>
/// <param name="dataHubPort">The port number for the local datahub service.</param>
/// <param name="outputTemplate">A message template describing the format used to write to the sink.
/// the default is "{Timestamp:G} [{Level}] {Message}{NewLine}{Exception}".</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
public static LoggerConfiguration Logentries(
this LoggerSinkConfiguration loggerConfiguration,
string dataHubServer,
int dataHubPort,
int batchPostingLimit = LogentriesSink.DefaultBatchPostingLimit,
TimeSpan? period = null,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
string outputTemplate = DefaultLogentriesOutputTemplate,
IFormatProvider formatProvider = null)
{
if (loggerConfiguration == null)
throw new ArgumentNullException(nameof(loggerConfiguration));

if (string.IsNullOrWhiteSpace(dataHubServer))
throw new ArgumentNullException(nameof(dataHubServer));

var defaultedPeriod = period ?? LogentriesSink.DefaultPeriod;

return loggerConfiguration.Sink(
new LogentriesSink(outputTemplate, formatProvider, token: null, useSsl: false, batchPostingLimit, defaultedPeriod, dataHubServer, dataHubPort),
restrictedToMinimumLevel);
}

/// <summary>
/// Adds a sink that writes log events to the LogEntries DataHub local service.
/// </summary>
/// <param name="loggerConfiguration">The logger configuration.</param>
/// <param name="dataHubServer">The local server name of the datahub service.</param>
/// <param name="dataHubPort">The port number for the local datahub service.</param>
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="textFormatter">Used to format the logs sent to Logentries.</param>
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
public static LoggerConfiguration Logentries(
this LoggerSinkConfiguration loggerConfiguration,
string dataHubServer,
int dataHubPort,
ITextFormatter textFormatter,
int batchPostingLimit = LogentriesSink.DefaultBatchPostingLimit,
TimeSpan? period = null,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum)
{
if (loggerConfiguration == null)
throw new ArgumentNullException(nameof(loggerConfiguration));

if (string.IsNullOrWhiteSpace(dataHubServer))
throw new ArgumentNullException(nameof(dataHubServer));

if (textFormatter == null)
throw new ArgumentNullException(nameof(textFormatter));

var defaultedPeriod = period ?? LogentriesSink.DefaultPeriod;

return loggerConfiguration.Sink(
new LogentriesSink(textFormatter, token, useSsl, batchPostingLimit, defaultedPeriod, url),
new LogentriesSink(textFormatter, token: null, useSsl: false, batchPostingLimit, defaultedPeriod, dataHubServer, dataHubPort),
restrictedToMinimumLevel);
}
}
Expand Down
19 changes: 2 additions & 17 deletions src/Serilog.Sinks.Logentries/Sinks/Logentries/LeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,11 @@ namespace Serilog.Sinks.Logentries
{
class LeClient
{
// Port number for token logging on Logentries API server.
const int LeApiTokenPort = 80;

// Port number for TLS encrypted token logging on Logentries API server
const int LeApiTokenTlsPort = 443;

// Port number for HTTP PUT logging on Logentries API server.
const int LeApiHttpPort = 80;

// Port number for SSL HTTP PUT logging on Logentries API server.
const int LeApiHttpsPort = 443;

public LeClient(bool useHttpPut, bool useSsl, string url)
public LeClient(bool useHttpPut, bool useSsl, string url, int port)
{
m_UseSsl = useSsl;
_url = url;
if (!m_UseSsl)
m_TcpPort = useHttpPut ? LeApiHttpPort : LeApiTokenPort;
else
m_TcpPort = useHttpPut ? LeApiHttpsPort : LeApiTokenTlsPort;
m_TcpPort = port;
}

bool m_UseSsl;
Expand Down
13 changes: 9 additions & 4 deletions src/Serilog.Sinks.Logentries/Sinks/Logentries/LogentriesSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class LogentriesSink : PeriodicBatchingSink
readonly string _token;
readonly bool _useSsl;
private readonly string _url;
private readonly int _port;
LeClient _client;
readonly ITextFormatter _textFormatter;

Expand Down Expand Up @@ -63,8 +64,9 @@ public class LogentriesSink : PeriodicBatchingSink
/// <param name="token">The input key as found on the Logentries website.</param>
/// <param name="useSsl">Indicates if you want to use SSL or not.</param>
/// <param name="url">Url to logentries</param>
public LogentriesSink(string outputTemplate, IFormatProvider formatProvider, string token, bool useSsl, int batchPostingLimit, TimeSpan period, string url)
: this(new MessageTemplateTextFormatter(outputTemplate, formatProvider), token, useSsl, batchPostingLimit, period, url)
/// <param name="port">Port to logentries</param>
public LogentriesSink(string outputTemplate, IFormatProvider formatProvider, string token, bool useSsl, int batchPostingLimit, TimeSpan period, string url, int port)
: this(new MessageTemplateTextFormatter(outputTemplate, formatProvider), token, useSsl, batchPostingLimit, period, url, port)
{
}

Expand All @@ -76,13 +78,14 @@ public LogentriesSink(string outputTemplate, IFormatProvider formatProvider, str
/// <param name="useSsl">Indicates if you want to use SSL or not.</param>
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
public LogentriesSink(ITextFormatter textFormatter, string token, bool useSsl, int batchPostingLimit, TimeSpan period, string url)
public LogentriesSink(ITextFormatter textFormatter, string token, bool useSsl, int batchPostingLimit, TimeSpan period, string url, int port)
: base(batchPostingLimit, period)
{
_textFormatter = textFormatter ?? throw new ArgumentNullException(nameof(textFormatter));
_token = token;
_useSsl = useSsl;
_url = url;
_port = port;
}

/// <summary>
Expand All @@ -99,7 +102,9 @@ protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
await Task.FromResult(0);

if (_client == null)
_client = new LeClient(false, _useSsl, _url);
{
_client = new LeClient(false, _useSsl, _url, _port);
}

await _client.ConnectAsync().ConfigureAwait(false);

Expand Down