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.
 
 
 
 

52 wiersze
1.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.Extensions.Logging;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. using MQTTnet.Core.Diagnostics;
  9. namespace MQTTnet.Core.Tests
  10. {
  11. [TestClass]
  12. public class MqttLoggerProviderTest
  13. {
  14. [TestMethod]
  15. public void TestLoggerCallback()
  16. {
  17. var serviceCollection = new ServiceCollection();
  18. serviceCollection.AddLogging();
  19. var serviceProvider = serviceCollection.BuildServiceProvider();
  20. using ((IDisposable)serviceProvider)
  21. {
  22. var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
  23. loggerFactory.AddMqttTrace();
  24. var expectedMsg = "Hello World!";
  25. MqttNetTraceMessage msg = null;
  26. MqttNetTrace.TraceMessagePublished += (sender, args) =>
  27. {
  28. msg = args.TraceMessage;
  29. };
  30. var logger = loggerFactory.CreateLogger<MqttLoggerProviderTest>();
  31. logger.LogInformation(expectedMsg);
  32. Assert.AreEqual(expectedMsg, msg.Message);
  33. var expectedException = new Exception("bad stuff");
  34. logger.LogError(new EventId(), expectedException, expectedException.Message);
  35. Assert.AreEqual(expectedException, msg.Exception);
  36. Assert.AreEqual(expectedException.Message, msg.Message);
  37. }
  38. }
  39. }
  40. }