Skip to content

Commit

Permalink
Added MQTT logging. Closes GH-604
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Oct 20, 2023
1 parent c211e19 commit e8df04e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
39 changes: 39 additions & 0 deletions src/Transports/MQTT/Wolverine.MQTT/Internals/MqttNetLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.Extensions.Logging;
using MQTTnet.Client;
using MQTTnet.Diagnostics;

namespace Wolverine.MQTT.Internals;

public class MqttNetLogger : IMqttNetLogger
{
private readonly ILogger _logger;

public MqttNetLogger(ILogger<MqttClient> logger)
{
_logger = logger;
}

public void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception)
{
switch (logLevel)
{
case MqttNetLogLevel.Error:
_logger.LogError(exception, message, parameters);
break;

case MqttNetLogLevel.Info:
_logger.LogInformation(message, parameters);
break;

case MqttNetLogLevel.Warning:
_logger.LogWarning(message, parameters);
break;

case MqttNetLogLevel.Verbose:
_logger.LogDebug(message, parameters);
break;
}
}

public bool IsEnabled => _logger.IsEnabled(LogLevel.Debug);
}
4 changes: 2 additions & 2 deletions src/Transports/MQTT/Wolverine.MQTT/Internals/MqttTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public override async ValueTask InitializeAsync(IWolverineRuntime runtime)

var mqttFactory = new MqttFactory();

// TODO -- add logging here!
Client = mqttFactory.CreateManagedMqttClient();
var logger = new MqttNetLogger(runtime.LoggerFactory.CreateLogger<MqttClient>());
Client = mqttFactory.CreateManagedMqttClient(logger);

Options.ClientOptions.ProtocolVersion = MqttProtocolVersion.V500;

Expand Down

0 comments on commit e8df04e

Please sign in to comment.