From 0d2fdc4de632cdf0152f452838660c20c5e8fa7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szak=C3=A1ts=20Alp=C3=A1r=20Zsolt?= Date: Tue, 19 Aug 2025 10:16:31 +0200 Subject: [PATCH] Enhances MQTT server logging Improves logging for MQTT server events to provide more detailed information for debugging and monitoring. Logs the message payload when a client publishes a message and includes username, password, and remote IP address when an observer fails to connect due to invalid credentials. --- Source/ProofOfConcept/Services/MQTTServer.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/ProofOfConcept/Services/MQTTServer.cs b/Source/ProofOfConcept/Services/MQTTServer.cs index cb1f2d9..4736c69 100644 --- a/Source/ProofOfConcept/Services/MQTTServer.cs +++ b/Source/ProofOfConcept/Services/MQTTServer.cs @@ -1,4 +1,5 @@ using System.Net; +using System.Text; using Microsoft.Extensions.Options; using MQTTnet.Protocol; using MQTTnet.Server; @@ -54,7 +55,7 @@ public class MQTTServer : IHostedService return Task.CompletedTask; } - logger.LogInformation("Client {ClientID} published message to topic: {Topic}", e.ClientId, e.ApplicationMessage.Topic); + logger.LogInformation("Client {ClientID} published message to topic: {Topic}: {Message}", e.ClientId, e.ApplicationMessage.Topic, Encoding.UTF8.GetString(e.ApplicationMessage.Payload)); return Task.CompletedTask; }; @@ -93,7 +94,7 @@ public class MQTTServer : IHostedService { if (e.UserName != configuration.Observer.Username || e.Password != configuration.Observer.Password) { - logger.LogWarning("Observer tried to connect with invalid credentials"); + logger.LogWarning("Observer tried to connect with invalid credentials: {Username} / {Password} from {RemoteIP}", e.UserName, e.Password, e.RemoteEndPoint is IPEndPoint ipEndPoint ? ipEndPoint.ToString() : e.RemoteEndPoint.GetType().FullName); e.ReasonCode = MqttConnectReasonCode.NotAuthorized; } }