From 62f14054ebe3f2dd2b7e7faedcd898322efdbe19 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Mon, 16 Apr 2018 10:41:35 +1200 Subject: [PATCH] Capture and log prematurely closed connections --- .../Internal/Transports/WebSocketsTransport.Log.cs | 7 +++++++ .../Internal/Transports/WebSocketsTransport.cs | 9 +++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.AspNetCore.Http.Connections/Internal/Transports/WebSocketsTransport.Log.cs b/src/Microsoft.AspNetCore.Http.Connections/Internal/Transports/WebSocketsTransport.Log.cs index 13df2672c6..29067a65b5 100644 --- a/src/Microsoft.AspNetCore.Http.Connections/Internal/Transports/WebSocketsTransport.Log.cs +++ b/src/Microsoft.AspNetCore.Http.Connections/Internal/Transports/WebSocketsTransport.Log.cs @@ -50,6 +50,9 @@ namespace Microsoft.AspNetCore.Http.Connections.Internal.Transports private static readonly Action _sendFailed = LoggerMessage.Define(LogLevel.Error, new EventId(13, "SendFailed"), "Socket failed to send."); + private static readonly Action _closedPrematurely = + LoggerMessage.Define(LogLevel.Debug, new EventId(14, "ClosedPrematurely"), "Socket connection closed prematurely."); + public static void SocketOpened(ILogger logger, string subProtocol) { _socketOpened(logger, subProtocol, null); @@ -115,6 +118,10 @@ namespace Microsoft.AspNetCore.Http.Connections.Internal.Transports _sendFailed(logger, ex); } + public static void ClosedPrematurely(ILogger logger, Exception ex) + { + _closedPrematurely(logger, ex); + } } } } diff --git a/src/Microsoft.AspNetCore.Http.Connections/Internal/Transports/WebSocketsTransport.cs b/src/Microsoft.AspNetCore.Http.Connections/Internal/Transports/WebSocketsTransport.cs index 3632e29c52..0442ae6a5f 100644 --- a/src/Microsoft.AspNetCore.Http.Connections/Internal/Transports/WebSocketsTransport.cs +++ b/src/Microsoft.AspNetCore.Http.Connections/Internal/Transports/WebSocketsTransport.cs @@ -182,13 +182,10 @@ namespace Microsoft.AspNetCore.Http.Connections.Internal.Transports } } } - catch (WebSocketException ex) when ( - ex.ErrorCode == 997 || - // Sometimes this error is raised without the ErrorCode - ex.Message == "The remote party closed the WebSocket connection without completing the close handshake.") + catch (WebSocketException ex) when (ex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely) { - // The remote party closed the WebSocket connection without completing the close handshake - // Don't long an error for this exception + // Client has closed the WebSocket connection without completing the close handshake + Log.ClosedPrematurely(_logger, ex); } catch (OperationCanceledException) {