Skip to content

Commit 7e2f1f1

Browse files
authored
feat(SocketClientOptions): add EnableLog parameter (#6371)
* feat(SocketClientOptions): add EnableLog parameter * test: 增加单元测试
1 parent 786b2c1 commit 7e2f1f1

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/BootstrapBlazor/Services/TcpSocket/SocketClientOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@ public class SocketClientOptions
5050
/// </summary>
5151
/// <remarks>This property specifies the local network endpoint that the socket client will bind to when establishing a connection.</remarks>
5252
public IPEndPoint LocalEndPoint { get; set; } = new IPEndPoint(IPAddress.Any, 0);
53+
54+
/// <summary>
55+
/// Gets or sets a value indicating whether logging is enabled. Default value is false.
56+
/// </summary>
57+
public bool EnableLog { get; set; }
5358
}

src/BootstrapBlazor/Services/TcpSocket/TcpSocketClientBase.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ public virtual async ValueTask<bool> SendAsync(ReadOnlyMemory<byte> data, Cancel
165165
{
166166
Log(LogLevel.Error, ex, $"TCP Socket send failed from {_localEndPoint} to {_remoteEndPoint}");
167167
}
168+
169+
if (options.EnableLog)
170+
{
171+
Log(LogLevel.Information, null, $"Sending data from {_localEndPoint} to {_remoteEndPoint}, Data Length: {data.Length} Data Content: {BitConverter.ToString(data.ToArray())} Result: {ret}");
172+
}
168173
return ret;
169174
}
170175

@@ -258,6 +263,11 @@ private async ValueTask<int> ReceiveCoreAsync(ISocketClientProvider client, Memo
258263
{
259264
Log(LogLevel.Error, ex, $"TCP Socket receive failed from {_localEndPoint} to {_remoteEndPoint}");
260265
}
266+
267+
if (options.EnableLog)
268+
{
269+
Log(LogLevel.Information, null, $"Receiving data from {_localEndPoint} to {_remoteEndPoint}, Data Length: {len} Data Content: {BitConverter.ToString(buffer.ToArray())}");
270+
}
261271
return len;
262272
}
263273

test/UnitTest/Services/TcpSocketFactoryTest.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,11 @@ private static ITcpSocketClient CreateClient(Action<ServiceCollection>? builder
643643

644644
var provider = sc.BuildServiceProvider();
645645
var factory = provider.GetRequiredService<ITcpSocketFactory>();
646-
var client = factory.GetOrCreate("test", op => op.LocalEndPoint = Utility.ConvertToIpEndPoint("localhost", 0));
646+
var client = factory.GetOrCreate("test", op =>
647+
{
648+
op.LocalEndPoint = Utility.ConvertToIpEndPoint("localhost", 0);
649+
op.EnableLog = true;
650+
});
647651
return client;
648652
}
649653

0 commit comments

Comments
 (0)