Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

MqttNetTrace.cs 2.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. namespace MQTTnet.Core.Diagnostics
  3. {
  4. public static class MqttNetTrace
  5. {
  6. public static event EventHandler<MqttNetTraceMessagePublishedEventArgs> TraceMessagePublished;
  7. public static void Verbose(string source, string message, params object[] parameters)
  8. {
  9. Publish(source, MqttNetTraceLevel.Verbose, null, message, parameters);
  10. }
  11. public static void Information(string source, string message, params object[] parameters)
  12. {
  13. Publish(source, MqttNetTraceLevel.Information, null, message, parameters);
  14. }
  15. public static void Warning(string source, string message, params object[] parameters)
  16. {
  17. Publish(source, MqttNetTraceLevel.Warning, null, message, parameters);
  18. }
  19. public static void Warning(string source, Exception exception, string message, params object[] parameters)
  20. {
  21. Publish(source, MqttNetTraceLevel.Warning, exception, message, parameters);
  22. }
  23. public static void Error(string source, string message, params object[] parameters)
  24. {
  25. Publish(source, MqttNetTraceLevel.Error, null, message, parameters);
  26. }
  27. public static void Error(string source, Exception exception, string message, params object[] parameters)
  28. {
  29. Publish(source, MqttNetTraceLevel.Error, exception, message, parameters);
  30. }
  31. private static void Publish(string source, MqttNetTraceLevel traceLevel, Exception exception, string message, params object[] parameters)
  32. {
  33. var handler = TraceMessagePublished;
  34. if (handler == null)
  35. {
  36. return;
  37. }
  38. if (parameters?.Length > 0)
  39. {
  40. try
  41. {
  42. message = string.Format(message, parameters);
  43. }
  44. catch (Exception formatException)
  45. {
  46. Error(nameof(MqttNetTrace), formatException, "Error while tracing message: " + message);
  47. return;
  48. }
  49. }
  50. handler.Invoke(null, new MqttNetTraceMessagePublishedEventArgs(Environment.CurrentManagedThreadId, source, traceLevel, message, exception));
  51. }
  52. }
  53. }