Clean up the logging style for HttpConnectionManager (#1819)
- Remove nameof usage
This commit is contained in:
parent
82bda4a9c8
commit
bb7cb14a1c
|
|
@ -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<ILogger, string, Exception> _createdNewConnection =
|
||||||
|
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(1, "CreatedNewConnection"), "New connection {TransportConnectionId} created.");
|
||||||
|
|
||||||
|
private static readonly Action<ILogger, string, Exception> _removedConnection =
|
||||||
|
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(2, "RemovedConnection"), "Removing connection {TransportConnectionId} from the list of connections.");
|
||||||
|
|
||||||
|
private static readonly Action<ILogger, string, Exception> _failedDispose =
|
||||||
|
LoggerMessage.Define<string>(LogLevel.Error, new EventId(3, "FailedDispose"), "Failed disposing connection {TransportConnectionId}.");
|
||||||
|
|
||||||
|
private static readonly Action<ILogger, string, Exception> _connectionReset =
|
||||||
|
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(4, "ConnectionReset"), "Connection {TransportConnectionId} was reset.");
|
||||||
|
|
||||||
|
private static readonly Action<ILogger, string, Exception> _connectionTimedOut =
|
||||||
|
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(5, "ConnectionTimedOut"), "Connection {TransportConnectionId} timed out.");
|
||||||
|
|
||||||
|
private static readonly Action<ILogger, Exception> _scanningConnections =
|
||||||
|
LoggerMessage.Define(LogLevel.Trace, new EventId(6, "ScanningConnections"), "Scanning connections.");
|
||||||
|
|
||||||
|
private static readonly Action<ILogger, TimeSpan, Exception> _scannedConnections =
|
||||||
|
LoggerMessage.Define<TimeSpan>(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -19,7 +19,7 @@ using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Http.Connections
|
namespace Microsoft.AspNetCore.Http.Connections
|
||||||
{
|
{
|
||||||
public class HttpConnectionManager
|
public partial class HttpConnectionManager
|
||||||
{
|
{
|
||||||
// TODO: Consider making this configurable? At least for testing?
|
// TODO: Consider making this configurable? At least for testing?
|
||||||
private static readonly TimeSpan _heartbeatTickRate = TimeSpan.FromSeconds(1);
|
private static readonly TimeSpan _heartbeatTickRate = TimeSpan.FromSeconds(1);
|
||||||
|
|
@ -75,7 +75,7 @@ namespace Microsoft.AspNetCore.Http.Connections
|
||||||
{
|
{
|
||||||
var id = MakeNewConnectionId();
|
var id = MakeNewConnectionId();
|
||||||
|
|
||||||
_logger.CreatedNewConnection(id);
|
Log.CreatedNewConnection(_logger, id);
|
||||||
var connectionTimer = HttpConnectionsEventSource.Log.ConnectionStart(id);
|
var connectionTimer = HttpConnectionsEventSource.Log.ConnectionStart(id);
|
||||||
|
|
||||||
var connection = new HttpConnectionContext(id);
|
var connection = new HttpConnectionContext(id);
|
||||||
|
|
@ -100,7 +100,7 @@ namespace Microsoft.AspNetCore.Http.Connections
|
||||||
{
|
{
|
||||||
// Remove the connection completely
|
// Remove the connection completely
|
||||||
HttpConnectionsEventSource.Log.ConnectionStop(id, pair.Timer);
|
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
|
// Time the scan so we know if it gets slower than 1sec
|
||||||
var timer = ValueStopwatch.StartNew();
|
var timer = ValueStopwatch.StartNew();
|
||||||
HttpConnectionsEventSource.Log.ScanningConnections();
|
HttpConnectionsEventSource.Log.ScanningConnections();
|
||||||
_logger.ScanningConnections();
|
Log.ScanningConnections(_logger);
|
||||||
|
|
||||||
// Scan the registered connections looking for ones that have timed out
|
// Scan the registered connections looking for ones that have timed out
|
||||||
foreach (var c in _connections)
|
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.
|
// But don't clean up connections while the debugger is attached.
|
||||||
if (!Debugger.IsAttached && status == HttpConnectionContext.ConnectionStatus.Inactive && (DateTimeOffset.UtcNow - lastSeenUtc).TotalSeconds > 5)
|
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);
|
HttpConnectionsEventSource.Log.ConnectionTimedOut(connection.ConnectionId);
|
||||||
var ignore = DisposeAndRemoveAsync(connection);
|
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.
|
// 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();
|
var elapsed = timer.GetElapsedTime();
|
||||||
HttpConnectionsEventSource.Log.ScannedConnections(elapsed);
|
HttpConnectionsEventSource.Log.ScannedConnections(elapsed);
|
||||||
_logger.ScannedConnections(elapsed);
|
Log.ScannedConnections(_logger, elapsed);
|
||||||
|
|
||||||
// Resume once we finished processing all connections
|
// Resume once we finished processing all connections
|
||||||
_timer.Change(_heartbeatTickRate, _heartbeatTickRate);
|
_timer.Change(_heartbeatTickRate, _heartbeatTickRate);
|
||||||
|
|
@ -229,15 +229,15 @@ namespace Microsoft.AspNetCore.Http.Connections
|
||||||
}
|
}
|
||||||
catch (IOException ex)
|
catch (IOException ex)
|
||||||
{
|
{
|
||||||
_logger.ConnectionReset(connection.ConnectionId, ex);
|
Log.ConnectionReset(_logger, connection.ConnectionId, ex);
|
||||||
}
|
}
|
||||||
catch (WebSocketException ex) when (ex.InnerException is IOException)
|
catch (WebSocketException ex) when (ex.InnerException is IOException)
|
||||||
{
|
{
|
||||||
_logger.ConnectionReset(connection.ConnectionId, ex);
|
Log.ConnectionReset(_logger, connection.ConnectionId, ex);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.FailedDispose(connection.ConnectionId, ex);
|
Log.FailedDispose(_logger, connection.ConnectionId, ex);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -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<ILogger, string, Exception> _createdNewConnection =
|
|
||||||
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(1, nameof(CreatedNewConnection)), "New connection {TransportConnectionId} created.");
|
|
||||||
|
|
||||||
private static readonly Action<ILogger, string, Exception> _removedConnection =
|
|
||||||
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(2, nameof(RemovedConnection)), "Removing connection {TransportConnectionId} from the list of connections.");
|
|
||||||
|
|
||||||
private static readonly Action<ILogger, string, Exception> _failedDispose =
|
|
||||||
LoggerMessage.Define<string>(LogLevel.Error, new EventId(3, nameof(FailedDispose)), "Failed disposing connection {TransportConnectionId}.");
|
|
||||||
|
|
||||||
private static readonly Action<ILogger, string, Exception> _connectionReset =
|
|
||||||
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(4, nameof(ConnectionReset)), "Connection {TransportConnectionId} was reset.");
|
|
||||||
|
|
||||||
private static readonly Action<ILogger, string, Exception> _connectionTimedOut =
|
|
||||||
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(5, nameof(ConnectionTimedOut)), "Connection {TransportConnectionId} timed out.");
|
|
||||||
|
|
||||||
private static readonly Action<ILogger, Exception> _scanningConnections =
|
|
||||||
LoggerMessage.Define(LogLevel.Trace, new EventId(6, nameof(ScanningConnections)), "Scanning connections.");
|
|
||||||
|
|
||||||
private static readonly Action<ILogger, TimeSpan, Exception> _scannedConnections =
|
|
||||||
LoggerMessage.Define<TimeSpan>(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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue