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.
 
 
 
 

43 rivejä
1.4 KiB

  1. using System;
  2. using Microsoft.Extensions.Logging;
  3. using MQTTnet.Diagnostics;
  4. namespace MQTTnet.Server.Logging
  5. {
  6. public class MqttNetLoggerWrapper : IMqttNetLogger
  7. {
  8. private readonly ILogger<MqttServer> _logger;
  9. public MqttNetLoggerWrapper(ILogger<MqttServer> logger)
  10. {
  11. _logger = logger ?? throw new ArgumentNullException(nameof(logger));
  12. }
  13. public event EventHandler<MqttNetLogMessagePublishedEventArgs> LogMessagePublished;
  14. public IMqttNetChildLogger CreateChildLogger(string source = null)
  15. {
  16. return new MqttNetChildLoggerWrapper(source, this);
  17. }
  18. public void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception)
  19. {
  20. var convertedLogLevel = ConvertLogLevel(logLevel);
  21. _logger.Log(convertedLogLevel, exception, message, parameters);
  22. }
  23. private static LogLevel ConvertLogLevel(MqttNetLogLevel logLevel)
  24. {
  25. switch (logLevel)
  26. {
  27. case MqttNetLogLevel.Error: return LogLevel.Error;
  28. case MqttNetLogLevel.Warning: return LogLevel.Warning;
  29. case MqttNetLogLevel.Info: return LogLevel.Information;
  30. case MqttNetLogLevel.Verbose: return LogLevel.Debug;
  31. }
  32. return LogLevel.Debug;
  33. }
  34. }
  35. }