Add Trace-level logging for JSON-RPC payloads in transports (#1191)
This commit is contained in:
parent
5b189aff0a
commit
ee8c485d41
1 changed files with 90 additions and 2 deletions
|
|
@ -1,6 +1,7 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using System.Diagnostics;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Channels;
|
||||
|
||||
namespace ModelContextProtocol.Protocol;
|
||||
|
|
@ -166,6 +167,21 @@ public abstract partial class TransportBase : ITransport
|
|||
[LoggerMessage(Level = LogLevel.Error, Message = "{EndpointName} transport send failed for message ID '{MessageId}'.")]
|
||||
private protected partial void LogTransportSendFailed(string endpointName, string messageId, Exception exception);
|
||||
|
||||
[LoggerMessage(Level = LogLevel.Trace, Message = "{EndpointName} transport sending message. Message: '{Message}'.")]
|
||||
private protected partial void LogTransportSendingMessageSensitive(string endpointName, string message);
|
||||
|
||||
/// <summary>
|
||||
/// Logs a sending message at Trace level if trace logging is enabled.
|
||||
/// </summary>
|
||||
/// <param name="message">The JSON-RPC message to log.</param>
|
||||
private protected void LogTransportSendingMessageSensitive(JsonRpcMessage message)
|
||||
{
|
||||
if (_logger.IsEnabled(LogLevel.Trace))
|
||||
{
|
||||
LogTransportSendingMessageSensitive(Name, JsonSerializer.Serialize(message, McpJsonUtilities.JsonContext.Default.JsonRpcMessage));
|
||||
}
|
||||
}
|
||||
|
||||
[LoggerMessage(Level = LogLevel.Information, Message = "{EndpointName} transport reading messages.")]
|
||||
private protected partial void LogTransportEnteringReadMessagesLoop(string endpointName);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue