Skip to content

Commit

Permalink
fix(logging): use serializable objects when logging network clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Fresa committed Sep 28, 2021
1 parent 61cf73c commit 3db4b9e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
10 changes: 6 additions & 4 deletions Test.It.With.Amqp/NetworkClient/NetworkClientServer.cs
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.ExceptionServices;
using System.Threading;
Expand Down Expand Up @@ -45,10 +46,11 @@ private void StartAcceptingClients(CancellationToken cancellation)
var clientSocket = await _clientAcceptingSocket
.AcceptAsync()
.ConfigureAwait(false);
Logger.Debug("Client connected {@clientSocket}", clientSocket);
_waitingClients.Enqueue(
new SocketNetworkClient(clientSocket));
var networkClient = new SocketNetworkClient(clientSocket);
Logger.Debug("Client connected {@clientSocket}", networkClient.Serialize());
_waitingClients.Enqueue(networkClient);
_clientAvailable.Release();
}
catch when (cts.IsCancellationRequested)
Expand All @@ -66,7 +68,7 @@ await _clientAvailable
.ConfigureAwait(false);

_waitingClients.TryDequeue(out var client);
Logger.Debug("Client accepted {@client}", client);
Logger.Debug("Client accepted {@client}", client.Serialize());
_clients.Enqueue(client);
return client;
}
Expand Down
18 changes: 18 additions & 0 deletions Test.It.With.Amqp/NetworkClient/SocketNetworkClient.cs
@@ -1,4 +1,5 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -9,12 +10,29 @@ namespace Test.It.With.Amqp.NetworkClient
internal sealed class SocketNetworkClient : IStartableNetworkClient
{
private readonly Socket _socket;
private object _serialized;

public SocketNetworkClient(Socket socket)
{
_socket = socket;
}

internal object Serialize()
{
if (_serialized != null) return _serialized;

var localAddress = _socket.LocalEndPoint as IPEndPoint;
_serialized = new
{
_socket.AddressFamily,
_socket.ProtocolType,
IPAddress = localAddress?.Address.ToString(),
localAddress?.Port
};

return _serialized;
}

public void Dispose()
{
try
Expand Down
8 changes: 7 additions & 1 deletion Test.It.With.Amqp/NetworkClient/SocketServer.cs
Expand Up @@ -42,7 +42,13 @@ private void ConnectAcceptingSocket(IPAddress address, int port)
Port = localEndPoint.Port;
Address = localEndPoint.Address;
_clientAcceptingSocket.Listen(100);
Logger.Info("Listening on {@endpoint}", localEndPoint);
Logger.Info("Listening on {@endpoint}", new
{
IPAddress = localEndPoint.Address.ToString(),
localEndPoint.Port,
localEndPoint.AddressFamily,
_clientAcceptingSocket.ProtocolType
});
}

public ValueTask DisposeAsync()
Expand Down

0 comments on commit 3db4b9e

Please sign in to comment.