Improve debug logging with caller context in AcBinaryHubProtocol

Diagnostic logging methods now use [CallerMemberName] to pass the actual caller's name to the logger via EventId.Name, ensuring logs reflect the true call site. Log message formats were simplified by removing explicit class prefixes. Comments clarify the rationale, and all internal calls were updated to the new signatures.
This commit is contained in:
Loretta 2026-07-13 07:01:19 +02:00
parent f2c636bb1c
commit 2bb9e3a77e
1 changed files with 9 additions and 7 deletions

View File

@ -762,30 +762,32 @@ public class AcBinaryHubProtocol : IHubProtocol
[Obsolete("Use ILogger via constructor parameter instead. This property will be removed in a future version.")]
public static Action<string>? DiagnosticLogger { get; set; }
// A caller-t EventId.Name-ként adjuk tovább (az AyCode logger onnan veszi a CallerName-et) — enélkül
// ez a wrapper saját magát bélyegzi be (pl. [->LogDiagnostic]) a valódi hívó helyett. Ld. DebugLogArgument.
[Conditional("DEBUG")]
private void LogDiagnostic(string message) => _logger?.LogDebug(message);
private void LogDiagnostic(string message, [CallerMemberName] string? caller = null) => _logger?.LogDebug(new EventId(0, caller), message);
[Conditional("DEBUG")]
private void LogReadSingleArgument(ReadOnlySequence<byte> argSlice, int argLength, Type targetType)
private void LogReadSingleArgument(ReadOnlySequence<byte> argSlice, int argLength, Type targetType, [CallerMemberName] string? caller = null)
{
if (_logger == null || !_logger.IsEnabled(LogLevel.Debug)) return;
var segmentCount = 0;
foreach (var _ in argSlice) segmentCount++;
_logger.LogDebug("[AcBinaryHubProtocol] ReadSingleArgument: argLength={ArgLength}, isSingleSegment={IsSingleSegment}, segments={SegmentCount}, type={TypeName}",
_logger.LogDebug(new EventId(0, caller), "ReadSingleArgument: argLength={ArgLength}, isSingleSegment={IsSingleSegment}, segments={SegmentCount}, type={TypeName}",
argLength, argSlice.IsSingleSegment, segmentCount, targetType.Name);
}
[Conditional("DEBUG")]
private void LogParseInvocation(string target, IReadOnlyList<Type> paramTypes, long remaining)
private void LogParseInvocation(string target, IReadOnlyList<Type> paramTypes, long remaining, [CallerMemberName] string? caller = null)
{
if (_logger == null || !_logger.IsEnabled(LogLevel.Debug)) return;
var typeNames = new string[paramTypes.Count];
for (var i = 0; i < paramTypes.Count; i++) typeNames[i] = paramTypes[i].Name;
_logger.LogDebug("[AcBinaryHubProtocol] ParseInvocation target='{Target}'; paramTypes.Count={ParamCount}; types=[{Types}]; remaining={Remaining}",
_logger.LogDebug(new EventId(0, caller), "ParseInvocation target='{Target}'; paramTypes.Count={ParamCount}; types=[{Types}]; remaining={Remaining}",
target, paramTypes.Count, string.Join(", ", typeNames), remaining);
}
@ -1406,7 +1408,7 @@ public class AcBinaryHubProtocol : IHubProtocol
{
var count = (int)ReadVarUInt(ref r);
LogDiagnostic($"[AcBinaryHubProtocol] ReadArguments count={count}; remaining={r.Remaining}");
LogDiagnostic($"ReadArguments count={count}; remaining={r.Remaining}");
var args = new object?[count];
@ -1414,7 +1416,7 @@ public class AcBinaryHubProtocol : IHubProtocol
{
var targetType = i < paramTypes.Count ? paramTypes[i] : typeof(object);
LogDiagnostic($"[AcBinaryHubProtocol] arg[{i}] targetType={targetType.Name}; remaining={r.Remaining}");
LogDiagnostic($"arg[{i}] targetType={targetType.Name}; remaining={r.Remaining}");
args[i] = ReadSingleArgument(ref r, targetType, headerContext);
OnArgumentRead(args[i], i);