diff --git a/src/Serilog.Sinks.Datadog.Logs/Configuration/Implementations/Microsoft.Extensions.Configuration/ApplyMicrosoftExtensionsConfiguration.cs b/src/Serilog.Sinks.Datadog.Logs/Configuration/Implementations/Microsoft.Extensions.Configuration/ApplyMicrosoftExtensionsConfiguration.cs index 66ebc94..b1a0535 100644 --- a/src/Serilog.Sinks.Datadog.Logs/Configuration/Implementations/Microsoft.Extensions.Configuration/ApplyMicrosoftExtensionsConfiguration.cs +++ b/src/Serilog.Sinks.Datadog.Logs/Configuration/Implementations/Microsoft.Extensions.Configuration/ApplyMicrosoftExtensionsConfiguration.cs @@ -17,19 +17,20 @@ internal static class ApplyMicrosoftExtensionsConfiguration /// Create the DatadogConfiguration object or apply any configuration changes to it. /// /// An optional externally-created DatadogConfiguration object to be updated with additional configuration values. - /// A configuration section typically named "configurationSection". + /// A configuration section typically named "configurationSection". /// The "merged" DatadogConfiguration object. - internal static DatadogConfiguration ConfigureDatadogConfiguration(DatadogConfiguration datadogConfiguration, IConfigurationSection configurationOption) + internal static DatadogConfiguration ConfigureDatadogConfiguration(DatadogConfiguration datadogConfiguration, IConfigurationSection configurationSection) { - if (configurationOption == null || !configurationOption.GetChildren().Any()) return datadogConfiguration ?? new DatadogConfiguration(); + if (configurationSection == null || !configurationSection.GetChildren().Any()) return datadogConfiguration ?? new DatadogConfiguration(); - var section = configurationOption.Get(); + var section = configurationSection.Get(); return new DatadogConfiguration( url: datadogConfiguration?.Url ?? section.Url, port: datadogConfiguration?.Port ?? section.Port, useSSL: datadogConfiguration?.UseSSL ?? section.UseSSL, - useTCP: datadogConfiguration?.UseTCP ?? section.UseTCP + useTCP: datadogConfiguration?.UseTCP ?? section.UseTCP, + maxRetries:datadogConfiguration?.MaxRetries ?? section.MaxRetries ); } } diff --git a/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/DatadogConfiguration.cs b/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/DatadogConfiguration.cs index eee0f32..eb364b2 100644 --- a/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/DatadogConfiguration.cs +++ b/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/DatadogConfiguration.cs @@ -45,20 +45,23 @@ public class DatadogConfiguration /// public bool UseTCP { get; set; } + /// + /// Number of retries before the client gives up logging. + /// + public int MaxRetries { get; set; } + public DatadogConfiguration() : this(DDUrl, DDPort, true, false) { } - public DatadogConfiguration(string url = DDUrl, int port = DDPort, bool useSSL = true, bool useTCP = false) + public DatadogConfiguration(string url = DDUrl, int port = DDPort, bool useSSL = true, bool useTCP = false, int maxRetries = 10) { Url = url; Port = port; UseSSL = useSSL; UseTCP = useTCP; + MaxRetries = maxRetries; } - public override string ToString() - { - return string.Format("{{ Url: {0}, Port: {1}, UseSSL: {2}, UseTCP: {3} }}", Url, Port, UseSSL, UseTCP); - } + public override string ToString() => $"{{ Url: {Url}, Port: {Port}, UseSSL: {UseSSL}, UseTCP: {UseTCP} }}"; } } diff --git a/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/DatadogHttpClient.cs b/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/DatadogHttpClient.cs index 3de4a7d..b544263 100644 --- a/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/DatadogHttpClient.cs +++ b/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/DatadogHttpClient.cs @@ -22,22 +22,19 @@ public class DatadogHttpClient : IDatadogClient private readonly string _url; private readonly DatadogLogRenderer _renderer; private readonly HttpClient _client; - - /// - /// Max number of retries when sending failed. - /// - private const int MaxRetries = 10; + private readonly int _maxRetries; /// /// Max backoff used when sending failed. /// private const int MaxBackoff = 30; - public DatadogHttpClient(string url, DatadogLogRenderer renderer, HttpClient client) + public DatadogHttpClient(string url, DatadogLogRenderer renderer, HttpClient client, int maxRetries) { _url = url; _renderer = renderer; _client = client; + _maxRetries = maxRetries; } public Task WriteAsync(IEnumerable events) @@ -79,7 +76,7 @@ private async Task Post(JsonPayloadBuilder payloadBuilder) var content = new StringContent(payload, Encoding.UTF8, _content); HttpResponseMessage lastResult = null; Exception lastException = null; - for (int retry = 0; retry < MaxRetries; retry++) + for (int retry = 0; retry < _maxRetries; retry++) { int backoff = (int)Math.Min(Math.Pow(2, retry), MaxBackoff); if (retry > 0) diff --git a/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/DatadogSink.cs b/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/DatadogSink.cs index 0f7b049..271d100 100755 --- a/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/DatadogSink.cs +++ b/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/DatadogSink.cs @@ -140,7 +140,7 @@ private static IDatadogClient CreateDatadogClient(string apiKey, DatadogLogRende else { var httpIntakeClient = new DatadogHttpIntakeClient(apiKey); - return new DatadogHttpClient($"{configuration.Url}/api/v2/logs", renderer, httpIntakeClient); + return new DatadogHttpClient($"{configuration.Url}/api/v2/logs", renderer, httpIntakeClient, configuration.MaxRetries); } } diff --git a/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/DatadogTcpClient.cs b/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/DatadogTcpClient.cs index 84335ed..25f405c 100644 --- a/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/DatadogTcpClient.cs +++ b/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/DatadogTcpClient.cs @@ -42,11 +42,6 @@ public class DatadogTcpClient : IDatadogClient /// private const string MessageDelimiter = "\n"; - /// - /// Max number of retries when sending failed. - /// - private const int MaxRetries = 5; - /// /// Max backoff used when sending failed. /// @@ -102,7 +97,7 @@ public async Task WriteAsync(IEnumerable events) } string payload = payloadBuilder.ToString(); var dataSent = false; - for (int retry = 0; retry < MaxRetries; retry++) + for (int retry = 0; retry < _config.MaxRetries; retry++) { int backoff = (int)Math.Min(Math.Pow(retry, 2), MaxBackoff); if (retry > 0) diff --git a/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/IDatadogClient.cs b/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/IDatadogClient.cs index 11211fc..6c8e775 100644 --- a/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/IDatadogClient.cs +++ b/src/Serilog.Sinks.Datadog.Logs/Sinks/Datadog/IDatadogClient.cs @@ -3,7 +3,6 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). // Copyright 2019 Datadog, Inc. -using System; using System.Collections.Generic; using System.Threading.Tasks; using Serilog.Events;