Skip to content

Commit bb7cb14

Browse files
authored
Clean up the logging style for HttpConnectionManager (#1819)
- Remove nameof usage
1 parent 82bda4a commit bb7cb14

File tree

3 files changed

+79
-77
lines changed

3 files changed

+79
-77
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace Microsoft.AspNetCore.Http.Connections
8+
{
9+
public partial class HttpConnectionManager
10+
{
11+
private static class Log
12+
{
13+
private static readonly Action<ILogger, string, Exception> _createdNewConnection =
14+
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(1, "CreatedNewConnection"), "New connection {TransportConnectionId} created.");
15+
16+
private static readonly Action<ILogger, string, Exception> _removedConnection =
17+
LoggerMessage.Define<string>(LogLevel.Debug, new EventId(2, "RemovedConnection"), "Removing connection {TransportConnectionId} from the list of connections.");
18+
19+
private static readonly Action<ILogger, string, Exception> _failedDispose =
20+
LoggerMessage.Define<string>(LogLevel.Error, new EventId(3, "FailedDispose"), "Failed disposing connection {TransportConnectionId}.");
21+
22+
private static readonly Action<ILogger, string, Exception> _connectionReset =
23+
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(4, "ConnectionReset"), "Connection {TransportConnectionId} was reset.");
24+
25+
private static readonly Action<ILogger, string, Exception> _connectionTimedOut =
26+
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(5, "ConnectionTimedOut"), "Connection {TransportConnectionId} timed out.");
27+
28+
private static readonly Action<ILogger, Exception> _scanningConnections =
29+
LoggerMessage.Define(LogLevel.Trace, new EventId(6, "ScanningConnections"), "Scanning connections.");
30+
31+
private static readonly Action<ILogger, TimeSpan, Exception> _scannedConnections =
32+
LoggerMessage.Define<TimeSpan>(LogLevel.Trace, new EventId(7, "ScannedConnections"), "Scanned connections in {Duration}.");
33+
34+
public static void CreatedNewConnection(ILogger logger, string connectionId)
35+
{
36+
_createdNewConnection(logger, connectionId, null);
37+
}
38+
39+
public static void RemovedConnection(ILogger logger, string connectionId)
40+
{
41+
_removedConnection(logger, connectionId, null);
42+
}
43+
44+
public static void FailedDispose(ILogger logger, string connectionId, Exception exception)
45+
{
46+
_failedDispose(logger, connectionId, exception);
47+
}
48+
49+
public static void ConnectionTimedOut(ILogger logger, string connectionId)
50+
{
51+
_connectionTimedOut(logger, connectionId, null);
52+
}
53+
54+
public static void ConnectionReset(ILogger logger, string connectionId, Exception exception)
55+
{
56+
_connectionReset(logger, connectionId, exception);
57+
}
58+
59+
public static void ScanningConnections(ILogger logger)
60+
{
61+
_scanningConnections(logger, null);
62+
}
63+
64+
public static void ScannedConnections(ILogger logger, TimeSpan duration)
65+
{
66+
_scannedConnections(logger, duration, null);
67+
}
68+
}
69+
}
70+
}

src/Microsoft.AspNetCore.Http.Connections/HttpConnectionManager.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
namespace Microsoft.AspNetCore.Http.Connections
2121
{
22-
public class HttpConnectionManager
22+
public partial class HttpConnectionManager
2323
{
2424
// TODO: Consider making this configurable? At least for testing?
2525
private static readonly TimeSpan _heartbeatTickRate = TimeSpan.FromSeconds(1);
@@ -75,7 +75,7 @@ public HttpConnectionContext CreateConnection()
7575
{
7676
var id = MakeNewConnectionId();
7777

78-
_logger.CreatedNewConnection(id);
78+
Log.CreatedNewConnection(_logger, id);
7979
var connectionTimer = HttpConnectionsEventSource.Log.ConnectionStart(id);
8080

8181
var connection = new HttpConnectionContext(id);
@@ -100,7 +100,7 @@ public void RemoveConnection(string id)
100100
{
101101
// Remove the connection completely
102102
HttpConnectionsEventSource.Log.ConnectionStop(id, pair.Timer);
103-
_logger.RemovedConnection(id);
103+
Log.RemovedConnection(_logger, id);
104104
}
105105
}
106106

@@ -143,7 +143,7 @@ public void Scan()
143143
// Time the scan so we know if it gets slower than 1sec
144144
var timer = ValueStopwatch.StartNew();
145145
HttpConnectionsEventSource.Log.ScanningConnections();
146-
_logger.ScanningConnections();
146+
Log.ScanningConnections(_logger);
147147

148148
// Scan the registered connections looking for ones that have timed out
149149
foreach (var c in _connections)
@@ -170,7 +170,7 @@ public void Scan()
170170
// But don't clean up connections while the debugger is attached.
171171
if (!Debugger.IsAttached && status == HttpConnectionContext.ConnectionStatus.Inactive && (DateTimeOffset.UtcNow - lastSeenUtc).TotalSeconds > 5)
172172
{
173-
_logger.ConnectionTimedOut(connection.ConnectionId);
173+
Log.ConnectionTimedOut(_logger, connection.ConnectionId);
174174
HttpConnectionsEventSource.Log.ConnectionTimedOut(connection.ConnectionId);
175175
var ignore = DisposeAndRemoveAsync(connection);
176176
}
@@ -184,7 +184,7 @@ public void Scan()
184184
// 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.
185185
var elapsed = timer.GetElapsedTime();
186186
HttpConnectionsEventSource.Log.ScannedConnections(elapsed);
187-
_logger.ScannedConnections(elapsed);
187+
Log.ScannedConnections(_logger, elapsed);
188188

189189
// Resume once we finished processing all connections
190190
_timer.Change(_heartbeatTickRate, _heartbeatTickRate);
@@ -229,15 +229,15 @@ public async Task DisposeAndRemoveAsync(HttpConnectionContext connection)
229229
}
230230
catch (IOException ex)
231231
{
232-
_logger.ConnectionReset(connection.ConnectionId, ex);
232+
Log.ConnectionReset(_logger, connection.ConnectionId, ex);
233233
}
234234
catch (WebSocketException ex) when (ex.InnerException is IOException)
235235
{
236-
_logger.ConnectionReset(connection.ConnectionId, ex);
236+
Log.ConnectionReset(_logger, connection.ConnectionId, ex);
237237
}
238238
catch (Exception ex)
239239
{
240-
_logger.FailedDispose(connection.ConnectionId, ex);
240+
Log.FailedDispose(_logger, connection.ConnectionId, ex);
241241
}
242242
finally
243243
{

src/Microsoft.AspNetCore.Http.Connections/Internal/SocketLoggerExtensions.cs

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)