mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
Refactor logger. (#1247)
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
<description>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.</description>
|
||||
<releaseNotes>
|
||||
* [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
|
||||
</releaseNotes>
|
||||
|
||||
@@ -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<IMqttNetLogger>(logger);
|
||||
services.AddSingleton<MqttHostedServer>();
|
||||
|
||||
@@ -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<IMqttServerAdapter> adapters, IMqttNetLogger logger)
|
||||
: base(adapters, logger)
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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<string> _unsubscriptions = new HashSet<string>();
|
||||
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;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
using MQTTnet.Diagnostics;
|
||||
using System;
|
||||
using System;
|
||||
using MQTTnet.Diagnostics.Logger;
|
||||
|
||||
namespace MQTTnet.Extensions.ManagedClient
|
||||
{
|
||||
|
||||
@@ -4,6 +4,7 @@ using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Formatter;
|
||||
using MQTTnet.Implementations;
|
||||
using System;
|
||||
using MQTTnet.Diagnostics.Logger;
|
||||
|
||||
namespace MQTTnet.Extensions.WebSocket4Net
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using MQTTnet.Adapter;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Diagnostics.Logger;
|
||||
using MQTTnet.LowLevelClient;
|
||||
|
||||
namespace MQTTnet.Client
|
||||
|
||||
@@ -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; }
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
+1
-3
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
|
||||
namespace MQTTnet.Diagnostics.Logger
|
||||
{
|
||||
/// <summary>
|
||||
/// This logger fires an event when a new message was published.
|
||||
/// </summary>
|
||||
public sealed class MqttNetEventLogger : IMqttNetLogger
|
||||
{
|
||||
public MqttNetEventLogger(string logId = null)
|
||||
{
|
||||
LogId = logId;
|
||||
}
|
||||
|
||||
public event EventHandler<MqttNetLogMessagePublishedEventArgs> 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
namespace MQTTnet.Diagnostics
|
||||
namespace MQTTnet.Diagnostics.Logger
|
||||
{
|
||||
public enum MqttNetLogLevel
|
||||
{
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
|
||||
namespace MQTTnet.Diagnostics
|
||||
namespace MQTTnet.Diagnostics.Logger
|
||||
{
|
||||
public sealed class MqttNetLogMessage
|
||||
{
|
||||
+1
-8
@@ -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; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace MQTTnet.Diagnostics.Logger
|
||||
{
|
||||
/// <summary>
|
||||
/// This logger does nothing with the messages.
|
||||
/// </summary>
|
||||
public sealed class MqttNetNullLogger : IMqttNetLogger
|
||||
{
|
||||
public void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-9
@@ -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)
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<MqttNetLogMessagePublishedEventArgs> LogMessagePublished;
|
||||
|
||||
public static bool HasListeners => LogMessagePublished != null;
|
||||
|
||||
public static void Publish(MqttNetLogMessage logMessage)
|
||||
{
|
||||
LogMessagePublished?.Invoke(null, new MqttNetLogMessagePublishedEventArgs(logMessage));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<MqttNetLogMessagePublishedEventArgs> 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
namespace MQTTnet.Diagnostics
|
||||
namespace MQTTnet.Diagnostics.Runtime
|
||||
{
|
||||
public static class TargetFrameworkProvider
|
||||
{
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Diagnostics.Logger;
|
||||
using MQTTnet.Server;
|
||||
|
||||
namespace MQTTnet
|
||||
|
||||
@@ -4,6 +4,7 @@ using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Formatter;
|
||||
using System;
|
||||
using MQTTnet.Channel;
|
||||
using MQTTnet.Diagnostics.Logger;
|
||||
|
||||
namespace MQTTnet.Implementations
|
||||
{
|
||||
|
||||
@@ -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<MqttTcpServerListener> _listeners = new List<MqttTcpServerListener>();
|
||||
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<IMqttChannelAdapter, Task> ClientHandler { get; set; }
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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 =>
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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())
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using MQTTnet.Diagnostics;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Diagnostics.Logger;
|
||||
|
||||
namespace MQTTnet.Server
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using MQTTnet.Adapter;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Diagnostics.Logger;
|
||||
|
||||
namespace MQTTnet.Server
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<MqttClientConnection> CreateClientConnection(MqttConnectPacket connectPacket, IMqttChannelAdapter channelAdapter, IDictionary<object, object> sessionItems)
|
||||
async Task<MqttClientConnection> CreateClientConnection(
|
||||
MqttConnectPacket connectPacket,
|
||||
IMqttChannelAdapter channelAdapter,
|
||||
IDictionary<object, object> sessionItems)
|
||||
{
|
||||
MqttClientConnection connection;
|
||||
|
||||
@@ -500,7 +504,9 @@ namespace MQTTnet.Server.Internal
|
||||
return connection;
|
||||
}
|
||||
|
||||
async Task<MqttApplicationMessageInterceptorContext> InterceptApplicationMessageAsync(IMqttServerApplicationMessageInterceptor interceptor, MqttClientConnection clientConnection,
|
||||
async Task<MqttApplicationMessageInterceptorContext> 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
|
||||
|
||||
@@ -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<string, MqttApplicationMessage> _messages = new Dictionary<string, MqttApplicationMessage>(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;
|
||||
|
||||
@@ -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; }
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
using MQTTnet.Diagnostics;
|
||||
|
||||
namespace MQTTnet.Server
|
||||
{
|
||||
public sealed class MqttApplicationMessageInterceptorContext
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the currently used logger.
|
||||
/// </summary>
|
||||
public IMqttNetScopedLogger Logger { get; internal set; }
|
||||
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the client identifier.
|
||||
/// Hint: This identifier needs to be unique over all used clients / devices on the broker to avoid connection issues.
|
||||
|
||||
@@ -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<IMqttServerAdapter> _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);
|
||||
|
||||
+7
-2
@@ -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<MqttApplicationMessageInterceptorContext, Exception, Task> ExceptionHandler { get; set; }
|
||||
|
||||
public MqttServerMultiThreadedApplicationMessageInterceptorDelegate(Func<MqttApplicationMessageInterceptorContext, Task> 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();
|
||||
|
||||
|
||||
@@ -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<byte>[] chunks)
|
||||
static byte[] Join(params ArraySegment<byte>[] chunks)
|
||||
{
|
||||
var buffer = new MemoryStream();
|
||||
foreach (var chunk in chunks)
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using System.Linq;
|
||||
using BenchmarkDotNet.Configs;
|
||||
using BenchmarkDotNet.Jobs;
|
||||
using BenchmarkDotNet.Jobs;
|
||||
using BenchmarkDotNet.Validators;
|
||||
|
||||
namespace MQTTnet.Benchmarks.Configurations
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using BenchmarkDotNet.Configs;
|
||||
using BenchmarkDotNet.Environments;
|
||||
using BenchmarkDotNet.Environments;
|
||||
using BenchmarkDotNet.Jobs;
|
||||
using BenchmarkDotNet.Toolchains.CsProj;
|
||||
|
||||
|
||||
@@ -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++)
|
||||
{
|
||||
_childLogger.Verbose("test log message {0}", "parameter");
|
||||
_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++)
|
||||
{
|
||||
_sourceNullLogger.Verbose("test log message {0}", "parameter");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using BenchmarkDotNet.Running;
|
||||
using MQTTnet.Benchmarks.Configurations;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Diagnostics.Runtime;
|
||||
|
||||
namespace MQTTnet.Benchmarks
|
||||
{
|
||||
|
||||
@@ -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<byte> _serializedPacket;
|
||||
private IMqttPacketFormatter _serializer;
|
||||
MqttBasePacket _packet;
|
||||
ArraySegment<byte> _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<byte> _buffer;
|
||||
private int _position;
|
||||
readonly ArraySegment<byte> _buffer;
|
||||
int _position;
|
||||
|
||||
public BenchmarkMqttChannel(ArraySegment<byte> buffer)
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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));
|
||||
|
||||
+7
-7
@@ -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
|
||||
+15
-14
@@ -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<MqttApplicationMessage>();
|
||||
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);
|
||||
|
||||
+1
-1
@@ -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
|
||||
+11
-11
@@ -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
|
||||
@@ -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<object>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<IMqttServer> 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<IMqttServer> StartServer(Action<MqttServerOptionsBuilder> 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)
|
||||
|
||||
@@ -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<MqttNetLogMessagePublishedEventArgs> 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
|
||||
|
||||
@@ -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<MqttNetLogMessagePublishedEventArgs>((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<object>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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));
|
||||
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user