From 7ede330b2b5844ce7a85ef0a55ed755e1443b6eb Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Fri, 9 Feb 2024 14:07:04 -0600 Subject: [PATCH] Remove StackTrace from EventSource Message When logging connection exception information to the EventSoruce we are putting the full exception ToString in the Message which clutters it to the users. Instead, just put the Exception.Message in the EventSource event message. The full exception information comes in the details of the EventSource event. Fix #1493 --- .../client/api/ShutdownEventArgs.cs | 27 +++++++++++++++---- .../RabbitMQ.Client/client/impl/Connection.cs | 5 ++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/projects/RabbitMQ.Client/client/api/ShutdownEventArgs.cs b/projects/RabbitMQ.Client/client/api/ShutdownEventArgs.cs index 2aa672227d..5d756eef95 100644 --- a/projects/RabbitMQ.Client/client/api/ShutdownEventArgs.cs +++ b/projects/RabbitMQ.Client/client/api/ShutdownEventArgs.cs @@ -123,17 +123,34 @@ public Exception Exception /// public string ReplyText { get; private set; } - /// - /// Override ToString to be useful for debugging. - /// - public override string ToString() + private string GetMessageCore() { return $"AMQP close-reason, initiated by {Initiator}" + $", code={ReplyCode}" + (ReplyText != null ? $", text='{ReplyText}'" : string.Empty) + $", classId={ClassId}" + $", methodId={MethodId}" - + (Cause != null ? $", cause={Cause}" : string.Empty) + + (Cause != null ? $", cause={Cause}" : string.Empty); + } + + /// + /// Gets a message suitable for logging. + /// + /// + /// This leaves out the full exception ToString since logging will include it separately. + /// + internal string GetLogMessage() + { + return GetMessageCore() + + (_exception != null ? $", exception={_exception.Message}" : string.Empty); + } + + /// + /// Override ToString to be useful for debugging. + /// + public override string ToString() + { + return GetMessageCore() + (_exception != null ? $", exception={_exception}" : string.Empty); } } diff --git a/projects/RabbitMQ.Client/client/impl/Connection.cs b/projects/RabbitMQ.Client/client/impl/Connection.cs index 477baa7910..20950dec42 100644 --- a/projects/RabbitMQ.Client/client/impl/Connection.cs +++ b/projects/RabbitMQ.Client/client/impl/Connection.cs @@ -455,16 +455,17 @@ public void HandleDomainUnload(object sender, EventArgs ea) public void HandleMainLoopException(ShutdownEventArgs reason) { + string message = reason.GetLogMessage(); if (!SetCloseReason(reason)) { - LogCloseError($"Unexpected Main Loop Exception while closing: {reason}", reason.Exception); + LogCloseError($"Unexpected Main Loop Exception while closing: {message}", reason.Exception); return; } _model0.MaybeSetConnectionStartException(reason.Exception); OnShutdown(); - LogCloseError($"Unexpected connection closure: {reason}", reason.Exception); + LogCloseError($"Unexpected connection closure: {message}", reason.Exception); } public bool HardProtocolExceptionHandler(HardProtocolException hpe)