Add MQTTnet debug logging.

This commit is contained in:
Christian Kratky
2019-03-11 21:32:36 +01:00
parent 3450889e84
commit 653ea8a102
6 changed files with 33 additions and 13 deletions
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using MQTTnet.Adapter;
using MQTTnet.Diagnostics;
using MQTTnet.Server.Configuration;
using MQTTnet.Server.Logging;
namespace MQTTnet.Server.Mqtt
@@ -11,13 +12,28 @@ namespace MQTTnet.Server.Mqtt
{
private readonly MqttFactory _mqttFactory;
public CustomMqttFactory(ILogger<MqttServer> logger)
public CustomMqttFactory(SettingsModel settings, ILogger<MqttServer> logger)
{
if (settings == null) throw new ArgumentNullException(nameof(settings));
if (logger == null) throw new ArgumentNullException(nameof(logger));
// It is important to avoid injecting the logger wrapper to ensure that no
// unused log messages are generated by the MQTTnet library. Debug logging
// has a huge performance impact.
if (settings.EnableDebugLogging)
{
var mqttNetLogger = new MqttNetLoggerWrapper(logger);
_mqttFactory = new MqttFactory(mqttNetLogger);
Logger = new MqttNetLoggerWrapper(logger);
logger.LogWarning("Debug logging is enabled. Performance of MQTTnet Server is decreased!");
}
else
{
_mqttFactory = new MqttFactory();
}
Logger = _mqttFactory.DefaultLogger;
_mqttFactory = new MqttFactory(Logger);
}
public IMqttNetLogger Logger { get; }