Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

53 строки
1.6 KiB

  1. using System;
  2. using Microsoft.Extensions.Logging;
  3. namespace MQTTnet.Core.Diagnostics
  4. {
  5. public class MqttNetLogger : ILogger
  6. {
  7. private readonly string _categoryName;
  8. private readonly MqttNetTrace _mqttNetTrace;
  9. public MqttNetLogger(string categoryName, MqttNetTrace mqttNetTrace)
  10. {
  11. _categoryName = categoryName;
  12. _mqttNetTrace = mqttNetTrace;
  13. }
  14. public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
  15. {
  16. if (formatter == null)
  17. {
  18. throw new ArgumentNullException(nameof(formatter));
  19. }
  20. if (!MqttNetTrace.HasListeners)
  21. {
  22. return;
  23. }
  24. var message = formatter(state, exception);
  25. var traceMessage = new MqttNetTraceMessage(DateTime.Now, Environment.CurrentManagedThreadId, _categoryName, logLevel, message, exception);
  26. _mqttNetTrace.Publish(traceMessage);
  27. }
  28. public bool IsEnabled(LogLevel logLevel)
  29. {
  30. return MqttNetTrace.HasListeners;
  31. }
  32. //not supported: async local requires netstandard1.3
  33. //for implementation see https://github.com/aspnet/Logging/blob/dev/src/Microsoft.Extensions.Logging.Console/ConsoleLogScope.cs
  34. public IDisposable BeginScope<TState>(TState state)
  35. {
  36. return new DisposableScope();
  37. }
  38. private class DisposableScope : IDisposable
  39. {
  40. public void Dispose()
  41. {
  42. }
  43. }
  44. }
  45. }