using System; 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 { public class CustomMqttFactory { private readonly MqttFactory _mqttFactory; public CustomMqttFactory(MqttSettingsModel settings, ILogger 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.LogWarning("Debug logging is enabled. Performance of MQTTnet Server is decreased!"); } else { _mqttFactory = new MqttFactory(); } Logger = _mqttFactory.DefaultLogger; } public IMqttNetLogger Logger { get; } public IMqttServer CreateMqttServer(List adapters) { if (adapters == null) throw new ArgumentNullException(nameof(adapters)); return _mqttFactory.CreateMqttServer(adapters); } } }