diff --git a/src/libp2p/Libp2p.Protocols.Ping/LogMessages.cs b/src/libp2p/Libp2p.Protocols.Ping/LogMessages.cs index 445f6e18..31643ba4 100644 --- a/src/libp2p/Libp2p.Protocols.Ping/LogMessages.cs +++ b/src/libp2p/Libp2p.Protocols.Ping/LogMessages.cs @@ -13,34 +13,38 @@ internal static partial class LogMessages [LoggerMessage( EventId = EventId + 1, EventName = nameof(ReadingPong), - Message = "Reading pong", + Message = "Reading pong {remotePeer}", Level = LogLevel.Trace)] internal static partial void ReadingPong( - this ILogger logger); + this ILogger logger, + Multiaddr remotePeer); [LoggerMessage( EventId = EventId + 2, EventName = nameof(VerifyingPong), - Message = "Verifying pong", + Message = "Verifying pong {remotePeer}", Level = LogLevel.Trace)] internal static partial void VerifyingPong( - this ILogger logger); + this ILogger logger, + Multiaddr remotePeer); [LoggerMessage( EventId = EventId + 3, EventName = nameof(ReadingPing), - Message = "Reading ping", + Message = "Reading ping {remotePeer}", Level = LogLevel.Trace)] internal static partial void ReadingPing( - this ILogger logger); + this ILogger logger, + Multiaddr remotePeer); [LoggerMessage( EventId = EventId + 4, EventName = nameof(ReturningPong), - Message = "Returning pong", + Message = "Returning pong {remotePeer}", Level = LogLevel.Trace)] internal static partial void ReturningPong( - this ILogger logger); + this ILogger logger, + Multiaddr remotePeer); [LoggerMessage( EventId = EventId + 5, @@ -54,10 +58,11 @@ internal static partial void LogPing( [LoggerMessage( EventId = EventId + 6, EventName = nameof(LogPinged), - Message = "Pinged", + Message = "Pinged {remotePeer}", Level = LogLevel.Debug)] internal static partial void LogPinged( - this ILogger logger); + this ILogger logger, + Multiaddr remotePeer); [LoggerMessage( EventId = EventId + 7, @@ -71,10 +76,11 @@ internal static partial void PingListenStarted( [LoggerMessage( EventId = EventId + 8, EventName = nameof(PingFinished), - Message = "Ping finished", + Message = "Ping finished {remotePeer}", Level = LogLevel.Debug)] internal static partial void PingFinished( - this ILogger logger); + this ILogger logger, + Multiaddr remotePeer); [LoggerMessage( EventId = EventId + 9, diff --git a/src/libp2p/Libp2p.Protocols.Ping/PingProtocol.cs b/src/libp2p/Libp2p.Protocols.Ping/PingProtocol.cs index e7cd44d9..3598f64a 100644 --- a/src/libp2p/Libp2p.Protocols.Ping/PingProtocol.cs +++ b/src/libp2p/Libp2p.Protocols.Ping/PingProtocol.cs @@ -15,7 +15,7 @@ public class PingProtocol : IProtocol { private const int PayloadLength = 32; - public string Id => "/ipfs/ping/1.0.0"; // TODO: order in class: fields, constructors, properties, methods? + public string Id => "/ipfs/ping/1.0.0"; private readonly Random _random = new(); private readonly ILogger? _logger; @@ -34,41 +34,35 @@ public async Task DialAsync(IChannel channel, IChannelFactory? channelFactory, _logger?.LogPing(context.RemotePeer.Address); await channel.WriteAsync(bytes); - _logger?.ReadingPong(); + _logger?.ReadingPong(context.RemotePeer.Address); ReadOnlySequence response = await channel.ReadAsync(PayloadLength, ReadBlockingMode.WaitAll); - _logger?.VerifyingPong(); + _logger?.VerifyingPong(context.RemotePeer.Address); if (!byteArray[0..PayloadLength].SequenceEqual(response.ToArray())) { - // TODO: do we need log context.RemotePeer or context.RemotePeer.Address? - // _logger?.LogWarning("Wrong response to ping from {from}", context.RemotePeer); - - // TODO: if we throw exception, probably LogLevel bigger than warning? _logger?.PingFailed(context.RemotePeer.Address); - throw new ApplicationException(); // TODO: add specific exception? + throw new ApplicationException(); } - _logger?.LogPinged(); // TODO: add context.RemotePeer.Address? + _logger?.LogPinged(context.RemotePeer.Address); } public async Task ListenAsync(IChannel channel, IChannelFactory? channelFactory, IPeerContext context) { - // TODO: do we need log context.RemotePeer or context.RemotePeer.Address? - // _logger?.LogDebug("Ping listen started from {remotePeer}", context.RemotePeer); _logger?.PingListenStarted(context.RemotePeer.Address); while (!channel.IsClosed) { - _logger?.ReadingPing(); + _logger?.ReadingPing(context.RemotePeer.Address); ReadOnlySequence request = await channel.ReadAsync(PayloadLength, ReadBlockingMode.WaitAll); - byte[] byteArray = request.ToArray(); // TODO: do we need convert to array and then again to ReadOnlySequence? + byte[] byteArray = request.ToArray(); ReadOnlySequence bytes = new(byteArray); - _logger?.ReturningPong(); + _logger?.ReturningPong(context.RemotePeer.Address); await channel.WriteAsync(bytes); } - _logger?.PingFinished(); + _logger?.PingFinished(context.RemotePeer.Address); } }