diff --git a/src/BootstrapBlazor/Services/TcpSocket/SocketClientOptions.cs b/src/BootstrapBlazor/Services/TcpSocket/SocketClientOptions.cs index 72904adfd72..13eae671abc 100644 --- a/src/BootstrapBlazor/Services/TcpSocket/SocketClientOptions.cs +++ b/src/BootstrapBlazor/Services/TcpSocket/SocketClientOptions.cs @@ -50,4 +50,9 @@ public class SocketClientOptions /// /// This property specifies the local network endpoint that the socket client will bind to when establishing a connection. public IPEndPoint LocalEndPoint { get; set; } = new IPEndPoint(IPAddress.Any, 0); + + /// + /// Gets or sets a value indicating whether logging is enabled. Default value is false. + /// + public bool EnableLog { get; set; } } diff --git a/src/BootstrapBlazor/Services/TcpSocket/TcpSocketClientBase.cs b/src/BootstrapBlazor/Services/TcpSocket/TcpSocketClientBase.cs index 24e563ed1a4..465a366ac80 100644 --- a/src/BootstrapBlazor/Services/TcpSocket/TcpSocketClientBase.cs +++ b/src/BootstrapBlazor/Services/TcpSocket/TcpSocketClientBase.cs @@ -165,6 +165,11 @@ public virtual async ValueTask SendAsync(ReadOnlyMemory data, Cancel { Log(LogLevel.Error, ex, $"TCP Socket send failed from {_localEndPoint} to {_remoteEndPoint}"); } + + if (options.EnableLog) + { + Log(LogLevel.Information, null, $"Sending data from {_localEndPoint} to {_remoteEndPoint}, Data Length: {data.Length} Data Content: {BitConverter.ToString(data.ToArray())} Result: {ret}"); + } return ret; } @@ -258,6 +263,11 @@ private async ValueTask ReceiveCoreAsync(ISocketClientProvider client, Memo { Log(LogLevel.Error, ex, $"TCP Socket receive failed from {_localEndPoint} to {_remoteEndPoint}"); } + + if (options.EnableLog) + { + Log(LogLevel.Information, null, $"Receiving data from {_localEndPoint} to {_remoteEndPoint}, Data Length: {len} Data Content: {BitConverter.ToString(buffer.ToArray())}"); + } return len; } diff --git a/test/UnitTest/Services/TcpSocketFactoryTest.cs b/test/UnitTest/Services/TcpSocketFactoryTest.cs index e8f1220f84a..cdafcfb8cb7 100644 --- a/test/UnitTest/Services/TcpSocketFactoryTest.cs +++ b/test/UnitTest/Services/TcpSocketFactoryTest.cs @@ -643,7 +643,11 @@ private static ITcpSocketClient CreateClient(Action? builder var provider = sc.BuildServiceProvider(); var factory = provider.GetRequiredService(); - var client = factory.GetOrCreate("test", op => op.LocalEndPoint = Utility.ConvertToIpEndPoint("localhost", 0)); + var client = factory.GetOrCreate("test", op => + { + op.LocalEndPoint = Utility.ConvertToIpEndPoint("localhost", 0); + op.EnableLog = true; + }); return client; }