Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

49 wiersze
1.5 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. var message = formatter(state, exception);
  21. var traceMessage = new MqttNetTraceMessage(DateTime.Now, Environment.CurrentManagedThreadId, _categoryName, logLevel, message, exception);
  22. _mqttNetTrace.Publish(traceMessage);
  23. }
  24. public bool IsEnabled(LogLevel logLevel)
  25. {
  26. return true;
  27. }
  28. //not supported: async local requires netstandard1.3
  29. //for implementation see https://github.com/aspnet/Logging/blob/dev/src/Microsoft.Extensions.Logging.Console/ConsoleLogScope.cs
  30. public IDisposable BeginScope<TState>(TState state)
  31. {
  32. return new DisposableScope();
  33. }
  34. private class DisposableScope : IDisposable
  35. {
  36. public void Dispose()
  37. {
  38. }
  39. }
  40. }
  41. }