-
-
Notifications
You must be signed in to change notification settings - Fork 362
feat(SocketClientOptions): add EnableLog parameter #6371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -165,6 +165,11 @@ public virtual async ValueTask<bool> SendAsync(ReadOnlyMemory<byte> 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}"); | ||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (performance): Converting ReadOnlyMemory to array may impact performance. ToArray() allocates and copies data, which can be costly for large or frequent logs. Consider limiting logged data size or using a more efficient method. Suggested implementation: if (options.EnableLog)
{
const int MaxLogBytes = 64;
int logLength = Math.Min(data.Length, MaxLogBytes);
string dataContent;
if (logLength > 0)
{
var span = data.Span.Slice(0, logLength);
dataContent = BitConverter.ToString(span.ToArray());
if (data.Length > MaxLogBytes)
{
dataContent += "...(truncated)";
}
}
else
{
dataContent = string.Empty;
}
Log(LogLevel.Information, null, $"Sending data from {_localEndPoint} to {_remoteEndPoint}, Data Length: {data.Length} Data Content: {dataContent} Result: {ret}");
}
return ret; if (options.EnableLog)
{
const int MaxLogBytes = 64;
int logLength = Math.Min(len, MaxLogBytes);
string dataContent;
if (logLength > 0)
{
var span = buffer.Span.Slice(0, logLength);
dataContent = BitConverter.ToString(span.ToArray());
if (len > MaxLogBytes)
{
dataContent += "...(truncated)";
}
}
else
{
dataContent = string.Empty;
}
Log(LogLevel.Information, null, $"Receiving data from {_localEndPoint} to {_remoteEndPoint}, Data Length: {len} Data Content: {dataContent}");
} |
||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| return ret; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
@@ -258,6 +263,11 @@ private async ValueTask<int> 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; | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+267
to
271
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (performance): Repeated ToArray() calls on buffer may be inefficient. For large or frequently used buffers, this conversion can impact performance. Consider logging a subset of the buffer or a more efficient format.
Suggested change
|
||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -643,7 +643,11 @@ private static ITcpSocketClient CreateClient(Action<ServiceCollection>? builder | |
|
|
||
| var provider = sc.BuildServiceProvider(); | ||
| var factory = provider.GetRequiredService<ITcpSocketFactory>(); | ||
| 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; | ||
|
Comment on lines
+646
to
+649
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): No test coverage for EnableLog=false scenario. Please add a test to cover the default EnableLog=false case and confirm that logging is not triggered. Suggested implementation: var provider = sc.BuildServiceProvider();
var factory = provider.GetRequiredService<ITcpSocketFactory>();
var client = factory.GetOrCreate("test", op =>
{
op.LocalEndPoint = Utility.ConvertToIpEndPoint("localhost", 0);
op.EnableLog = true;
});
return client;
}
[Fact]
public void GetOrCreate_DefaultEnableLogFalse_DoesNotTriggerLogging()
{
// Arrange
var sc = new ServiceCollection();
// Register ITcpSocketFactory and any required dependencies, including a mock logger if needed
var mockLogger = new Mock<ILogger>();
sc.AddSingleton<ILogger>(mockLogger.Object);
sc.AddSingleton<ITcpSocketFactory, TcpSocketFactory>();
var provider = sc.BuildServiceProvider();
var factory = provider.GetRequiredService<ITcpSocketFactory>();
// Act
var client = factory.GetOrCreate("test-default", op =>
{
op.LocalEndPoint = Utility.ConvertToIpEndPoint("localhost", 0);
// Do not set EnableLog, should default to false
});
// Assert
// Check that logging was not triggered
mockLogger.Verify(
logger => logger.Log(
It.IsAny<LogLevel>(),
It.IsAny<EventId>(),
It.IsAny<It.IsAnyType>(),
It.IsAny<Exception>(),
(Func<It.IsAnyType, Exception, string>)It.IsAny<object>()),
Times.Never
);
}
|
||
| }); | ||
| return client; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚨 issue (security): Logging raw data content may expose sensitive information.
Redact or limit logged data content, or make logging configurable to avoid exposing sensitive information.