From 0920c86d373cdce15efa27641c68bc468160c021 Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 26 Sep 2021 11:05:00 +0200 Subject: [PATCH] Refactor logger. (#1247) --- Build/MQTTnet.nuspec | 3 + .../Extensions/ServiceCollectionExtensions.cs | 3 +- Source/MQTTnet.AspnetCore/MqttHostedServer.cs | 6 +- .../MqttWebSocketServerAdapter.cs | 2 +- .../ManagedMqttClient.cs | 5 +- .../MqttFactoryExtensions.cs | 4 +- .../WebSocket4NetMqttClientAdapterFactory.cs | 1 + Source/MQTTnet/Adapter/MqttChannelAdapter.cs | 5 +- .../Adapter/MqttPacketInspectorHandler.cs | 5 +- Source/MQTTnet/Client/IMqttClientFactory.cs | 1 + Source/MQTTnet/Client/MqttClient.cs | 5 +- .../Diagnostics/IMqttNetScopedLogger.cs | 11 -- .../{ => Logger}/IMqttNetLogger.cs | 4 +- .../Diagnostics/Logger/MqttNetEventLogger.cs | 58 +++++++ .../{ => Logger}/MqttNetLogLevel.cs | 2 +- .../{ => Logger}/MqttNetLogMessage.cs | 2 +- .../MqttNetLogMessagePublishedEventArgs.cs | 9 +- .../Diagnostics/Logger/MqttNetNullLogger.cs | 14 ++ .../MqttNetSourceLogger.cs} | 13 +- .../Logger/MqttNetSourceLoggerExtensions.cs | 69 ++++++++ .../Diagnostics/MqttNetGlobalLogger.cs | 17 -- Source/MQTTnet/Diagnostics/MqttNetLogger.cs | 73 --------- .../MqttNetScopedLoggerExtensions.cs | 62 ------- .../{ => Runtime}/TargetFrameworkProvider.cs | 2 +- Source/MQTTnet/IMqttFactory.cs | 1 + .../MqttClientAdapterFactory.cs | 1 + .../Implementations/MqttTcpServerAdapter.cs | 5 +- .../Implementations/MqttTcpServerListener.cs | 5 +- Source/MQTTnet/Internal/TaskExtensions.cs | 3 +- .../LowLevelClient/LowLevelMqttClient.cs | 5 +- Source/MQTTnet/MqttFactory.cs | 4 +- .../Server/IMqttRetainedMessagesManager.cs | 1 + Source/MQTTnet/Server/IMqttServerFactory.cs | 1 + .../Server/Internal/MqttClientConnection.cs | 5 +- .../Internal/MqttClientSessionsManager.cs | 15 +- .../Internal/MqttRetainedMessagesManager.cs | 5 +- .../Internal/MqttServerEventDispatcher.cs | 5 +- .../Internal/MqttServerKeepAliveMonitor.cs | 5 +- ...qttApplicationMessageInterceptorContext.cs | 8 +- Source/MQTTnet/Server/MqttServer.cs | 5 +- ...edApplicationMessageInterceptorDelegate.cs | 9 +- .../ChannelAdapterBenchmark.cs | 16 +- .../Configurations/AllowNonOptimized.cs | 4 +- .../Configurations/RuntimeCompareConfig.cs | 3 +- Tests/MQTTnet.Benchmarks/LoggerBenchmark.cs | 56 +++++-- .../MessageProcessingBenchmark.cs | 6 +- ...rocessingMqttConnectionContextBenchmark.cs | 15 +- .../MqttTcpChannelBenchmark.cs | 20 +-- Tests/MQTTnet.Benchmarks/Program.cs | 2 +- .../MQTTnet.Benchmarks/SerializerBenchmark.cs | 16 +- Tests/MQTTnet.Benchmarks/TcpPipesBenchmark.cs | 8 +- .../TopicFilterComparerBenchmark.cs | 5 +- .../{ => Client}/LowLevelMqttClient_Tests.cs | 14 +- .../{ => Client}/ManagedMqttClient_Tests.cs | 29 ++-- .../MqttClientOptionsBuilder_Tests.cs | 2 +- .../{ => Client}/MqttClient_Tests.cs | 22 +-- .../Factory/MqttFactory_Tests.cs | 154 ++++++++++++++++++ .../MQTTnet.Core.Tests/Logger/Logger_Tests.cs | 62 +++++++ .../Logger/SourceLogger_Tests.cs | 26 +++ .../Mockups/TestEnvironment.cs | 18 +- .../MQTTnet.Core.Tests/Mockups/TestLogger.cs | 13 +- Tests/MQTTnet.Core.Tests/MqttFactory_Tests.cs | 81 --------- .../MQTTnet.Core.Tests/MqttNetLogger_Tests.cs | 68 -------- .../MqttPacketSerializer_Tests.cs | 7 +- .../MQTTnet.TestApp.NetCore/ClientFlowTest.cs | 4 +- Tests/MQTTnet.TestApp.NetCore/ClientTest.cs | 4 +- .../MqttNetConsoleLogger.cs | 6 +- Tests/MQTTnet.TestApp.NetCore/Program.cs | 1 + .../ServerAndClientTest.cs | 4 +- Tests/MQTTnet.TestApp.NetCore/ServerTest.cs | 3 +- 70 files changed, 618 insertions(+), 510 deletions(-) delete mode 100644 Source/MQTTnet/Diagnostics/IMqttNetScopedLogger.cs rename Source/MQTTnet/Diagnostics/{ => Logger}/IMqttNetLogger.cs (66%) create mode 100644 Source/MQTTnet/Diagnostics/Logger/MqttNetEventLogger.cs rename Source/MQTTnet/Diagnostics/{ => Logger}/MqttNetLogLevel.cs (73%) rename Source/MQTTnet/Diagnostics/{ => Logger}/MqttNetLogMessage.cs (95%) rename Source/MQTTnet/Diagnostics/{ => Logger}/MqttNetLogMessagePublishedEventArgs.cs (53%) create mode 100644 Source/MQTTnet/Diagnostics/Logger/MqttNetNullLogger.cs rename Source/MQTTnet/Diagnostics/{MqttNetScopedLogger.cs => Logger/MqttNetSourceLogger.cs} (52%) create mode 100644 Source/MQTTnet/Diagnostics/Logger/MqttNetSourceLoggerExtensions.cs delete mode 100644 Source/MQTTnet/Diagnostics/MqttNetGlobalLogger.cs delete mode 100644 Source/MQTTnet/Diagnostics/MqttNetLogger.cs delete mode 100644 Source/MQTTnet/Diagnostics/MqttNetScopedLoggerExtensions.cs rename Source/MQTTnet/Diagnostics/{ => Runtime}/TargetFrameworkProvider.cs (94%) rename Tests/MQTTnet.Core.Tests/{ => Client}/LowLevelMqttClient_Tests.cs (99%) rename Tests/MQTTnet.Core.Tests/{ => Client}/ManagedMqttClient_Tests.cs (97%) rename Tests/MQTTnet.Core.Tests/{ => Client}/MqttClientOptionsBuilder_Tests.cs (95%) rename Tests/MQTTnet.Core.Tests/{ => Client}/MqttClient_Tests.cs (99%) create mode 100644 Tests/MQTTnet.Core.Tests/Factory/MqttFactory_Tests.cs create mode 100644 Tests/MQTTnet.Core.Tests/Logger/Logger_Tests.cs create mode 100644 Tests/MQTTnet.Core.Tests/Logger/SourceLogger_Tests.cs delete mode 100644 Tests/MQTTnet.Core.Tests/MqttFactory_Tests.cs delete mode 100644 Tests/MQTTnet.Core.Tests/MqttNetLogger_Tests.cs diff --git a/Build/MQTTnet.nuspec b/Build/MQTTnet.nuspec index dc54c49..6893d80 100644 --- a/Build/MQTTnet.nuspec +++ b/Build/MQTTnet.nuspec @@ -13,6 +13,7 @@ MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker) and supports v3.1.0, v3.1.1 and v5.0.0 of the MQTT protocol. * [Core] Added all builders to the MQTT factory. +* [Core] Removed global logger and refactored logging (BREAKING CHANGE!). * [Client] Renamed _MqttClientConnectResult_ to _MqttClientConnectResult_ (BREAKING CHANGE!). * [ManagedClient] Extended ReconnectAsync (thanks to @nvsnkv, #1202). * [ManagedClient] Improved Amazon AWS support (thanks to @scottbrogden-iheartmedia, #1209). @@ -26,6 +27,8 @@ * [Server] Refactored connection management code. * [Server] Exposed more details in _MqttServerClientConnectedEventArgs_. * [Server] Processing all pending messages before stopping (thanks to @AblEdge, #1234). +* [Server] Added support for a custom exception handler in _MqttServerMultiThreadedApplicationMessageInterceptorDelegate_. +* [Server] Removed logger from _MqttServerMultiThreadedApplicationMessageInterceptorDelegate_ (BREAKING CHANGE!). * [MQTTnet.Server] Moved server project to a dedicated GitHub repository. Git commit: $gitCommit diff --git a/Source/MQTTnet.AspnetCore/Extensions/ServiceCollectionExtensions.cs b/Source/MQTTnet.AspnetCore/Extensions/ServiceCollectionExtensions.cs index 7ad53f4..683fe1c 100644 --- a/Source/MQTTnet.AspnetCore/Extensions/ServiceCollectionExtensions.cs +++ b/Source/MQTTnet.AspnetCore/Extensions/ServiceCollectionExtensions.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using MQTTnet.Adapter; using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; using MQTTnet.Implementations; using MQTTnet.Server; @@ -61,7 +62,7 @@ namespace MQTTnet.AspNetCore.Extensions private static IServiceCollection AddHostedMqttServer(this IServiceCollection services) { - var logger = new MqttNetLogger(); + var logger = new MqttNetEventLogger(); services.AddSingleton(logger); services.AddSingleton(); diff --git a/Source/MQTTnet.AspnetCore/MqttHostedServer.cs b/Source/MQTTnet.AspnetCore/MqttHostedServer.cs index 7108c11..3a9c882 100644 --- a/Source/MQTTnet.AspnetCore/MqttHostedServer.cs +++ b/Source/MQTTnet.AspnetCore/MqttHostedServer.cs @@ -4,14 +4,14 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; using MQTTnet.Adapter; -using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; using MQTTnet.Server; namespace MQTTnet.AspNetCore { - public class MqttHostedServer : MqttServer, IHostedService + public sealed class MqttHostedServer : MqttServer, IHostedService { - private readonly IMqttServerOptions _options; + readonly IMqttServerOptions _options; public MqttHostedServer(IMqttServerOptions options, IEnumerable adapters, IMqttNetLogger logger) : base(adapters, logger) diff --git a/Source/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs b/Source/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs index cec833e..270305d 100644 --- a/Source/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs +++ b/Source/MQTTnet.AspnetCore/MqttWebSocketServerAdapter.cs @@ -1,12 +1,12 @@ using Microsoft.AspNetCore.Http; using MQTTnet.Adapter; -using MQTTnet.Diagnostics; using MQTTnet.Formatter; using MQTTnet.Implementations; using MQTTnet.Server; using System; using System.Net.WebSockets; using System.Threading.Tasks; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.AspNetCore { diff --git a/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs b/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs index a9e9481..6e1c1f5 100644 --- a/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs +++ b/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs @@ -13,6 +13,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.Extensions.ManagedClient { @@ -33,7 +34,7 @@ namespace MQTTnet.Extensions.ManagedClient readonly HashSet _unsubscriptions = new HashSet(); readonly SemaphoreSlim _subscriptionsQueuedSignal = new SemaphoreSlim(0); - readonly IMqttNetScopedLogger _logger; + readonly MqttNetSourceLogger _logger; readonly AsyncLock _messageQueueLock = new AsyncLock(); @@ -48,7 +49,7 @@ namespace MQTTnet.Extensions.ManagedClient InternalClient = mqttClient ?? throw new ArgumentNullException(nameof(mqttClient)); if (logger == null) throw new ArgumentNullException(nameof(logger)); - _logger = logger.CreateScopedLogger(nameof(ManagedMqttClient)); + _logger = logger.WithSource(nameof(ManagedMqttClient)); } public bool IsConnected => InternalClient.IsConnected; diff --git a/Source/MQTTnet.Extensions.ManagedClient/MqttFactoryExtensions.cs b/Source/MQTTnet.Extensions.ManagedClient/MqttFactoryExtensions.cs index b0fd882..10256c5 100644 --- a/Source/MQTTnet.Extensions.ManagedClient/MqttFactoryExtensions.cs +++ b/Source/MQTTnet.Extensions.ManagedClient/MqttFactoryExtensions.cs @@ -1,5 +1,5 @@ -using MQTTnet.Diagnostics; -using System; +using System; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.Extensions.ManagedClient { diff --git a/Source/MQTTnet.Extensions.WebSocket4Net/WebSocket4NetMqttClientAdapterFactory.cs b/Source/MQTTnet.Extensions.WebSocket4Net/WebSocket4NetMqttClientAdapterFactory.cs index 6382653..e4e72cc 100644 --- a/Source/MQTTnet.Extensions.WebSocket4Net/WebSocket4NetMqttClientAdapterFactory.cs +++ b/Source/MQTTnet.Extensions.WebSocket4Net/WebSocket4NetMqttClientAdapterFactory.cs @@ -4,6 +4,7 @@ using MQTTnet.Diagnostics; using MQTTnet.Formatter; using MQTTnet.Implementations; using System; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.Extensions.WebSocket4Net { diff --git a/Source/MQTTnet/Adapter/MqttChannelAdapter.cs b/Source/MQTTnet/Adapter/MqttChannelAdapter.cs index 8e3e8dd..8ca0996 100644 --- a/Source/MQTTnet/Adapter/MqttChannelAdapter.cs +++ b/Source/MQTTnet/Adapter/MqttChannelAdapter.cs @@ -11,6 +11,7 @@ using System.Runtime.InteropServices; using System.Security.Cryptography.X509Certificates; using System.Threading; using System.Threading.Tasks; +using MQTTnet.Diagnostics.Logger; using MQTTnet.Diagnostics.PacketInspection; namespace MQTTnet.Adapter @@ -24,7 +25,7 @@ namespace MQTTnet.Adapter readonly byte[] _fixedHeaderBuffer = new byte[2]; readonly MqttPacketInspectorHandler _packetInspectorHandler; - readonly IMqttNetScopedLogger _logger; + readonly MqttNetSourceLogger _logger; readonly IMqttChannel _channel; readonly AsyncLock _syncRoot = new AsyncLock(); @@ -40,7 +41,7 @@ namespace MQTTnet.Adapter _packetInspectorHandler = new MqttPacketInspectorHandler(packetInspector, logger); if (logger == null) throw new ArgumentNullException(nameof(logger)); - _logger = logger.CreateScopedLogger(nameof(MqttChannelAdapter)); + _logger = logger.WithSource(nameof(MqttChannelAdapter)); } public string Endpoint => _channel.Endpoint; diff --git a/Source/MQTTnet/Adapter/MqttPacketInspectorHandler.cs b/Source/MQTTnet/Adapter/MqttPacketInspectorHandler.cs index f8048d9..04eb260 100644 --- a/Source/MQTTnet/Adapter/MqttPacketInspectorHandler.cs +++ b/Source/MQTTnet/Adapter/MqttPacketInspectorHandler.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Linq; using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; using MQTTnet.Diagnostics.PacketInspection; namespace MQTTnet.Adapter @@ -10,7 +11,7 @@ namespace MQTTnet.Adapter { readonly MemoryStream _receivedPacketBuffer; readonly IMqttPacketInspector _packetInspector; - readonly IMqttNetScopedLogger _logger; + readonly MqttNetSourceLogger _logger; public MqttPacketInspectorHandler(IMqttPacketInspector packetInspector, IMqttNetLogger logger) { @@ -22,7 +23,7 @@ namespace MQTTnet.Adapter } if (logger == null) throw new ArgumentNullException(nameof(logger)); - _logger = logger.CreateScopedLogger(nameof(MqttPacketInspectorHandler)); + _logger = logger.WithSource(nameof(MqttPacketInspectorHandler)); } public void BeginReceivePacket() diff --git a/Source/MQTTnet/Client/IMqttClientFactory.cs b/Source/MQTTnet/Client/IMqttClientFactory.cs index 4bbb79a..4831456 100644 --- a/Source/MQTTnet/Client/IMqttClientFactory.cs +++ b/Source/MQTTnet/Client/IMqttClientFactory.cs @@ -1,5 +1,6 @@ using MQTTnet.Adapter; using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; using MQTTnet.LowLevelClient; namespace MQTTnet.Client diff --git a/Source/MQTTnet/Client/MqttClient.cs b/Source/MQTTnet/Client/MqttClient.cs index 067f54f..781ddd0 100644 --- a/Source/MQTTnet/Client/MqttClient.cs +++ b/Source/MQTTnet/Client/MqttClient.cs @@ -16,6 +16,7 @@ using MQTTnet.Protocol; using System; using System.Threading; using System.Threading.Tasks; +using MQTTnet.Diagnostics.Logger; using MQTTnet.Implementations; namespace MQTTnet.Client @@ -27,7 +28,7 @@ namespace MQTTnet.Client readonly object _disconnectLock = new object(); readonly IMqttClientAdapterFactory _adapterFactory; - readonly IMqttNetScopedLogger _logger; + readonly MqttNetSourceLogger _logger; CancellationTokenSource _backgroundCancellationTokenSource; Task _packetReceiverTask; @@ -49,7 +50,7 @@ namespace MQTTnet.Client if (logger == null) throw new ArgumentNullException(nameof(logger)); _adapterFactory = channelFactory ?? throw new ArgumentNullException(nameof(channelFactory)); - _logger = logger.CreateScopedLogger(nameof(MqttClient)); + _logger = logger.WithSource(nameof(MqttClient)); } public IMqttClientConnectedHandler ConnectedHandler { get; set; } diff --git a/Source/MQTTnet/Diagnostics/IMqttNetScopedLogger.cs b/Source/MQTTnet/Diagnostics/IMqttNetScopedLogger.cs deleted file mode 100644 index 14bb756..0000000 --- a/Source/MQTTnet/Diagnostics/IMqttNetScopedLogger.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - -namespace MQTTnet.Diagnostics -{ - public interface IMqttNetScopedLogger - { - IMqttNetScopedLogger CreateScopedLogger(string source); - - void Publish(MqttNetLogLevel logLevel, string message, object[] parameters, Exception exception); - } -} diff --git a/Source/MQTTnet/Diagnostics/IMqttNetLogger.cs b/Source/MQTTnet/Diagnostics/Logger/IMqttNetLogger.cs similarity index 66% rename from Source/MQTTnet/Diagnostics/IMqttNetLogger.cs rename to Source/MQTTnet/Diagnostics/Logger/IMqttNetLogger.cs index e0637ab..c96ee64 100644 --- a/Source/MQTTnet/Diagnostics/IMqttNetLogger.cs +++ b/Source/MQTTnet/Diagnostics/Logger/IMqttNetLogger.cs @@ -1,11 +1,9 @@ using System; -namespace MQTTnet.Diagnostics +namespace MQTTnet.Diagnostics.Logger { public interface IMqttNetLogger { - IMqttNetScopedLogger CreateScopedLogger(string source); - void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception); } } diff --git a/Source/MQTTnet/Diagnostics/Logger/MqttNetEventLogger.cs b/Source/MQTTnet/Diagnostics/Logger/MqttNetEventLogger.cs new file mode 100644 index 0000000..bbf914a --- /dev/null +++ b/Source/MQTTnet/Diagnostics/Logger/MqttNetEventLogger.cs @@ -0,0 +1,58 @@ +using System; + +namespace MQTTnet.Diagnostics.Logger +{ + /// + /// This logger fires an event when a new message was published. + /// + public sealed class MqttNetEventLogger : IMqttNetLogger + { + public MqttNetEventLogger(string logId = null) + { + LogId = logId; + } + + public event EventHandler LogMessagePublished; + + public string LogId { get; } + + public void Publish(MqttNetLogLevel level, string source, string message, object[] parameters, Exception exception) + { + var eventHandler = LogMessagePublished; + if (eventHandler == null) + { + // No listener is attached so we can step out. + // Keep a reference to the handler because the handler + // might be null after preparing the message. + return; + } + + if (parameters?.Length > 0 && message?.Length > 0) + { + try + { + message = string.Format(message, parameters); + } + catch (FormatException) + { + message = "MESSAGE FORMAT INVALID: " + message; + } + } + + // We only use UTC here to improve performance. Using a local date time + // would require to load the time zone settings! + var logMessage = new MqttNetLogMessage + { + LogId = LogId, + Timestamp = DateTime.UtcNow, + Source = source, + ThreadId = Environment.CurrentManagedThreadId, + Level = level, + Message = message, + Exception = exception + }; + + eventHandler.Invoke(this, new MqttNetLogMessagePublishedEventArgs(logMessage)); + } + } +} \ No newline at end of file diff --git a/Source/MQTTnet/Diagnostics/MqttNetLogLevel.cs b/Source/MQTTnet/Diagnostics/Logger/MqttNetLogLevel.cs similarity index 73% rename from Source/MQTTnet/Diagnostics/MqttNetLogLevel.cs rename to Source/MQTTnet/Diagnostics/Logger/MqttNetLogLevel.cs index cdc7a78..f4b07b7 100644 --- a/Source/MQTTnet/Diagnostics/MqttNetLogLevel.cs +++ b/Source/MQTTnet/Diagnostics/Logger/MqttNetLogLevel.cs @@ -1,4 +1,4 @@ -namespace MQTTnet.Diagnostics +namespace MQTTnet.Diagnostics.Logger { public enum MqttNetLogLevel { diff --git a/Source/MQTTnet/Diagnostics/MqttNetLogMessage.cs b/Source/MQTTnet/Diagnostics/Logger/MqttNetLogMessage.cs similarity index 95% rename from Source/MQTTnet/Diagnostics/MqttNetLogMessage.cs rename to Source/MQTTnet/Diagnostics/Logger/MqttNetLogMessage.cs index 285dc00..8d3b06c 100644 --- a/Source/MQTTnet/Diagnostics/MqttNetLogMessage.cs +++ b/Source/MQTTnet/Diagnostics/Logger/MqttNetLogMessage.cs @@ -1,6 +1,6 @@ using System; -namespace MQTTnet.Diagnostics +namespace MQTTnet.Diagnostics.Logger { public sealed class MqttNetLogMessage { diff --git a/Source/MQTTnet/Diagnostics/MqttNetLogMessagePublishedEventArgs.cs b/Source/MQTTnet/Diagnostics/Logger/MqttNetLogMessagePublishedEventArgs.cs similarity index 53% rename from Source/MQTTnet/Diagnostics/MqttNetLogMessagePublishedEventArgs.cs rename to Source/MQTTnet/Diagnostics/Logger/MqttNetLogMessagePublishedEventArgs.cs index 81ad62d..581921d 100644 --- a/Source/MQTTnet/Diagnostics/MqttNetLogMessagePublishedEventArgs.cs +++ b/Source/MQTTnet/Diagnostics/Logger/MqttNetLogMessagePublishedEventArgs.cs @@ -1,21 +1,14 @@ using System; -namespace MQTTnet.Diagnostics +namespace MQTTnet.Diagnostics.Logger { public sealed class MqttNetLogMessagePublishedEventArgs : EventArgs { public MqttNetLogMessagePublishedEventArgs(MqttNetLogMessage logMessage) { LogMessage = logMessage ?? throw new ArgumentNullException(nameof(logMessage)); - -#pragma warning disable CS0618 // Type or member is obsolete - TraceMessage = logMessage; -#pragma warning restore CS0618 // Type or member is obsolete } - [Obsolete("Use new property LogMessage instead.")] - public MqttNetLogMessage TraceMessage { get; } - public MqttNetLogMessage LogMessage { get; } } } diff --git a/Source/MQTTnet/Diagnostics/Logger/MqttNetNullLogger.cs b/Source/MQTTnet/Diagnostics/Logger/MqttNetNullLogger.cs new file mode 100644 index 0000000..806fd34 --- /dev/null +++ b/Source/MQTTnet/Diagnostics/Logger/MqttNetNullLogger.cs @@ -0,0 +1,14 @@ +using System; + +namespace MQTTnet.Diagnostics.Logger +{ + /// + /// This logger does nothing with the messages. + /// + public sealed class MqttNetNullLogger : IMqttNetLogger + { + public void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception) + { + } + } +} \ No newline at end of file diff --git a/Source/MQTTnet/Diagnostics/MqttNetScopedLogger.cs b/Source/MQTTnet/Diagnostics/Logger/MqttNetSourceLogger.cs similarity index 52% rename from Source/MQTTnet/Diagnostics/MqttNetScopedLogger.cs rename to Source/MQTTnet/Diagnostics/Logger/MqttNetSourceLogger.cs index 7b16db5..4b76ddf 100644 --- a/Source/MQTTnet/Diagnostics/MqttNetScopedLogger.cs +++ b/Source/MQTTnet/Diagnostics/Logger/MqttNetSourceLogger.cs @@ -1,21 +1,16 @@ using System; -namespace MQTTnet.Diagnostics +namespace MQTTnet.Diagnostics.Logger { - public sealed class MqttNetScopedLogger : IMqttNetScopedLogger + public sealed class MqttNetSourceLogger { readonly IMqttNetLogger _logger; readonly string _source; - public MqttNetScopedLogger(IMqttNetLogger logger, string source) + public MqttNetSourceLogger(IMqttNetLogger logger, string source) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); - _source = source ?? throw new ArgumentNullException(nameof(source)); - } - - public IMqttNetScopedLogger CreateScopedLogger(string source) - { - return new MqttNetScopedLogger(_logger, source); + _source = source; } public void Publish(MqttNetLogLevel logLevel, string message, object[] parameters, Exception exception) diff --git a/Source/MQTTnet/Diagnostics/Logger/MqttNetSourceLoggerExtensions.cs b/Source/MQTTnet/Diagnostics/Logger/MqttNetSourceLoggerExtensions.cs new file mode 100644 index 0000000..e92c860 --- /dev/null +++ b/Source/MQTTnet/Diagnostics/Logger/MqttNetSourceLoggerExtensions.cs @@ -0,0 +1,69 @@ +using System; + +namespace MQTTnet.Diagnostics.Logger +{ + public static class MqttNetSourceLoggerExtensions + { + public static MqttNetSourceLogger WithSource(this IMqttNetLogger logger, string source) + { + if (logger == null) throw new ArgumentNullException(nameof(logger)); + + return new MqttNetSourceLogger(logger, source); + } + + public static void Verbose(this MqttNetSourceLogger logger, string message, params object[] parameters) + { + logger.Publish(MqttNetLogLevel.Verbose, message, parameters, null); + } + + public static void Verbose(this MqttNetSourceLogger logger, string message) + { + logger.Publish(MqttNetLogLevel.Verbose, message, null, null); + } + + public static void Info(this MqttNetSourceLogger logger, string message, params object[] parameters) + { + logger.Publish(MqttNetLogLevel.Info, message, parameters, null); + } + + public static void Info(this MqttNetSourceLogger logger, string message) + { + logger.Publish(MqttNetLogLevel.Info, message, null, null); + } + + public static void Warning(this MqttNetSourceLogger logger, Exception exception, string message, params object[] parameters) + { + logger.Publish(MqttNetLogLevel.Warning, message, parameters, exception); + } + + public static void Warning(this MqttNetSourceLogger logger, Exception exception, string message) + { + logger.Publish(MqttNetLogLevel.Warning, message, null, exception); + } + + public static void Warning(this MqttNetSourceLogger logger, string message, params object[] parameters) + { + logger.Publish(MqttNetLogLevel.Warning, message, parameters, null); + } + + public static void Warning(this MqttNetSourceLogger logger, string message) + { + logger.Publish(MqttNetLogLevel.Warning, message, null, null); + } + + public static void Error(this MqttNetSourceLogger logger, Exception exception, string message, params object[] parameters) + { + logger.Publish(MqttNetLogLevel.Error, message, parameters, exception); + } + + public static void Error(this MqttNetSourceLogger logger, Exception exception, string message) + { + logger.Publish(MqttNetLogLevel.Error, message, null, exception); + } + + public static void Error(this MqttNetSourceLogger logger, string message) + { + logger.Publish(MqttNetLogLevel.Error, message, null, null); + } + } +} diff --git a/Source/MQTTnet/Diagnostics/MqttNetGlobalLogger.cs b/Source/MQTTnet/Diagnostics/MqttNetGlobalLogger.cs deleted file mode 100644 index 0ad7486..0000000 --- a/Source/MQTTnet/Diagnostics/MqttNetGlobalLogger.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; - -namespace MQTTnet.Diagnostics -{ - [Obsolete("Please pass an instance of the IMqttNetLogger to the factory/server or client. The global logger will be deleted in the future.")] - public static class MqttNetGlobalLogger - { - public static event EventHandler LogMessagePublished; - - public static bool HasListeners => LogMessagePublished != null; - - public static void Publish(MqttNetLogMessage logMessage) - { - LogMessagePublished?.Invoke(null, new MqttNetLogMessagePublishedEventArgs(logMessage)); - } - } -} diff --git a/Source/MQTTnet/Diagnostics/MqttNetLogger.cs b/Source/MQTTnet/Diagnostics/MqttNetLogger.cs deleted file mode 100644 index 200ab88..0000000 --- a/Source/MQTTnet/Diagnostics/MqttNetLogger.cs +++ /dev/null @@ -1,73 +0,0 @@ -using System; - -namespace MQTTnet.Diagnostics -{ - public class MqttNetLogger : IMqttNetLogger - { - readonly string _logId; - - public MqttNetLogger() - { - } - - public MqttNetLogger(string logId) - { - _logId = logId; - } - - public event EventHandler LogMessagePublished; - - // TODO: Consider creating a LoggerFactory which will allow creating loggers. The logger factory will - // be the only place which has the published event. - public IMqttNetScopedLogger CreateScopedLogger(string source) - { - if (source is null) throw new ArgumentNullException(nameof(source)); - - return new MqttNetScopedLogger(this, source); - } - - public void Publish(MqttNetLogLevel level, string source, string message, object[] parameters, Exception exception) - { - var hasLocalListeners = LogMessagePublished != null; - var hasGlobalListeners = MqttNetGlobalLogger.HasListeners; - - if (!hasLocalListeners && !hasGlobalListeners) - { - return; - } - - if (parameters?.Length > 0 && message?.Length > 0) - { - try - { - message = string.Format(message, parameters); - } - catch (FormatException) - { - message = "MESSAGE FORMAT INVALID: " + message; - } - } - - var logMessage = new MqttNetLogMessage - { - LogId = _logId, - Timestamp = DateTime.UtcNow, - Source = source, - ThreadId = Environment.CurrentManagedThreadId, - Level = level, - Message = message, - Exception = exception - }; - - if (hasGlobalListeners) - { - MqttNetGlobalLogger.Publish(logMessage); - } - - if (hasLocalListeners) - { - LogMessagePublished?.Invoke(this, new MqttNetLogMessagePublishedEventArgs(logMessage)); - } - } - } -} \ No newline at end of file diff --git a/Source/MQTTnet/Diagnostics/MqttNetScopedLoggerExtensions.cs b/Source/MQTTnet/Diagnostics/MqttNetScopedLoggerExtensions.cs deleted file mode 100644 index 961e5cd..0000000 --- a/Source/MQTTnet/Diagnostics/MqttNetScopedLoggerExtensions.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System; - -namespace MQTTnet.Diagnostics -{ - public static class MqttNetScopedLoggerExtensions - { - public static void Verbose(this IMqttNetScopedLogger logger, string message, params object[] parameters) - { - logger.Publish(MqttNetLogLevel.Verbose, message, parameters, null); - } - - public static void Verbose(this IMqttNetScopedLogger logger, string message) - { - logger.Publish(MqttNetLogLevel.Verbose, message, null, null); - } - - public static void Info(this IMqttNetScopedLogger logger, string message, params object[] parameters) - { - logger.Publish(MqttNetLogLevel.Info, message, parameters, null); - } - - public static void Info(this IMqttNetScopedLogger logger, string message) - { - logger.Publish(MqttNetLogLevel.Info, message, null, null); - } - - public static void Warning(this IMqttNetScopedLogger logger, Exception exception, string message, params object[] parameters) - { - logger.Publish(MqttNetLogLevel.Warning, message, parameters, exception); - } - - public static void Warning(this IMqttNetScopedLogger logger, Exception exception, string message) - { - logger.Publish(MqttNetLogLevel.Warning, message, null, exception); - } - - public static void Warning(this IMqttNetScopedLogger logger, string message, params object[] parameters) - { - logger.Publish(MqttNetLogLevel.Warning, message, parameters, null); - } - - public static void Warning(this IMqttNetScopedLogger logger, string message) - { - logger.Publish(MqttNetLogLevel.Warning, message, null, null); - } - - public static void Error(this IMqttNetScopedLogger logger, Exception exception, string message, params object[] parameters) - { - logger.Publish(MqttNetLogLevel.Error, message, parameters, exception); - } - - public static void Error(this IMqttNetScopedLogger logger, Exception exception, string message) - { - logger.Publish(MqttNetLogLevel.Error, message, null, exception); - } - - public static void Error(this IMqttNetScopedLogger logger, string message) - { - logger.Publish(MqttNetLogLevel.Error, message, null, null); - } - } -} diff --git a/Source/MQTTnet/Diagnostics/TargetFrameworkProvider.cs b/Source/MQTTnet/Diagnostics/Runtime/TargetFrameworkProvider.cs similarity index 94% rename from Source/MQTTnet/Diagnostics/TargetFrameworkProvider.cs rename to Source/MQTTnet/Diagnostics/Runtime/TargetFrameworkProvider.cs index a5e181d..abe8631 100644 --- a/Source/MQTTnet/Diagnostics/TargetFrameworkProvider.cs +++ b/Source/MQTTnet/Diagnostics/Runtime/TargetFrameworkProvider.cs @@ -1,4 +1,4 @@ -namespace MQTTnet.Diagnostics +namespace MQTTnet.Diagnostics.Runtime { public static class TargetFrameworkProvider { diff --git a/Source/MQTTnet/IMqttFactory.cs b/Source/MQTTnet/IMqttFactory.cs index 8c0e5af..1c65f5c 100644 --- a/Source/MQTTnet/IMqttFactory.cs +++ b/Source/MQTTnet/IMqttFactory.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using MQTTnet.Client; using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; using MQTTnet.Server; namespace MQTTnet diff --git a/Source/MQTTnet/Implementations/MqttClientAdapterFactory.cs b/Source/MQTTnet/Implementations/MqttClientAdapterFactory.cs index 920f982..3d3d8ed 100644 --- a/Source/MQTTnet/Implementations/MqttClientAdapterFactory.cs +++ b/Source/MQTTnet/Implementations/MqttClientAdapterFactory.cs @@ -4,6 +4,7 @@ using MQTTnet.Diagnostics; using MQTTnet.Formatter; using System; using MQTTnet.Channel; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.Implementations { diff --git a/Source/MQTTnet/Implementations/MqttTcpServerAdapter.cs b/Source/MQTTnet/Implementations/MqttTcpServerAdapter.cs index 1900ff4..e3d495f 100644 --- a/Source/MQTTnet/Implementations/MqttTcpServerAdapter.cs +++ b/Source/MQTTnet/Implementations/MqttTcpServerAdapter.cs @@ -9,13 +9,14 @@ using System.Net.Sockets; using System.Security.Cryptography.X509Certificates; using System.Threading; using System.Threading.Tasks; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.Implementations { public sealed class MqttTcpServerAdapter : IMqttServerAdapter { readonly List _listeners = new List(); - readonly IMqttNetScopedLogger _logger; + readonly MqttNetSourceLogger _logger; readonly IMqttNetLogger _rootLogger; CancellationTokenSource _cancellationTokenSource; @@ -23,7 +24,7 @@ namespace MQTTnet.Implementations public MqttTcpServerAdapter(IMqttNetLogger logger) { _rootLogger = logger ?? throw new ArgumentNullException(nameof(logger)); - _logger = logger.CreateScopedLogger(nameof(MqttTcpServerAdapter)); + _logger = logger.WithSource(nameof(MqttTcpServerAdapter)); } public Func ClientHandler { get; set; } diff --git a/Source/MQTTnet/Implementations/MqttTcpServerListener.cs b/Source/MQTTnet/Implementations/MqttTcpServerListener.cs index 8f164fb..8d5ac31 100644 --- a/Source/MQTTnet/Implementations/MqttTcpServerListener.cs +++ b/Source/MQTTnet/Implementations/MqttTcpServerListener.cs @@ -12,12 +12,13 @@ using System.Net.Sockets; using System.Security.Cryptography.X509Certificates; using System.Threading; using System.Threading.Tasks; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.Implementations { public sealed class MqttTcpServerListener : IDisposable { - readonly IMqttNetScopedLogger _logger; + readonly MqttNetSourceLogger _logger; readonly IMqttNetLogger _rootLogger; readonly AddressFamily _addressFamily; readonly MqttServerTcpEndpointBaseOptions _options; @@ -37,7 +38,7 @@ namespace MQTTnet.Implementations _options = options; _tlsCertificate = tlsCertificate; _rootLogger = logger; - _logger = logger.CreateScopedLogger(nameof(MqttTcpServerListener)); + _logger = logger.WithSource(nameof(MqttTcpServerListener)); if (_options is MqttServerTlsTcpEndpointOptions tlsOptions) { diff --git a/Source/MQTTnet/Internal/TaskExtensions.cs b/Source/MQTTnet/Internal/TaskExtensions.cs index e789470..b10c3f5 100644 --- a/Source/MQTTnet/Internal/TaskExtensions.cs +++ b/Source/MQTTnet/Internal/TaskExtensions.cs @@ -1,11 +1,12 @@ using MQTTnet.Diagnostics; using System.Threading.Tasks; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.Internal { public static class TaskExtensions { - public static void RunInBackground(this Task task, IMqttNetScopedLogger logger = null) + public static void RunInBackground(this Task task, MqttNetSourceLogger logger = null) { task?.ContinueWith(t => { diff --git a/Source/MQTTnet/LowLevelClient/LowLevelMqttClient.cs b/Source/MQTTnet/LowLevelClient/LowLevelMqttClient.cs index 56a8642..2fa0e7c 100644 --- a/Source/MQTTnet/LowLevelClient/LowLevelMqttClient.cs +++ b/Source/MQTTnet/LowLevelClient/LowLevelMqttClient.cs @@ -5,12 +5,13 @@ using MQTTnet.Packets; using System; using System.Threading; using System.Threading.Tasks; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.LowLevelClient { public sealed class LowLevelMqttClient : ILowLevelMqttClient { - readonly IMqttNetScopedLogger _logger; + readonly MqttNetSourceLogger _logger; readonly IMqttClientAdapterFactory _clientAdapterFactory; IMqttChannelAdapter _adapter; @@ -21,7 +22,7 @@ namespace MQTTnet.LowLevelClient _clientAdapterFactory = clientAdapterFactory ?? throw new ArgumentNullException(nameof(clientAdapterFactory)); if (logger is null) throw new ArgumentNullException(nameof(logger)); - _logger = logger.CreateScopedLogger(nameof(LowLevelMqttClient)); + _logger = logger.WithSource(nameof(LowLevelMqttClient)); } bool IsConnected => _adapter != null; diff --git a/Source/MQTTnet/MqttFactory.cs b/Source/MQTTnet/MqttFactory.cs index cde9b0f..6a03659 100644 --- a/Source/MQTTnet/MqttFactory.cs +++ b/Source/MQTTnet/MqttFactory.cs @@ -1,6 +1,5 @@ using MQTTnet.Adapter; using MQTTnet.Client; -using MQTTnet.Diagnostics; using MQTTnet.Implementations; using MQTTnet.LowLevelClient; using MQTTnet.Server; @@ -10,6 +9,7 @@ using System.Linq; using MQTTnet.Client.Options; using MQTTnet.Client.Subscribing; using MQTTnet.Client.Unsubscribing; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet { @@ -17,7 +17,7 @@ namespace MQTTnet { IMqttClientAdapterFactory _clientAdapterFactory; - public MqttFactory() : this(new MqttNetLogger()) + public MqttFactory() : this(new MqttNetNullLogger()) { } diff --git a/Source/MQTTnet/Server/IMqttRetainedMessagesManager.cs b/Source/MQTTnet/Server/IMqttRetainedMessagesManager.cs index 0acc78a..4b85a7e 100644 --- a/Source/MQTTnet/Server/IMqttRetainedMessagesManager.cs +++ b/Source/MQTTnet/Server/IMqttRetainedMessagesManager.cs @@ -1,6 +1,7 @@ using MQTTnet.Diagnostics; using System.Collections.Generic; using System.Threading.Tasks; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.Server { diff --git a/Source/MQTTnet/Server/IMqttServerFactory.cs b/Source/MQTTnet/Server/IMqttServerFactory.cs index 39efaa3..a612c86 100644 --- a/Source/MQTTnet/Server/IMqttServerFactory.cs +++ b/Source/MQTTnet/Server/IMqttServerFactory.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using MQTTnet.Adapter; using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.Server { diff --git a/Source/MQTTnet/Server/Internal/MqttClientConnection.cs b/Source/MQTTnet/Server/Internal/MqttClientConnection.cs index 4cb0c53..9143c5c 100644 --- a/Source/MQTTnet/Server/Internal/MqttClientConnection.cs +++ b/Source/MQTTnet/Server/Internal/MqttClientConnection.cs @@ -6,6 +6,7 @@ using MQTTnet.Adapter; using MQTTnet.Client; using MQTTnet.Client.Disconnecting; using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; using MQTTnet.Exceptions; using MQTTnet.Formatter; using MQTTnet.Implementations; @@ -26,7 +27,7 @@ namespace MQTTnet.Server.Internal readonly MqttClientSessionsManager _sessionsManager; readonly IMqttChannelAdapter _channelAdapter; - readonly IMqttNetScopedLogger _logger; + readonly MqttNetSourceLogger _logger; readonly IMqttServerOptions _serverOptions; readonly string _endpoint; @@ -52,7 +53,7 @@ namespace MQTTnet.Server.Internal _connectPacket = connectPacket ?? throw new ArgumentNullException(nameof(connectPacket)); if (logger == null) throw new ArgumentNullException(nameof(logger)); - _logger = logger.CreateScopedLogger(nameof(MqttClientConnection)); + _logger = logger.WithSource(nameof(MqttClientConnection)); } public string ClientId => _connectPacket.ClientId; diff --git a/Source/MQTTnet/Server/Internal/MqttClientSessionsManager.cs b/Source/MQTTnet/Server/Internal/MqttClientSessionsManager.cs index a6c3591..33627e9 100644 --- a/Source/MQTTnet/Server/Internal/MqttClientSessionsManager.cs +++ b/Source/MQTTnet/Server/Internal/MqttClientSessionsManager.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using MQTTnet.Adapter; using MQTTnet.Client.Disconnecting; using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; using MQTTnet.Exceptions; using MQTTnet.Formatter; using MQTTnet.Internal; @@ -30,7 +31,7 @@ namespace MQTTnet.Server.Internal readonly IMqttRetainedMessagesManager _retainedMessagesManager; readonly IMqttServerOptions _options; - readonly IMqttNetScopedLogger _logger; + readonly MqttNetSourceLogger _logger; readonly IMqttNetLogger _rootLogger; public MqttClientSessionsManager( @@ -40,7 +41,7 @@ namespace MQTTnet.Server.Internal IMqttNetLogger logger) { if (logger == null) throw new ArgumentNullException(nameof(logger)); - _logger = logger.CreateScopedLogger(nameof(MqttClientSessionsManager)); + _logger = logger.WithSource(nameof(MqttClientSessionsManager)); _rootLogger = logger; _eventDispatcher = eventDispatcher ?? throw new ArgumentNullException(nameof(eventDispatcher)); @@ -448,7 +449,10 @@ namespace MQTTnet.Server.Internal return context; } - async Task CreateClientConnection(MqttConnectPacket connectPacket, IMqttChannelAdapter channelAdapter, IDictionary sessionItems) + async Task CreateClientConnection( + MqttConnectPacket connectPacket, + IMqttChannelAdapter channelAdapter, + IDictionary sessionItems) { MqttClientConnection connection; @@ -500,7 +504,9 @@ namespace MQTTnet.Server.Internal return connection; } - async Task InterceptApplicationMessageAsync(IMqttServerApplicationMessageInterceptor interceptor, MqttClientConnection clientConnection, + async Task InterceptApplicationMessageAsync( + IMqttServerApplicationMessageInterceptor interceptor, + MqttClientConnection clientConnection, MqttApplicationMessage applicationMessage) { string senderClientId; @@ -522,7 +528,6 @@ namespace MQTTnet.Server.Internal { ClientId = senderClientId, SessionItems = sessionItems, - Logger = _logger, AcceptPublish = true, ApplicationMessage = applicationMessage, CloseConnection = false diff --git a/Source/MQTTnet/Server/Internal/MqttRetainedMessagesManager.cs b/Source/MQTTnet/Server/Internal/MqttRetainedMessagesManager.cs index e6979ea..ebe8ee2 100644 --- a/Source/MQTTnet/Server/Internal/MqttRetainedMessagesManager.cs +++ b/Source/MQTTnet/Server/Internal/MqttRetainedMessagesManager.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; using MQTTnet.Implementations; using MQTTnet.Internal; @@ -14,14 +15,14 @@ namespace MQTTnet.Server.Internal readonly AsyncLock _storageAccessLock = new AsyncLock(); readonly Dictionary _messages = new Dictionary(4096); - IMqttNetScopedLogger _logger; + MqttNetSourceLogger _logger; IMqttServerOptions _options; // TODO: Get rid of the logger here! public Task Start(IMqttServerOptions options, IMqttNetLogger logger) { if (logger == null) throw new ArgumentNullException(nameof(logger)); - _logger = logger.CreateScopedLogger(nameof(MqttRetainedMessagesManager)); + _logger = logger.WithSource(nameof(MqttRetainedMessagesManager)); _options = options ?? throw new ArgumentNullException(nameof(options)); return PlatformAbstractionLayer.CompletedTask; diff --git a/Source/MQTTnet/Server/Internal/MqttServerEventDispatcher.cs b/Source/MQTTnet/Server/Internal/MqttServerEventDispatcher.cs index 4023c5a..97b9025 100644 --- a/Source/MQTTnet/Server/Internal/MqttServerEventDispatcher.cs +++ b/Source/MQTTnet/Server/Internal/MqttServerEventDispatcher.cs @@ -3,19 +3,20 @@ using System.Threading.Tasks; using MQTTnet.Adapter; using MQTTnet.Client.Receiving; using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; using MQTTnet.Packets; namespace MQTTnet.Server.Internal { public sealed class MqttServerEventDispatcher { - readonly IMqttNetScopedLogger _logger; + readonly MqttNetSourceLogger _logger; public MqttServerEventDispatcher(IMqttNetLogger logger) { if (logger is null) throw new ArgumentNullException(nameof(logger)); - _logger = logger.CreateScopedLogger(nameof(MqttServerEventDispatcher)); + _logger = logger.WithSource(nameof(MqttServerEventDispatcher)); } public IMqttServerClientConnectedHandler ClientConnectedHandler { get; set; } diff --git a/Source/MQTTnet/Server/Internal/MqttServerKeepAliveMonitor.cs b/Source/MQTTnet/Server/Internal/MqttServerKeepAliveMonitor.cs index e41acbd..abcc120 100644 --- a/Source/MQTTnet/Server/Internal/MqttServerKeepAliveMonitor.cs +++ b/Source/MQTTnet/Server/Internal/MqttServerKeepAliveMonitor.cs @@ -3,6 +3,7 @@ using System.Threading; using System.Threading.Tasks; using MQTTnet.Client.Disconnecting; using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; using MQTTnet.Implementations; using MQTTnet.Internal; @@ -12,7 +13,7 @@ namespace MQTTnet.Server.Internal { readonly IMqttServerOptions _options; readonly MqttClientSessionsManager _sessionsManager; - readonly IMqttNetScopedLogger _logger; + readonly MqttNetSourceLogger _logger; public MqttServerKeepAliveMonitor(IMqttServerOptions options, MqttClientSessionsManager sessionsManager, IMqttNetLogger logger) { @@ -20,7 +21,7 @@ namespace MQTTnet.Server.Internal _sessionsManager = sessionsManager ?? throw new ArgumentNullException(nameof(sessionsManager)); if (logger == null) throw new ArgumentNullException(nameof(logger)); - _logger = logger.CreateScopedLogger(nameof(MqttServerKeepAliveMonitor)); + _logger = logger.WithSource(nameof(MqttServerKeepAliveMonitor)); } public void Start(CancellationToken cancellationToken) diff --git a/Source/MQTTnet/Server/MqttApplicationMessageInterceptorContext.cs b/Source/MQTTnet/Server/MqttApplicationMessageInterceptorContext.cs index c4df979..1ab4ee4 100644 --- a/Source/MQTTnet/Server/MqttApplicationMessageInterceptorContext.cs +++ b/Source/MQTTnet/Server/MqttApplicationMessageInterceptorContext.cs @@ -1,15 +1,9 @@ using System.Collections.Generic; -using MQTTnet.Diagnostics; namespace MQTTnet.Server { public sealed class MqttApplicationMessageInterceptorContext - { - /// - /// Gets the currently used logger. - /// - public IMqttNetScopedLogger Logger { get; internal set; } - + { /// /// Gets the client identifier. /// Hint: This identifier needs to be unique over all used clients / devices on the broker to avoid connection issues. diff --git a/Source/MQTTnet/Server/MqttServer.cs b/Source/MQTTnet/Server/MqttServer.cs index 1b42feb..54827e2 100644 --- a/Source/MQTTnet/Server/MqttServer.cs +++ b/Source/MQTTnet/Server/MqttServer.cs @@ -10,6 +10,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using MQTTnet.Diagnostics.Logger; using MQTTnet.Implementations; using MQTTnet.Internal; using MQTTnet.Server.Internal; @@ -21,7 +22,7 @@ namespace MQTTnet.Server readonly MqttServerEventDispatcher _eventDispatcher; readonly ICollection _adapters; readonly IMqttNetLogger _rootLogger; - readonly IMqttNetScopedLogger _logger; + readonly MqttNetSourceLogger _logger; MqttClientSessionsManager _clientSessionsManager; IMqttRetainedMessagesManager _retainedMessagesManager; @@ -34,7 +35,7 @@ namespace MQTTnet.Server _adapters = adapters.ToList(); if (logger == null) throw new ArgumentNullException(nameof(logger)); - _logger = logger.CreateScopedLogger(nameof(MqttServer)); + _logger = logger.WithSource(nameof(MqttServer)); _rootLogger = logger; _eventDispatcher = new MqttServerEventDispatcher(logger); diff --git a/Source/MQTTnet/Server/MqttServerMultiThreadedApplicationMessageInterceptorDelegate.cs b/Source/MQTTnet/Server/MqttServerMultiThreadedApplicationMessageInterceptorDelegate.cs index 9f8bf4f..bd7b6ec 100644 --- a/Source/MQTTnet/Server/MqttServerMultiThreadedApplicationMessageInterceptorDelegate.cs +++ b/Source/MQTTnet/Server/MqttServerMultiThreadedApplicationMessageInterceptorDelegate.cs @@ -1,6 +1,5 @@ using System; using System.Threading.Tasks; -using MQTTnet.Diagnostics; using MQTTnet.Implementations; using MQTTnet.Internal; @@ -21,6 +20,8 @@ namespace MQTTnet.Server }; } + public Func ExceptionHandler { get; set; } + public MqttServerMultiThreadedApplicationMessageInterceptorDelegate(Func callback) { _callback = callback ?? throw new ArgumentNullException(nameof(callback)); @@ -36,7 +37,11 @@ namespace MQTTnet.Server } catch (Exception exception) { - context.Logger.Error(exception, "Error while intercepting application message."); + var exceptionHandler = ExceptionHandler; + if (exceptionHandler != null) + { + await exceptionHandler.Invoke(context, exception).ConfigureAwait(false); + } } }).RunInBackground(); diff --git a/Tests/MQTTnet.Benchmarks/ChannelAdapterBenchmark.cs b/Tests/MQTTnet.Benchmarks/ChannelAdapterBenchmark.cs index d6906f4..34a05a9 100644 --- a/Tests/MQTTnet.Benchmarks/ChannelAdapterBenchmark.cs +++ b/Tests/MQTTnet.Benchmarks/ChannelAdapterBenchmark.cs @@ -1,22 +1,22 @@ using BenchmarkDotNet.Attributes; using MQTTnet.Adapter; -using MQTTnet.Diagnostics; using MQTTnet.Internal; using MQTTnet.Packets; using System; using System.IO; using System.Threading; +using MQTTnet.Diagnostics.Logger; using MQTTnet.Formatter; namespace MQTTnet.Benchmarks { [MemoryDiagnoser] - public class ChannelAdapterBenchmark + public sealed class ChannelAdapterBenchmark { - private MqttChannelAdapter _channelAdapter; - private int _iterations; - private MemoryStream _stream; - private MqttPublishPacket _packet; + MqttChannelAdapter _channelAdapter; + int _iterations; + MemoryStream _stream; + MqttPublishPacket _packet; [GlobalSetup] public void Setup() @@ -43,7 +43,7 @@ namespace MQTTnet.Benchmarks var channel = new TestMqttChannel(_stream); - _channelAdapter = new MqttChannelAdapter(channel, serializer, null, new MqttNetLogger()); + _channelAdapter = new MqttChannelAdapter(channel, serializer, null, new MqttNetEventLogger()); } [Benchmark] @@ -72,7 +72,7 @@ namespace MQTTnet.Benchmarks _stream.Position = 0; } - private static byte[] Join(params ArraySegment[] chunks) + static byte[] Join(params ArraySegment[] chunks) { var buffer = new MemoryStream(); foreach (var chunk in chunks) diff --git a/Tests/MQTTnet.Benchmarks/Configurations/AllowNonOptimized.cs b/Tests/MQTTnet.Benchmarks/Configurations/AllowNonOptimized.cs index 248c52f..c40eb09 100644 --- a/Tests/MQTTnet.Benchmarks/Configurations/AllowNonOptimized.cs +++ b/Tests/MQTTnet.Benchmarks/Configurations/AllowNonOptimized.cs @@ -1,6 +1,4 @@ -using System.Linq; -using BenchmarkDotNet.Configs; -using BenchmarkDotNet.Jobs; +using BenchmarkDotNet.Jobs; using BenchmarkDotNet.Validators; namespace MQTTnet.Benchmarks.Configurations diff --git a/Tests/MQTTnet.Benchmarks/Configurations/RuntimeCompareConfig.cs b/Tests/MQTTnet.Benchmarks/Configurations/RuntimeCompareConfig.cs index 60f009b..d9f5909 100644 --- a/Tests/MQTTnet.Benchmarks/Configurations/RuntimeCompareConfig.cs +++ b/Tests/MQTTnet.Benchmarks/Configurations/RuntimeCompareConfig.cs @@ -1,5 +1,4 @@ -using BenchmarkDotNet.Configs; -using BenchmarkDotNet.Environments; +using BenchmarkDotNet.Environments; using BenchmarkDotNet.Jobs; using BenchmarkDotNet.Toolchains.CsProj; diff --git a/Tests/MQTTnet.Benchmarks/LoggerBenchmark.cs b/Tests/MQTTnet.Benchmarks/LoggerBenchmark.cs index 20a8f01..f31f89e 100644 --- a/Tests/MQTTnet.Benchmarks/LoggerBenchmark.cs +++ b/Tests/MQTTnet.Benchmarks/LoggerBenchmark.cs @@ -1,6 +1,6 @@ using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Jobs; -using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.Benchmarks { @@ -9,17 +9,29 @@ namespace MQTTnet.Benchmarks [MemoryDiagnoser] public class LoggerBenchmark { - MqttNetLogger _logger; - IMqttNetScopedLogger _childLogger; + MqttNetNullLogger _nullLogger; + MqttNetSourceLogger _sourceNullLogger; + + MqttNetEventLogger _eventLogger; + MqttNetSourceLogger _sourceEventLogger; + + MqttNetEventLogger _eventLoggerNoListener; + MqttNetSourceLogger _sourceEventLoggerNoListener; + bool _useHandler; [GlobalSetup] public void Setup() { - _logger = new MqttNetLogger(); - _childLogger = _logger.CreateScopedLogger("child"); - - _logger.LogMessagePublished += OnLogMessagePublished; + _nullLogger = new MqttNetNullLogger(); + _sourceNullLogger = _nullLogger.WithSource("Source"); + + _eventLogger = new MqttNetEventLogger(); + _eventLogger.LogMessagePublished += OnLogMessagePublished; + _sourceEventLogger = _eventLogger.WithSource("Source"); + + _eventLoggerNoListener = new MqttNetEventLogger(); + _sourceEventLoggerNoListener = _eventLoggerNoListener.WithSource("Source"); } void OnLogMessagePublished(object sender, MqttNetLogMessagePublishedEventArgs eventArgs) @@ -31,22 +43,44 @@ namespace MQTTnet.Benchmarks } [Benchmark] - public void Log_10000_Messages_NoHandler() + public void Log_10000_Messages_No_Listener() { _useHandler = false; + for (var i = 0; i < 10000; i++) { - _childLogger.Verbose("test log message {0}", "parameter"); + _sourceEventLoggerNoListener.Verbose("test log message {0}", "parameter"); } } [Benchmark] - public void Log_10000_Messages_WithHandler() + public void Log_10000_Messages_With_To_String() { _useHandler = true; + + for (var i = 0; i < 10000; i++) + { + _sourceEventLogger.Verbose("test log message {0}", "parameter"); + } + } + + [Benchmark] + public void Log_10000_Messages_Without_To_String() + { + _useHandler = false; + + for (var i = 0; i < 10000; i++) + { + _sourceEventLogger.Verbose("test log message {0}", "parameter"); + } + } + + [Benchmark] + public void Log_10000_Messages_With_NullLogger() + { for (var i = 0; i < 10000; i++) { - _childLogger.Verbose("test log message {0}", "parameter"); + _sourceNullLogger.Verbose("test log message {0}", "parameter"); } } } diff --git a/Tests/MQTTnet.Benchmarks/MessageProcessingBenchmark.cs b/Tests/MQTTnet.Benchmarks/MessageProcessingBenchmark.cs index 99fe030..91718f1 100644 --- a/Tests/MQTTnet.Benchmarks/MessageProcessingBenchmark.cs +++ b/Tests/MQTTnet.Benchmarks/MessageProcessingBenchmark.cs @@ -11,9 +11,9 @@ namespace MQTTnet.Benchmarks [MemoryDiagnoser] public class MessageProcessingBenchmark { - private IMqttServer _mqttServer; - private IMqttClient _mqttClient; - private MqttApplicationMessage _message; + IMqttServer _mqttServer; + IMqttClient _mqttClient; + MqttApplicationMessage _message; [GlobalSetup] public void Setup() diff --git a/Tests/MQTTnet.Benchmarks/MessageProcessingMqttConnectionContextBenchmark.cs b/Tests/MQTTnet.Benchmarks/MessageProcessingMqttConnectionContextBenchmark.cs index d14f7f4..651f400 100644 --- a/Tests/MQTTnet.Benchmarks/MessageProcessingMqttConnectionContextBenchmark.cs +++ b/Tests/MQTTnet.Benchmarks/MessageProcessingMqttConnectionContextBenchmark.cs @@ -1,25 +1,20 @@ using BenchmarkDotNet.Attributes; using MQTTnet.Client; - - -using MQTTnet.AspNetCore; - using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; -using MQTTnet.Server; -using MQTTnet.Diagnostics; using MQTTnet.AspNetCore.Client; using MQTTnet.AspNetCore.Extensions; using MQTTnet.Client.Options; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.Benchmarks { [MemoryDiagnoser] public class MessageProcessingMqttConnectionContextBenchmark { - private IWebHost _host; - private IMqttClient _mqttClient; - private MqttApplicationMessage _message; + IWebHost _host; + IMqttClient _mqttClient; + MqttApplicationMessage _message; [GlobalSetup] public void Setup() @@ -39,7 +34,7 @@ namespace MQTTnet.Benchmarks .Build(); var factory = new MqttFactory(); - _mqttClient = factory.CreateMqttClient(new MqttNetLogger(), new MqttClientConnectionContextFactory()); + _mqttClient = factory.CreateMqttClient(new MqttNetEventLogger(), new MqttClientConnectionContextFactory()); _host.StartAsync().GetAwaiter().GetResult(); diff --git a/Tests/MQTTnet.Benchmarks/MqttTcpChannelBenchmark.cs b/Tests/MQTTnet.Benchmarks/MqttTcpChannelBenchmark.cs index ecfa76e..32ca9e7 100644 --- a/Tests/MQTTnet.Benchmarks/MqttTcpChannelBenchmark.cs +++ b/Tests/MQTTnet.Benchmarks/MqttTcpChannelBenchmark.cs @@ -1,26 +1,26 @@ using BenchmarkDotNet.Attributes; using MQTTnet.Channel; using MQTTnet.Client.Options; -using MQTTnet.Diagnostics; using MQTTnet.Implementations; using MQTTnet.Server; using System.Threading; using System.Threading.Tasks; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.Benchmarks { [MemoryDiagnoser] - public class MqttTcpChannelBenchmark + public sealed class MqttTcpChannelBenchmark { - private IMqttServer _mqttServer; - private IMqttChannel _serverChannel; - private IMqttChannel _clientChannel; + IMqttServer _mqttServer; + IMqttChannel _serverChannel; + IMqttChannel _clientChannel; [GlobalSetup] public void Setup() { var factory = new MqttFactory(); - var tcpServer = new MqttTcpServerAdapter(new MqttNetLogger()); + var tcpServer = new MqttTcpServerAdapter(new MqttNetEventLogger()); tcpServer.ClientHandler += args => { _serverChannel = @@ -31,7 +31,7 @@ namespace MQTTnet.Benchmarks return Task.CompletedTask; }; - _mqttServer = factory.CreateMqttServer(new[] { tcpServer }, new MqttNetLogger()); + _mqttServer = factory.CreateMqttServer(new[] { tcpServer }, new MqttNetEventLogger()); var serverOptions = new MqttServerOptionsBuilder().Build(); _mqttServer.StartAsync(serverOptions).GetAwaiter().GetResult(); @@ -54,7 +54,7 @@ namespace MQTTnet.Benchmarks await Task.WhenAll(WriteAsync(iterations, size), ReadAsync(iterations, size)); } - private async Task ReadAsync(int iterations, int size) + async Task ReadAsync(int iterations, int size) { await Task.Yield(); @@ -68,7 +68,7 @@ namespace MQTTnet.Benchmarks } } - private async Task WriteAsync(int iterations, int size) + async Task WriteAsync(int iterations, int size) { await Task.Yield(); @@ -78,4 +78,4 @@ namespace MQTTnet.Benchmarks } } } -} +} \ No newline at end of file diff --git a/Tests/MQTTnet.Benchmarks/Program.cs b/Tests/MQTTnet.Benchmarks/Program.cs index 760cb52..b2d1919 100644 --- a/Tests/MQTTnet.Benchmarks/Program.cs +++ b/Tests/MQTTnet.Benchmarks/Program.cs @@ -1,7 +1,7 @@ using System; using BenchmarkDotNet.Running; using MQTTnet.Benchmarks.Configurations; -using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Runtime; namespace MQTTnet.Benchmarks { diff --git a/Tests/MQTTnet.Benchmarks/SerializerBenchmark.cs b/Tests/MQTTnet.Benchmarks/SerializerBenchmark.cs index ff567e8..e2b2bcd 100644 --- a/Tests/MQTTnet.Benchmarks/SerializerBenchmark.cs +++ b/Tests/MQTTnet.Benchmarks/SerializerBenchmark.cs @@ -9,7 +9,7 @@ using MQTTnet.Channel; using MQTTnet.Formatter; using MQTTnet.Formatter.V3; using BenchmarkDotNet.Jobs; -using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.Benchmarks { @@ -18,9 +18,9 @@ namespace MQTTnet.Benchmarks [MemoryDiagnoser] public class SerializerBenchmark { - private MqttBasePacket _packet; - private ArraySegment _serializedPacket; - private IMqttPacketFormatter _serializer; + MqttBasePacket _packet; + ArraySegment _serializedPacket; + IMqttPacketFormatter _serializer; [GlobalSetup] public void Setup() @@ -49,7 +49,7 @@ namespace MQTTnet.Benchmarks { var channel = new BenchmarkMqttChannel(_serializedPacket); var fixedHeader = new byte[2]; - var reader = new MqttChannelAdapter(channel, new MqttPacketFormatterAdapter(new MqttPacketWriter()), null, new MqttNetLogger()); + var reader = new MqttChannelAdapter(channel, new MqttPacketFormatterAdapter(new MqttPacketWriter()), null, new MqttNetEventLogger()); for (var i = 0; i < 10000; i++) { @@ -59,10 +59,10 @@ namespace MQTTnet.Benchmarks } } - private class BenchmarkMqttChannel : IMqttChannel + class BenchmarkMqttChannel : IMqttChannel { - private readonly ArraySegment _buffer; - private int _position; + readonly ArraySegment _buffer; + int _position; public BenchmarkMqttChannel(ArraySegment buffer) { diff --git a/Tests/MQTTnet.Benchmarks/TcpPipesBenchmark.cs b/Tests/MQTTnet.Benchmarks/TcpPipesBenchmark.cs index 4252ab3..636d693 100644 --- a/Tests/MQTTnet.Benchmarks/TcpPipesBenchmark.cs +++ b/Tests/MQTTnet.Benchmarks/TcpPipesBenchmark.cs @@ -11,8 +11,8 @@ namespace MQTTnet.Benchmarks [MemoryDiagnoser] public class TcpPipesBenchmark { - private IDuplexPipe _client; - private IDuplexPipe _server; + IDuplexPipe _client; + IDuplexPipe _server; [GlobalSetup] public void Setup() @@ -43,7 +43,7 @@ namespace MQTTnet.Benchmarks await Task.WhenAll(WriteAsync(iterations, size), ReadAsync(iterations, size)); } - private async Task ReadAsync(int iterations, int size) + async Task ReadAsync(int iterations, int size) { await Task.Yield(); @@ -59,7 +59,7 @@ namespace MQTTnet.Benchmarks } } - private async Task WriteAsync(int iterations, int size) + async Task WriteAsync(int iterations, int size) { await Task.Yield(); diff --git a/Tests/MQTTnet.Benchmarks/TopicFilterComparerBenchmark.cs b/Tests/MQTTnet.Benchmarks/TopicFilterComparerBenchmark.cs index 92365c3..e6a09aa 100644 --- a/Tests/MQTTnet.Benchmarks/TopicFilterComparerBenchmark.cs +++ b/Tests/MQTTnet.Benchmarks/TopicFilterComparerBenchmark.cs @@ -1,6 +1,5 @@ using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Jobs; -using MQTTnet.Server; using System; using MQTTnet.Server.Internal; @@ -11,7 +10,7 @@ namespace MQTTnet.Benchmarks [MemoryDiagnoser] public class TopicFilterComparerBenchmark { - private static readonly char[] TopicLevelSeparator = { '/' }; + static readonly char[] TopicLevelSeparator = { '/' }; [GlobalSetup] public void Setup() @@ -48,7 +47,7 @@ namespace MQTTnet.Benchmarks } } - private static bool LegacyMethodByStringSplit(string topic, string filter) + static bool LegacyMethodByStringSplit(string topic, string filter) { if (topic == null) throw new ArgumentNullException(nameof(topic)); if (filter == null) throw new ArgumentNullException(nameof(filter)); diff --git a/Tests/MQTTnet.Core.Tests/LowLevelMqttClient_Tests.cs b/Tests/MQTTnet.Core.Tests/Client/LowLevelMqttClient_Tests.cs similarity index 99% rename from Tests/MQTTnet.Core.Tests/LowLevelMqttClient_Tests.cs rename to Tests/MQTTnet.Core.Tests/Client/LowLevelMqttClient_Tests.cs index 40ce57d..814adca 100644 --- a/Tests/MQTTnet.Core.Tests/LowLevelMqttClient_Tests.cs +++ b/Tests/MQTTnet.Core.Tests/Client/LowLevelMqttClient_Tests.cs @@ -1,18 +1,18 @@ using System; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using MQTTnet.Client.Options; -using MQTTnet.LowLevelClient; -using MQTTnet.Packets; -using MQTTnet.Protocol; -using MQTTnet.Tests.Mockups; using System.Collections.Generic; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MQTTnet.Client.Options; using MQTTnet.Exceptions; +using MQTTnet.LowLevelClient; +using MQTTnet.Packets; +using MQTTnet.Protocol; +using MQTTnet.Tests.Mockups; -namespace MQTTnet.Tests +namespace MQTTnet.Tests.Client { [TestClass] public class LowLevelMqttClient_Tests diff --git a/Tests/MQTTnet.Core.Tests/ManagedMqttClient_Tests.cs b/Tests/MQTTnet.Core.Tests/Client/ManagedMqttClient_Tests.cs similarity index 97% rename from Tests/MQTTnet.Core.Tests/ManagedMqttClient_Tests.cs rename to Tests/MQTTnet.Core.Tests/Client/ManagedMqttClient_Tests.cs index 29a9df5..7f29468 100644 --- a/Tests/MQTTnet.Core.Tests/ManagedMqttClient_Tests.cs +++ b/Tests/MQTTnet.Core.Tests/Client/ManagedMqttClient_Tests.cs @@ -1,19 +1,20 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; using MQTTnet.Client; using MQTTnet.Client.Connecting; using MQTTnet.Client.Options; using MQTTnet.Client.Receiving; using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; using MQTTnet.Extensions.ManagedClient; using MQTTnet.Server; using MQTTnet.Tests.Mockups; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -namespace MQTTnet.Tests +namespace MQTTnet.Tests.Client { [TestClass] public class ManagedMqttClient_Tests @@ -92,7 +93,7 @@ namespace MQTTnet.Tests { var server = await testEnvironment.StartServer(); - var managedClient = new ManagedMqttClient(testEnvironment.CreateClient(), new MqttNetLogger()); + var managedClient = new ManagedMqttClient(testEnvironment.CreateClient(), new MqttNetEventLogger()); var clientOptions = new MqttClientOptionsBuilder() .WithTcpServer("localhost", testEnvironment.ServerPort); @@ -122,7 +123,7 @@ namespace MQTTnet.Tests await testEnvironment.StartServer(); - var managedClient = new ManagedMqttClient(testEnvironment.CreateClient(), new MqttNetLogger()); + var managedClient = new ManagedMqttClient(testEnvironment.CreateClient(), new MqttNetEventLogger()); var clientOptions = new MqttClientOptionsBuilder() .WithTcpServer("localhost", testEnvironment.ServerPort); var storage = new ManagedMqttClientTestStorage(); @@ -303,17 +304,17 @@ namespace MQTTnet.Tests }); // Wait a bit for the retained message to be available - await Task.Delay(500); + await Task.Delay(1000); await sendingClient.DisconnectAsync(); // Now use the managed client and check if subscriptions get cleared properly. var clientOptions = new MqttClientOptionsBuilder() - .WithTcpServer("localhost", testEnvironment.ServerPort); + .WithTcpServer("127.0.0.1", testEnvironment.ServerPort); var receivedManagedMessages = new List(); - var managedClient = new ManagedMqttClient(testEnvironment.CreateClient(), new MqttNetLogger()); + var managedClient = new ManagedMqttClient(testEnvironment.CreateClient(), new MqttNetEventLogger()); managedClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(c => { receivedManagedMessages.Add(c.ApplicationMessage); @@ -326,7 +327,7 @@ namespace MQTTnet.Tests .WithAutoReconnectDelay(TimeSpan.FromSeconds(1)) .Build()); - await Task.Delay(500); + await Task.Delay(1000); Assert.AreEqual(1, receivedManagedMessages.Count); @@ -431,7 +432,7 @@ namespace MQTTnet.Tests // at connection check intervals managedOptions.ConnectionCheckInterval = connectionCheckInterval ?? TimeSpan.FromSeconds(0.1); - var managedClient = new ManagedMqttClient(underlyingClient ?? testEnvironment.CreateClient(), new MqttNetLogger()); + var managedClient = new ManagedMqttClient(underlyingClient ?? testEnvironment.CreateClient(), new MqttNetEventLogger()); var connected = GetConnectedTask(managedClient); diff --git a/Tests/MQTTnet.Core.Tests/MqttClientOptionsBuilder_Tests.cs b/Tests/MQTTnet.Core.Tests/Client/MqttClientOptionsBuilder_Tests.cs similarity index 95% rename from Tests/MQTTnet.Core.Tests/MqttClientOptionsBuilder_Tests.cs rename to Tests/MQTTnet.Core.Tests/Client/MqttClientOptionsBuilder_Tests.cs index a482081..56748bb 100644 --- a/Tests/MQTTnet.Core.Tests/MqttClientOptionsBuilder_Tests.cs +++ b/Tests/MQTTnet.Core.Tests/Client/MqttClientOptionsBuilder_Tests.cs @@ -4,7 +4,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using MQTTnet.Client.Options; using MQTTnet.Extensions; -namespace MQTTnet.Tests +namespace MQTTnet.Tests.Client { [TestClass] public class MqttClientOptionsBuilder_Tests diff --git a/Tests/MQTTnet.Core.Tests/MqttClient_Tests.cs b/Tests/MQTTnet.Core.Tests/Client/MqttClient_Tests.cs similarity index 99% rename from Tests/MQTTnet.Core.Tests/MqttClient_Tests.cs rename to Tests/MQTTnet.Core.Tests/Client/MqttClient_Tests.cs index c1efd49..0e188f0 100644 --- a/Tests/MQTTnet.Core.Tests/MqttClient_Tests.cs +++ b/Tests/MQTTnet.Core.Tests/Client/MqttClient_Tests.cs @@ -1,13 +1,3 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using MQTTnet.Client; -using MQTTnet.Client.Connecting; -using MQTTnet.Client.Disconnecting; -using MQTTnet.Client.Options; -using MQTTnet.Client.Subscribing; -using MQTTnet.Exceptions; -using MQTTnet.Protocol; -using MQTTnet.Server; -using MQTTnet.Tests.Mockups; using System; using System.Collections.Generic; using System.Linq; @@ -15,11 +5,21 @@ using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MQTTnet.Client; +using MQTTnet.Client.Connecting; +using MQTTnet.Client.Disconnecting; +using MQTTnet.Client.Options; using MQTTnet.Client.Receiving; +using MQTTnet.Client.Subscribing; +using MQTTnet.Exceptions; using MQTTnet.Formatter; using MQTTnet.Packets; +using MQTTnet.Protocol; +using MQTTnet.Server; +using MQTTnet.Tests.Mockups; -namespace MQTTnet.Tests +namespace MQTTnet.Tests.Client { [TestClass] public class Client_Tests diff --git a/Tests/MQTTnet.Core.Tests/Factory/MqttFactory_Tests.cs b/Tests/MQTTnet.Core.Tests/Factory/MqttFactory_Tests.cs new file mode 100644 index 0000000..d7b5b27 --- /dev/null +++ b/Tests/MQTTnet.Core.Tests/Factory/MqttFactory_Tests.cs @@ -0,0 +1,154 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MQTTnet.Diagnostics.Logger; +using MQTTnet.Extensions.ManagedClient; + +namespace MQTTnet.Tests.Factory +{ + [TestClass] + public sealed class MqttFactory_Tests : BaseTestClass + { + [TestMethod] + public async Task Create_Managed_Client_With_Logger() + { + var factory = new MqttFactory(); + + // This test compares + // 1. correct logID + var logId = "logId"; + var hasInvalidLogId = false; + + // 2. if the total log calls are the same for global and local + //var globalLogCount = 0; + var localLogCount = 0; + + var logger = new MqttNetEventLogger(logId); + logger.LogMessagePublished += (s, e) => + { + if (e.LogMessage.LogId != logId) + { + hasInvalidLogId = true; + } + + Interlocked.Increment(ref localLogCount); + }; + + var managedClient = factory.CreateManagedMqttClient(logger); + try + { + var clientOptions = new ManagedMqttClientOptionsBuilder(); + + clientOptions.WithClientOptions(o => o.WithTcpServer("this_is_an_invalid_host").WithCommunicationTimeout(TimeSpan.FromSeconds(1))); + + // try connect to get some log entries + await managedClient.StartAsync(clientOptions.Build()); + + // wait at least connect timeout or we have some log messages + var tcs = new TaskCompletionSource(); + managedClient.ConnectingFailedHandler = new ConnectingFailedHandlerDelegate(e => tcs.TrySetResult(null)); + await Task.WhenAny(Task.Delay(managedClient.Options.ClientOptions.CommunicationTimeout), tcs.Task); + } + finally + { + await managedClient.StopAsync(); + } + + await Task.Delay(500); + + Assert.IsFalse(hasInvalidLogId); + Assert.AreNotEqual(0, localLogCount); + } + + [TestMethod] + public void Create_ApplicationMessageBuilder() + { + var factory = new MqttFactory(); + var builder = factory.CreateApplicationMessageBuilder(); + + Assert.IsNotNull(builder); + } + + [TestMethod] + public void Create_ClientOptionsBuilder() + { + var factory = new MqttFactory(); + var builder = factory.CreateClientOptionsBuilder(); + + Assert.IsNotNull(builder); + } + + [TestMethod] + public void Create_ServerOptionsBuilder() + { + var factory = new MqttFactory(); + var builder = factory.CreateServerOptionsBuilder(); + + Assert.IsNotNull(builder); + } + + [TestMethod] + public void Create_SubscribeOptionsBuilder() + { + var factory = new MqttFactory(); + var builder = factory.CreateSubscribeOptionsBuilder(); + + Assert.IsNotNull(builder); + } + + [TestMethod] + public void Create_UnsubscribeOptionsBuilder() + { + var factory = new MqttFactory(); + var builder = factory.CreateUnsubscribeOptionsBuilder(); + + Assert.IsNotNull(builder); + } + + [TestMethod] + public void Create_TopicFilterBuilder() + { + var factory = new MqttFactory(); + var builder = factory.CreateTopicFilterBuilder(); + + Assert.IsNotNull(builder); + } + + [TestMethod] + public void Create_MqttServer() + { + var factory = new MqttFactory(); + var server = factory.CreateMqttServer(); + + Assert.IsNotNull(server); + } + + [TestMethod] + public void Create_MqttClient() + { + var factory = new MqttFactory(); + var client = factory.CreateMqttClient(); + + Assert.IsNotNull(client); + } + + [TestMethod] + public void Create_LowLevelMqttClient() + { + var factory = new MqttFactory(); + var client = factory.CreateLowLevelMqttClient(); + + Assert.IsNotNull(client); + } + + [TestMethod] + public void Create_ManagedMqttClient() + { + var factory = new MqttFactory(); + var client = factory.CreateManagedMqttClient(); + + Assert.IsNotNull(client); + } + } +} \ No newline at end of file diff --git a/Tests/MQTTnet.Core.Tests/Logger/Logger_Tests.cs b/Tests/MQTTnet.Core.Tests/Logger/Logger_Tests.cs new file mode 100644 index 0000000..ce029ae --- /dev/null +++ b/Tests/MQTTnet.Core.Tests/Logger/Logger_Tests.cs @@ -0,0 +1,62 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MQTTnet.Diagnostics.Logger; + +namespace MQTTnet.Tests.Logger +{ + [TestClass] + public sealed class Logger_Tests : BaseTestClass + { + [TestMethod] + public void Log_Without_Source() + { + var logger = new MqttNetEventLogger(); + + MqttNetLogMessage logMessage = null; + logger.LogMessagePublished += (s, e) => { logMessage = e.LogMessage; }; + + logger.Publish(MqttNetLogLevel.Info, "SOURCE", "MESSAGE", new object[] { "ABC" }, new InvalidOperationException()); + + Assert.AreEqual(MqttNetLogLevel.Info, logMessage.Level); + Assert.AreEqual("SOURCE", logMessage.Source); + Assert.AreEqual("MESSAGE", logMessage.Message); + Assert.AreEqual("InvalidOperationException", logMessage.Exception.GetType().Name); + } + + [TestMethod] + public void Root_Log_Messages() + { + var logger = new MqttNetEventLogger(); + var childLogger = logger.WithSource("Source1"); + + var logMessagesCount = 0; + + logger.LogMessagePublished += (s, e) => { logMessagesCount++; }; + + childLogger.Verbose("Verbose"); + childLogger.Info("Info"); + childLogger.Warning(null, "Warning"); + childLogger.Error(null, "Error"); + + Assert.AreEqual(4, logMessagesCount); + } + + [TestMethod] + public void Use_Custom_Log_Id() + { + var logger = new MqttNetEventLogger("logId"); + var childLogger = logger.WithSource("Source1"); + + logger.LogMessagePublished += (s, e) => + { + Assert.AreEqual("logId", e.LogMessage.LogId); + Assert.AreEqual("Source1", e.LogMessage.Source); + }; + + childLogger.Verbose("Verbose"); + childLogger.Info("Info"); + childLogger.Warning(null, "Warning"); + childLogger.Error(null, "Error"); + } + } +} \ No newline at end of file diff --git a/Tests/MQTTnet.Core.Tests/Logger/SourceLogger_Tests.cs b/Tests/MQTTnet.Core.Tests/Logger/SourceLogger_Tests.cs new file mode 100644 index 0000000..4b825aa --- /dev/null +++ b/Tests/MQTTnet.Core.Tests/Logger/SourceLogger_Tests.cs @@ -0,0 +1,26 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MQTTnet.Diagnostics.Logger; + +namespace MQTTnet.Tests.Logger +{ + [TestClass] + public sealed class SourceLogger_Tests : BaseTestClass + { + [TestMethod] + public void Log_With_Source() + { + MqttNetLogMessage logMessage = null; + + var logger = new MqttNetEventLogger(); + logger.LogMessagePublished += (s, e) => + { + logMessage = e.LogMessage; + }; + + var sourceLogger = logger.WithSource("The_Source"); + sourceLogger.Info("MESSAGE", null, null); + + Assert.AreEqual("The_Source", logMessage.Source); + } + } +} \ No newline at end of file diff --git a/Tests/MQTTnet.Core.Tests/Mockups/TestEnvironment.cs b/Tests/MQTTnet.Core.Tests/Mockups/TestEnvironment.cs index df07ebf..3d00abc 100644 --- a/Tests/MQTTnet.Core.Tests/Mockups/TestEnvironment.cs +++ b/Tests/MQTTnet.Core.Tests/Mockups/TestEnvironment.cs @@ -1,7 +1,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using MQTTnet.Client; using MQTTnet.Client.Options; -using MQTTnet.Diagnostics; using MQTTnet.Server; using System; using System.Collections.Generic; @@ -9,6 +8,7 @@ using System.Diagnostics; using System.Linq; using System.Threading; using System.Threading.Tasks; +using MQTTnet.Diagnostics.Logger; using MQTTnet.Extensions.Rpc; using MQTTnet.Extensions.Rpc.Options; using MQTTnet.Formatter; @@ -36,9 +36,9 @@ namespace MQTTnet.Tests.Mockups public int ServerPort { get; set; } = 1888; - public MqttNetLogger ServerLogger { get; } = new MqttNetLogger("server"); + public MqttNetEventLogger ServerLogger { get; } = new MqttNetEventLogger("server"); - public MqttNetLogger ClientLogger { get; } = new MqttNetLogger("client"); + public MqttNetEventLogger ClientLogger { get; } = new MqttNetEventLogger("client"); public TestContext TestContext { get; } @@ -191,19 +191,19 @@ namespace MQTTnet.Tests.Mockups public async Task StartServer(MqttServerOptionsBuilder options) { - CreateServer(); + var server = CreateServer(); options.WithDefaultEndpointPort(ServerPort); options.WithMaxPendingMessagesPerClient(int.MaxValue); - await Server.StartAsync(options.Build()); + await server.StartAsync(options.Build()); - return Server; + return server; } public async Task StartServer(Action options) { - CreateServer(); + var server = CreateServer(); var optionsBuilder = Factory.CreateServerOptionsBuilder(); optionsBuilder.WithDefaultEndpointPort(ServerPort); @@ -211,9 +211,9 @@ namespace MQTTnet.Tests.Mockups options?.Invoke(optionsBuilder); - await Server.StartAsync(optionsBuilder.Build()); + await server.StartAsync(optionsBuilder.Build()); - return Server; + return server; } public TestApplicationMessageReceivedHandler CreateApplicationMessageHandler(IMqttClient mqttClient) diff --git a/Tests/MQTTnet.Core.Tests/Mockups/TestLogger.cs b/Tests/MQTTnet.Core.Tests/Mockups/TestLogger.cs index 247c0f0..75e958c 100644 --- a/Tests/MQTTnet.Core.Tests/Mockups/TestLogger.cs +++ b/Tests/MQTTnet.Core.Tests/Mockups/TestLogger.cs @@ -1,17 +1,12 @@ -using MQTTnet.Diagnostics; -using System; +using System; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.Tests.Mockups { - public class TestLogger : IMqttNetLogger + public sealed class TestLogger : IMqttNetLogger { public event EventHandler LogMessagePublished; - - public IMqttNetScopedLogger CreateScopedLogger(string source) - { - return new MqttNetScopedLogger(this, source); - } - + public void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception) { LogMessagePublished?.Invoke(this, new MqttNetLogMessagePublishedEventArgs(new MqttNetLogMessage diff --git a/Tests/MQTTnet.Core.Tests/MqttFactory_Tests.cs b/Tests/MQTTnet.Core.Tests/MqttFactory_Tests.cs deleted file mode 100644 index d586d60..0000000 --- a/Tests/MQTTnet.Core.Tests/MqttFactory_Tests.cs +++ /dev/null @@ -1,81 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using MQTTnet.Diagnostics; -using MQTTnet.Extensions.ManagedClient; -using System; -using System.Threading; -using System.Threading.Tasks; - -namespace MQTTnet.Tests -{ - [TestClass] - public class MqttFactory_Tests - { - [TestMethod] - public async Task Create_Managed_Client_With_Logger() - { - var factory = new MqttFactory(); - - // This test compares - // 1. correct logID - var logId = "logId"; - var hasInvalidLogId = false; - - // 2. if the total log calls are the same for global and local - //var globalLogCount = 0; - var localLogCount = 0; - - var logger = new MqttNetLogger(logId); - - // TODO: This is commented out because it is affected by other tests. - //// we have a theoretical bug here if a concurrent test is also logging - //var globalLog = new EventHandler((s, e) => - //{ - // if (e.TraceMessage.LogId != logId) - // { - // invalidLogId = e.TraceMessage.LogId; - // } - - // Interlocked.Increment(ref globalLogCount); - //}); - - //MqttNetGlobalLogger.LogMessagePublished += globalLog; - - logger.LogMessagePublished += (s, e) => - { - if (e.LogMessage.LogId != logId) - { - hasInvalidLogId = true; - } - - Interlocked.Increment(ref localLogCount); - }; - - var managedClient = factory.CreateManagedMqttClient(logger); - try - { - var clientOptions = new ManagedMqttClientOptionsBuilder(); - - clientOptions.WithClientOptions(o => o.WithTcpServer("this_is_an_invalid_host").WithCommunicationTimeout(TimeSpan.FromSeconds(1))); - - // try connect to get some log entries - await managedClient.StartAsync(clientOptions.Build()); - - // wait at least connect timeout or we have some log messages - var tcs = new TaskCompletionSource(); - managedClient.ConnectingFailedHandler = new ConnectingFailedHandlerDelegate(e => tcs.TrySetResult(null)); - await Task.WhenAny(Task.Delay(managedClient.Options.ClientOptions.CommunicationTimeout), tcs.Task); - } - finally - { - await managedClient.StopAsync(); - - //MqttNetGlobalLogger.LogMessagePublished -= globalLog; - } - - await Task.Delay(500); - - Assert.IsFalse(hasInvalidLogId); - Assert.AreNotEqual(0, localLogCount); - } - } -} \ No newline at end of file diff --git a/Tests/MQTTnet.Core.Tests/MqttNetLogger_Tests.cs b/Tests/MQTTnet.Core.Tests/MqttNetLogger_Tests.cs deleted file mode 100644 index 722fd6a..0000000 --- a/Tests/MQTTnet.Core.Tests/MqttNetLogger_Tests.cs +++ /dev/null @@ -1,68 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using MQTTnet.Diagnostics; - -namespace MQTTnet.Tests -{ - [TestClass] - public class MqttNetLogger_Tests - { - [TestMethod] - public void Root_Log_Messages() - { - var logger = new MqttNetLogger(); - var childLogger = logger.CreateScopedLogger("Source1"); - - var logMessagesCount = 0; - - logger.LogMessagePublished += (s, e) => - { - logMessagesCount++; - }; - - childLogger.Verbose("Verbose"); - childLogger.Info("Info"); - childLogger.Warning(null, "Warning"); - childLogger.Error(null, "Error"); - - Assert.AreEqual(4, logMessagesCount); - } - - [TestMethod] - public void Bubbling_Log_Messages() - { - var logger = new MqttNetLogger(); - var childLogger = logger.CreateScopedLogger("Source1"); - - var logMessagesCount = 0; - - logger.LogMessagePublished += (s, e) => - { - logMessagesCount++; - }; - - childLogger.Verbose("Verbose"); - childLogger.Info("Info"); - childLogger.Warning(null, "Warning"); - childLogger.Error(null, "Error"); - - Assert.AreEqual(4, logMessagesCount); - } - - [TestMethod] - public void Set_Custom_Log_ID() - { - var logger = new MqttNetLogger("logId"); - var childLogger = logger.CreateScopedLogger("Source1"); - - logger.LogMessagePublished += (s, e) => - { - Assert.AreEqual("logId", e.LogMessage.LogId); - }; - - childLogger.Verbose("Verbose"); - childLogger.Info("Info"); - childLogger.Warning(null, "Warning"); - childLogger.Error(null, "Error"); - } - } -} diff --git a/Tests/MQTTnet.Core.Tests/MqttPacketSerializer_Tests.cs b/Tests/MQTTnet.Core.Tests/MqttPacketSerializer_Tests.cs index 6b4124b..26c2323 100644 --- a/Tests/MQTTnet.Core.Tests/MqttPacketSerializer_Tests.cs +++ b/Tests/MQTTnet.Core.Tests/MqttPacketSerializer_Tests.cs @@ -7,6 +7,7 @@ using System.Threading; using Microsoft.VisualStudio.TestTools.UnitTesting; using MQTTnet.Adapter; using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; using MQTTnet.Exceptions; using MQTTnet.Formatter; using MQTTnet.Formatter.V3; @@ -626,7 +627,7 @@ namespace MQTTnet.Tests using (var headerStream = new MemoryStream(buffer1.ToArray())) { var channel = new TestMqttChannel(headerStream); - var adapter = new MqttChannelAdapter(channel, new MqttPacketFormatterAdapter(protocolVersion, writer), null, new MqttNetLogger()); + var adapter = new MqttChannelAdapter(channel, new MqttPacketFormatterAdapter(protocolVersion, writer), null, new MqttNetEventLogger()); var receivedPacket = adapter.ReceivePacketAsync(CancellationToken.None).GetAwaiter().GetResult(); var buffer2 = serializer.Encode(receivedPacket); @@ -656,7 +657,7 @@ namespace MQTTnet.Tests var buffer = serializer.Encode(packet); var channel = new TestMqttChannel(buffer.ToArray()); - var adapter = new MqttChannelAdapter(channel, new MqttPacketFormatterAdapter(protocolVersion, writer), null, new MqttNetLogger()); + var adapter = new MqttChannelAdapter(channel, new MqttPacketFormatterAdapter(protocolVersion, writer), null, new MqttNetEventLogger()); return (TPacket)adapter.ReceivePacketAsync(CancellationToken.None).GetAwaiter().GetResult(); //using (var headerStream = new MemoryStream(buffer1.ToArray())) @@ -680,7 +681,7 @@ namespace MQTTnet.Tests MqttProtocolVersion DeserializeAndDetectVersion(MqttPacketFormatterAdapter packetFormatterAdapter, byte[] buffer) { var channel = new TestMqttChannel(buffer); - var adapter = new MqttChannelAdapter(channel, packetFormatterAdapter, null, new MqttNetLogger()); + var adapter = new MqttChannelAdapter(channel, packetFormatterAdapter, null, new MqttNetEventLogger()); adapter.ReceivePacketAsync(CancellationToken.None).GetAwaiter().GetResult(); return packetFormatterAdapter.ProtocolVersion; diff --git a/Tests/MQTTnet.TestApp.NetCore/ClientFlowTest.cs b/Tests/MQTTnet.TestApp.NetCore/ClientFlowTest.cs index 9c2b47b..8c0c6d9 100644 --- a/Tests/MQTTnet.TestApp.NetCore/ClientFlowTest.cs +++ b/Tests/MQTTnet.TestApp.NetCore/ClientFlowTest.cs @@ -2,7 +2,7 @@ using System.Threading.Tasks; using MQTTnet.Client; using MQTTnet.Client.Options; -using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.TestApp.NetCore { @@ -12,7 +12,7 @@ namespace MQTTnet.TestApp.NetCore { try { - var logger = new MqttNetLogger(); + var logger = new MqttNetEventLogger(); MqttNetConsoleLogger.ForwardToConsole(logger); var factory = new MqttFactory(logger); diff --git a/Tests/MQTTnet.TestApp.NetCore/ClientTest.cs b/Tests/MQTTnet.TestApp.NetCore/ClientTest.cs index 1e392c6..1f17740 100644 --- a/Tests/MQTTnet.TestApp.NetCore/ClientTest.cs +++ b/Tests/MQTTnet.TestApp.NetCore/ClientTest.cs @@ -7,7 +7,7 @@ using MQTTnet.Protocol; using System; using System.Text; using System.Threading.Tasks; -using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.TestApp.NetCore { @@ -17,7 +17,7 @@ namespace MQTTnet.TestApp.NetCore { try { - var logger = new MqttNetLogger(); + var logger = new MqttNetEventLogger(); MqttNetConsoleLogger.ForwardToConsole(logger); var factory = new MqttFactory(logger); diff --git a/Tests/MQTTnet.TestApp.NetCore/MqttNetConsoleLogger.cs b/Tests/MQTTnet.TestApp.NetCore/MqttNetConsoleLogger.cs index c723643..1c1d6ed 100644 --- a/Tests/MQTTnet.TestApp.NetCore/MqttNetConsoleLogger.cs +++ b/Tests/MQTTnet.TestApp.NetCore/MqttNetConsoleLogger.cs @@ -1,6 +1,6 @@ -using MQTTnet.Diagnostics; -using System; +using System; using System.Text; +using MQTTnet.Diagnostics.Logger; namespace MQTTnet.TestApp.NetCore { @@ -8,7 +8,7 @@ namespace MQTTnet.TestApp.NetCore { static readonly object _lock = new object(); - public static void ForwardToConsole(MqttNetLogger logger) + public static void ForwardToConsole(MqttNetEventLogger logger) { if (logger == null) throw new ArgumentNullException(nameof(logger)); diff --git a/Tests/MQTTnet.TestApp.NetCore/Program.cs b/Tests/MQTTnet.TestApp.NetCore/Program.cs index 5543cde..a4a1f75 100644 --- a/Tests/MQTTnet.TestApp.NetCore/Program.cs +++ b/Tests/MQTTnet.TestApp.NetCore/Program.cs @@ -9,6 +9,7 @@ using System.Net.Security; using System.Threading; using System.Threading.Tasks; using MQTTnet.Client; +using MQTTnet.Diagnostics.Runtime; namespace MQTTnet.TestApp.NetCore { diff --git a/Tests/MQTTnet.TestApp.NetCore/ServerAndClientTest.cs b/Tests/MQTTnet.TestApp.NetCore/ServerAndClientTest.cs index bab9235..2898639 100644 --- a/Tests/MQTTnet.TestApp.NetCore/ServerAndClientTest.cs +++ b/Tests/MQTTnet.TestApp.NetCore/ServerAndClientTest.cs @@ -2,7 +2,7 @@ using System.Threading.Tasks; using MQTTnet.Client; using MQTTnet.Client.Options; -using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; using MQTTnet.Server; namespace MQTTnet.TestApp.NetCore @@ -11,7 +11,7 @@ namespace MQTTnet.TestApp.NetCore { public static async Task RunAsync() { - var logger = new MqttNetLogger(); + var logger = new MqttNetEventLogger(); MqttNetConsoleLogger.ForwardToConsole(logger); var factory = new MqttFactory(logger); diff --git a/Tests/MQTTnet.TestApp.NetCore/ServerTest.cs b/Tests/MQTTnet.TestApp.NetCore/ServerTest.cs index 88969e8..21c47b7 100644 --- a/Tests/MQTTnet.TestApp.NetCore/ServerTest.cs +++ b/Tests/MQTTnet.TestApp.NetCore/ServerTest.cs @@ -3,6 +3,7 @@ using System.Text; using System.Threading.Tasks; using MQTTnet.Client.Receiving; using MQTTnet.Diagnostics; +using MQTTnet.Diagnostics.Logger; using MQTTnet.Protocol; using MQTTnet.Server; using MQTTnet.Server.Internal; @@ -22,7 +23,7 @@ namespace MQTTnet.TestApp.NetCore public static void RunEmptyServerWithLogging() { - var logger = new MqttNetLogger(); + var logger = new MqttNetEventLogger(); MqttNetConsoleLogger.ForwardToConsole(logger); var mqttFactory = new MqttFactory(logger);