diff --git a/src/Serilog.Sinks.Logentries/LoggerConfigurationLogentriesExtensions.cs b/src/Serilog.Sinks.Logentries/LoggerConfigurationLogentriesExtensions.cs index 4862589..8521076 100644 --- a/src/Serilog.Sinks.Logentries/LoggerConfigurationLogentriesExtensions.cs +++ b/src/Serilog.Sinks.Logentries/LoggerConfigurationLogentriesExtensions.cs @@ -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; + /// /// Adds a sink that writes log events to the Logentries.com webservice. /// Create a token TCP input for this on the logentries website. @@ -48,7 +54,8 @@ public static class LoggerConfigurationLogentriesExtensions /// A required parameter is null. 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, @@ -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); } @@ -84,7 +97,7 @@ public static LoggerConfiguration Logentries( /// A required parameter is null. public static LoggerConfiguration Logentries( this LoggerSinkConfiguration loggerConfiguration, - string token, + string token, ITextFormatter textFormatter, bool useSsl = true, int batchPostingLimit = LogentriesSink.DefaultBatchPostingLimit, @@ -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); + } + /// + /// Adds a sink that writes log events to the LogEntries DataHub local service. + /// + /// The logger configuration. + /// The minimum log event level required in order to write an event to the sink. + /// The local server name of the datahub service. + /// The port number for the local datahub service. + /// A message template describing the format used to write to the sink. + /// the default is "{Timestamp:G} [{Level}] {Message}{NewLine}{Exception}". + /// Supplies culture-specific formatting information, or null. + /// The maximum number of events to post in a single batch. + /// The time to wait between checking for event batches. + /// Logger configuration, allowing configuration to continue. + /// A required parameter is null. + 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); + } + + /// + /// Adds a sink that writes log events to the LogEntries DataHub local service. + /// + /// The logger configuration. + /// The local server name of the datahub service. + /// The port number for the local datahub service. + /// The minimum log event level required in order to write an event to the sink. + /// Used to format the logs sent to Logentries. + /// The maximum number of events to post in a single batch. + /// The time to wait between checking for event batches. + /// Logger configuration, allowing configuration to continue. + /// A required parameter is null. + 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); } } diff --git a/src/Serilog.Sinks.Logentries/Sinks/Logentries/LeClient.cs b/src/Serilog.Sinks.Logentries/Sinks/Logentries/LeClient.cs index 1c78d63..2dc8624 100644 --- a/src/Serilog.Sinks.Logentries/Sinks/Logentries/LeClient.cs +++ b/src/Serilog.Sinks.Logentries/Sinks/Logentries/LeClient.cs @@ -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; diff --git a/src/Serilog.Sinks.Logentries/Sinks/Logentries/LogentriesSink.cs b/src/Serilog.Sinks.Logentries/Sinks/Logentries/LogentriesSink.cs index 3c0957d..6bd0221 100644 --- a/src/Serilog.Sinks.Logentries/Sinks/Logentries/LogentriesSink.cs +++ b/src/Serilog.Sinks.Logentries/Sinks/Logentries/LogentriesSink.cs @@ -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; @@ -63,8 +64,9 @@ public class LogentriesSink : PeriodicBatchingSink /// The input key as found on the Logentries website. /// Indicates if you want to use SSL or not. /// Url to logentries - 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) + /// Port to logentries + 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) { } @@ -76,13 +78,14 @@ public LogentriesSink(string outputTemplate, IFormatProvider formatProvider, str /// Indicates if you want to use SSL or not. /// The maximum number of events to post in a single batch. /// The time to wait between checking for event batches. - 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; } /// @@ -99,7 +102,9 @@ protected override async Task EmitBatchAsync(IEnumerable 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);