25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MqttNetLoggerWrapper.cs 1.8 KiB

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