From 042e79c797ae29f1ec7ed641532594362ffc16c8 Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Mon, 12 Sep 2022 12:33:21 +1000 Subject: [PATCH] Limit the default pooled connection lifetime on .NET 5 and greater --- src/Serilog.Sinks.Seq/Serilog.Sinks.Seq.csproj | 2 +- .../Sinks/Seq/Http/SeqIngestionApiClient.cs | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Serilog.Sinks.Seq/Serilog.Sinks.Seq.csproj b/src/Serilog.Sinks.Seq/Serilog.Sinks.Seq.csproj index 2913c25..fbebe43 100644 --- a/src/Serilog.Sinks.Seq/Serilog.Sinks.Seq.csproj +++ b/src/Serilog.Sinks.Seq/Serilog.Sinks.Seq.csproj @@ -36,7 +36,7 @@ - $(DefineConstants);DURABLE;THREADING_TIMER;WRITE_ALL_BYTES_ASYNC + $(DefineConstants);DURABLE;THREADING_TIMER;WRITE_ALL_BYTES_ASYNC;SOCKETS_HTTP_HANDLER_ALWAYS_DEFAULT diff --git a/src/Serilog.Sinks.Seq/Sinks/Seq/Http/SeqIngestionApiClient.cs b/src/Serilog.Sinks.Seq/Sinks/Seq/Http/SeqIngestionApiClient.cs index a043475..845c40d 100644 --- a/src/Serilog.Sinks.Seq/Sinks/Seq/Http/SeqIngestionApiClient.cs +++ b/src/Serilog.Sinks.Seq/Sinks/Seq/Http/SeqIngestionApiClient.cs @@ -36,7 +36,23 @@ public SeqIngestionApiClient(string serverUrl, string? apiKey, HttpMessageHandle { if (serverUrl == null) throw new ArgumentNullException(nameof(serverUrl)); _apiKey = apiKey; - _httpClient = messageHandler != null ? new HttpClient(messageHandler) : new HttpClient(); + _httpClient = messageHandler != null + ? new HttpClient(messageHandler) + : +#if SOCKETS_HTTP_HANDLER_ALWAYS_DEFAULT + new HttpClient(new SocketsHttpHandler + { + // The default value is infinite; this causes problems for long-running processes if DNS changes + // require that the Seq API be accessed at a different IP address. Setting a timeout here puts + // an upper bound on the duration of DNS-related outages, while hopefully incurring only infrequent + // connection reestablishment costs. + PooledConnectionLifetime = TimeSpan.FromMinutes(5) + }) +#else + new HttpClient() +#endif + ; + _httpClient.BaseAddress = new Uri(NormalizeServerBaseAddress(serverUrl)); }