You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CustomMqttFactory.cs 1.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Extensions.Logging;
  4. using MQTTnet.Adapter;
  5. using MQTTnet.Diagnostics;
  6. using MQTTnet.Server.Configuration;
  7. using MQTTnet.Server.Logging;
  8. namespace MQTTnet.Server.Mqtt
  9. {
  10. public class CustomMqttFactory
  11. {
  12. private readonly MqttFactory _mqttFactory;
  13. public CustomMqttFactory(SettingsModel settings, ILogger<MqttServer> logger)
  14. {
  15. if (settings == null) throw new ArgumentNullException(nameof(settings));
  16. if (logger == null) throw new ArgumentNullException(nameof(logger));
  17. // It is important to avoid injecting the logger wrapper to ensure that no
  18. // unused log messages are generated by the MQTTnet library. Debug logging
  19. // has a huge performance impact.
  20. if (settings.EnableDebugLogging)
  21. {
  22. var mqttNetLogger = new MqttNetLoggerWrapper(logger);
  23. _mqttFactory = new MqttFactory(mqttNetLogger);
  24. logger.LogWarning("Debug logging is enabled. Performance of MQTTnet Server is decreased!");
  25. }
  26. else
  27. {
  28. _mqttFactory = new MqttFactory();
  29. }
  30. Logger = _mqttFactory.DefaultLogger;
  31. }
  32. public IMqttNetLogger Logger { get; }
  33. public IMqttServer CreateMqttServer(List<IMqttServerAdapter> adapters)
  34. {
  35. if (adapters == null) throw new ArgumentNullException(nameof(adapters));
  36. return _mqttFactory.CreateMqttServer(adapters);
  37. }
  38. }
  39. }