Enhances MQTT server logging
All checks were successful
Build, Push and Run Container / build (push) Successful in 25s

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.
This commit is contained in:
2025-08-19 10:16:31 +02:00
parent de4e06401b
commit 0d2fdc4de6

View File

@@ -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;
}
}