You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MqttLoggerProviderTest.cs 1.6 KiB

преди 7 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. }