diff --git a/src/Microsoft.AspNetCore.Http.Connections/HttpConnectionManager.Log.cs b/src/Microsoft.AspNetCore.Http.Connections/HttpConnectionManager.Log.cs new file mode 100644 index 0000000000..fb2851c25f --- /dev/null +++ b/src/Microsoft.AspNetCore.Http.Connections/HttpConnectionManager.Log.cs @@ -0,0 +1,70 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.Extensions.Logging; + +namespace Microsoft.AspNetCore.Http.Connections +{ + public partial class HttpConnectionManager + { + private static class Log + { + private static readonly Action _createdNewConnection = + LoggerMessage.Define(LogLevel.Debug, new EventId(1, "CreatedNewConnection"), "New connection {TransportConnectionId} created."); + + private static readonly Action _removedConnection = + LoggerMessage.Define(LogLevel.Debug, new EventId(2, "RemovedConnection"), "Removing connection {TransportConnectionId} from the list of connections."); + + private static readonly Action _failedDispose = + LoggerMessage.Define(LogLevel.Error, new EventId(3, "FailedDispose"), "Failed disposing connection {TransportConnectionId}."); + + private static readonly Action _connectionReset = + LoggerMessage.Define(LogLevel.Trace, new EventId(4, "ConnectionReset"), "Connection {TransportConnectionId} was reset."); + + private static readonly Action _connectionTimedOut = + LoggerMessage.Define(LogLevel.Trace, new EventId(5, "ConnectionTimedOut"), "Connection {TransportConnectionId} timed out."); + + private static readonly Action _scanningConnections = + LoggerMessage.Define(LogLevel.Trace, new EventId(6, "ScanningConnections"), "Scanning connections."); + + private static readonly Action _scannedConnections = + LoggerMessage.Define(LogLevel.Trace, new EventId(7, "ScannedConnections"), "Scanned connections in {Duration}."); + + public static void CreatedNewConnection(ILogger logger, string connectionId) + { + _createdNewConnection(logger, connectionId, null); + } + + public static void RemovedConnection(ILogger logger, string connectionId) + { + _removedConnection(logger, connectionId, null); + } + + public static void FailedDispose(ILogger logger, string connectionId, Exception exception) + { + _failedDispose(logger, connectionId, exception); + } + + public static void ConnectionTimedOut(ILogger logger, string connectionId) + { + _connectionTimedOut(logger, connectionId, null); + } + + public static void ConnectionReset(ILogger logger, string connectionId, Exception exception) + { + _connectionReset(logger, connectionId, exception); + } + + public static void ScanningConnections(ILogger logger) + { + _scanningConnections(logger, null); + } + + public static void ScannedConnections(ILogger logger, TimeSpan duration) + { + _scannedConnections(logger, duration, null); + } + } + } +} diff --git a/src/Microsoft.AspNetCore.Http.Connections/HttpConnectionManager.cs b/src/Microsoft.AspNetCore.Http.Connections/HttpConnectionManager.cs index d3b9de1b3f..0de21b7b8f 100644 --- a/src/Microsoft.AspNetCore.Http.Connections/HttpConnectionManager.cs +++ b/src/Microsoft.AspNetCore.Http.Connections/HttpConnectionManager.cs @@ -19,7 +19,7 @@ using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.Http.Connections { - public class HttpConnectionManager + public partial class HttpConnectionManager { // TODO: Consider making this configurable? At least for testing? private static readonly TimeSpan _heartbeatTickRate = TimeSpan.FromSeconds(1); @@ -75,7 +75,7 @@ namespace Microsoft.AspNetCore.Http.Connections { var id = MakeNewConnectionId(); - _logger.CreatedNewConnection(id); + Log.CreatedNewConnection(_logger, id); var connectionTimer = HttpConnectionsEventSource.Log.ConnectionStart(id); var connection = new HttpConnectionContext(id); @@ -100,7 +100,7 @@ namespace Microsoft.AspNetCore.Http.Connections { // Remove the connection completely HttpConnectionsEventSource.Log.ConnectionStop(id, pair.Timer); - _logger.RemovedConnection(id); + Log.RemovedConnection(_logger, id); } } @@ -143,7 +143,7 @@ namespace Microsoft.AspNetCore.Http.Connections // Time the scan so we know if it gets slower than 1sec var timer = ValueStopwatch.StartNew(); HttpConnectionsEventSource.Log.ScanningConnections(); - _logger.ScanningConnections(); + Log.ScanningConnections(_logger); // Scan the registered connections looking for ones that have timed out foreach (var c in _connections) @@ -170,7 +170,7 @@ namespace Microsoft.AspNetCore.Http.Connections // But don't clean up connections while the debugger is attached. if (!Debugger.IsAttached && status == HttpConnectionContext.ConnectionStatus.Inactive && (DateTimeOffset.UtcNow - lastSeenUtc).TotalSeconds > 5) { - _logger.ConnectionTimedOut(connection.ConnectionId); + Log.ConnectionTimedOut(_logger, connection.ConnectionId); HttpConnectionsEventSource.Log.ConnectionTimedOut(connection.ConnectionId); var ignore = DisposeAndRemoveAsync(connection); } @@ -184,7 +184,7 @@ namespace Microsoft.AspNetCore.Http.Connections // TODO: We could use this timer to determine if the connection scanner is too slow, but we need an idea of what "too slow" is. var elapsed = timer.GetElapsedTime(); HttpConnectionsEventSource.Log.ScannedConnections(elapsed); - _logger.ScannedConnections(elapsed); + Log.ScannedConnections(_logger, elapsed); // Resume once we finished processing all connections _timer.Change(_heartbeatTickRate, _heartbeatTickRate); @@ -229,15 +229,15 @@ namespace Microsoft.AspNetCore.Http.Connections } catch (IOException ex) { - _logger.ConnectionReset(connection.ConnectionId, ex); + Log.ConnectionReset(_logger, connection.ConnectionId, ex); } catch (WebSocketException ex) when (ex.InnerException is IOException) { - _logger.ConnectionReset(connection.ConnectionId, ex); + Log.ConnectionReset(_logger, connection.ConnectionId, ex); } catch (Exception ex) { - _logger.FailedDispose(connection.ConnectionId, ex); + Log.FailedDispose(_logger, connection.ConnectionId, ex); } finally { diff --git a/src/Microsoft.AspNetCore.Http.Connections/Internal/SocketLoggerExtensions.cs b/src/Microsoft.AspNetCore.Http.Connections/Internal/SocketLoggerExtensions.cs deleted file mode 100644 index f58d0f5beb..0000000000 --- a/src/Microsoft.AspNetCore.Http.Connections/Internal/SocketLoggerExtensions.cs +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.Extensions.Logging; - -namespace Microsoft.AspNetCore.Http.Connections.Internal -{ - internal static class SocketLoggerExtensions - { - // Category: ConnectionManager - private static readonly Action _createdNewConnection = - LoggerMessage.Define(LogLevel.Debug, new EventId(1, nameof(CreatedNewConnection)), "New connection {TransportConnectionId} created."); - - private static readonly Action _removedConnection = - LoggerMessage.Define(LogLevel.Debug, new EventId(2, nameof(RemovedConnection)), "Removing connection {TransportConnectionId} from the list of connections."); - - private static readonly Action _failedDispose = - LoggerMessage.Define(LogLevel.Error, new EventId(3, nameof(FailedDispose)), "Failed disposing connection {TransportConnectionId}."); - - private static readonly Action _connectionReset = - LoggerMessage.Define(LogLevel.Trace, new EventId(4, nameof(ConnectionReset)), "Connection {TransportConnectionId} was reset."); - - private static readonly Action _connectionTimedOut = - LoggerMessage.Define(LogLevel.Trace, new EventId(5, nameof(ConnectionTimedOut)), "Connection {TransportConnectionId} timed out."); - - private static readonly Action _scanningConnections = - LoggerMessage.Define(LogLevel.Trace, new EventId(6, nameof(ScanningConnections)), "Scanning connections."); - - private static readonly Action _scannedConnections = - LoggerMessage.Define(LogLevel.Trace, new EventId(7, nameof(ScannedConnections)), "Scanned connections in {Duration}."); - - public static void CreatedNewConnection(this ILogger logger, string connectionId) - { - _createdNewConnection(logger, connectionId, null); - } - - public static void RemovedConnection(this ILogger logger, string connectionId) - { - _removedConnection(logger, connectionId, null); - } - - public static void FailedDispose(this ILogger logger, string connectionId, Exception exception) - { - _failedDispose(logger, connectionId, exception); - } - - public static void ConnectionTimedOut(this ILogger logger, string connectionId) - { - _connectionTimedOut(logger, connectionId, null); - } - - public static void ConnectionReset(this ILogger logger, string connectionId, Exception exception) - { - _connectionReset(logger, connectionId, exception); - } - - public static void ScanningConnections(this ILogger logger) - { - _scanningConnections(logger, null); - } - - public static void ScannedConnections(this ILogger logger, TimeSpan duration) - { - _scannedConnections(logger, duration, null); - } - } -}