Align different formats of log messages.

This commit is contained in:
Christian
2020-11-16 20:47:44 +01:00
parent f9c3014e52
commit a93f44217b
14 changed files with 35 additions and 34 deletions
@@ -425,7 +425,7 @@ namespace MQTTnet.Extensions.ManagedClient
{ {
transmitException = exception; transmitException = exception;
_logger.Warning(exception, $"Publishing application ({message.Id}) message failed."); _logger.Warning(exception, "Publishing application message ({0}) failed.", message.Id);
if (message.ApplicationMessage.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtMostOnce) if (message.ApplicationMessage.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtMostOnce)
{ {
@@ -450,7 +450,7 @@ namespace MQTTnet.Extensions.ManagedClient
catch (Exception exception) catch (Exception exception)
{ {
transmitException = exception; transmitException = exception;
_logger.Error(exception, $"Error while publishing application message ({message.Id})."); _logger.Error(exception, "Error while publishing application message ({0}).", message.Id);
} }
finally finally
{ {
@@ -484,7 +484,7 @@ namespace MQTTnet.Extensions.ManagedClient
continue; continue;
} }
_logger.Verbose($"Publishing subscriptions ({subscriptions.Count} subscriptions and {unsubscriptions.Count} unsubscriptions)"); _logger.Verbose("Publishing {0} subscriptions and {1} unsubscriptions)", subscriptions.Count, unsubscriptions.Count);
foreach (var unsubscription in unsubscriptions) foreach (var unsubscription in unsubscriptions)
{ {
+3 -3
View File
@@ -89,7 +89,7 @@ namespace MQTTnet.Client
using (var combined = CancellationTokenSource.CreateLinkedTokenSource(backgroundCancellationToken, cancellationToken)) using (var combined = CancellationTokenSource.CreateLinkedTokenSource(backgroundCancellationToken, cancellationToken))
{ {
_logger.Verbose($"Trying to connect with server '{options.ChannelOptions}' (Timeout={options.CommunicationTimeout})."); _logger.Verbose("Trying to connect with server '{0}' (Timeout={1}).", options.ChannelOptions, options.CommunicationTimeout);
await _adapter.ConnectAsync(options.CommunicationTimeout, combined.Token).ConfigureAwait(false); await _adapter.ConnectAsync(options.CommunicationTimeout, combined.Token).ConfigureAwait(false);
_logger.Verbose("Connection with server established."); _logger.Verbose("Connection with server established.");
@@ -404,7 +404,7 @@ namespace MQTTnet.Client
} }
catch (Exception e) catch (Exception e)
{ {
_logger.Warning(e, "Error when sending packet of type '{0}'.", typeof(TResponsePacket).Name); _logger.Warning(e, "Error when sending request packet ({0}).", requestPacket.GetType().Name);
packetAwaiter.Cancel(); packetAwaiter.Cancel();
} }
@@ -416,7 +416,7 @@ namespace MQTTnet.Client
{ {
if (exception is MqttCommunicationTimedOutException) if (exception is MqttCommunicationTimedOutException)
{ {
_logger.Warning(null, "Timeout while waiting for packet of type '{0}'.", typeof(TResponsePacket).Name); _logger.Warning(null, "Timeout while waiting for response packet ({0}).", typeof(TResponsePacket).Name);
} }
throw; throw;
@@ -4,8 +4,6 @@ namespace MQTTnet.Diagnostics
{ {
public interface IMqttNetLogger public interface IMqttNetLogger
{ {
event EventHandler<MqttNetLogMessagePublishedEventArgs> LogMessagePublished;
IMqttNetScopedLogger CreateScopedLogger(string source); IMqttNetScopedLogger CreateScopedLogger(string source);
void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception); void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception);
@@ -2,6 +2,7 @@
namespace MQTTnet.Diagnostics 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 class MqttNetGlobalLogger
{ {
public static event EventHandler<MqttNetLogMessagePublishedEventArgs> LogMessagePublished; public static event EventHandler<MqttNetLogMessagePublishedEventArgs> LogMessagePublished;
@@ -10,8 +11,6 @@ namespace MQTTnet.Diagnostics
public static void Publish(MqttNetLogMessage logMessage) public static void Publish(MqttNetLogMessage logMessage)
{ {
if (logMessage == null) throw new ArgumentNullException(nameof(logMessage));
LogMessagePublished?.Invoke(null, new MqttNetLogMessagePublishedEventArgs(logMessage)); LogMessagePublished?.Invoke(null, new MqttNetLogMessagePublishedEventArgs(logMessage));
} }
} }
@@ -2,18 +2,18 @@
namespace MQTTnet.Diagnostics namespace MQTTnet.Diagnostics
{ {
public class MqttNetLogMessagePublishedEventArgs : EventArgs public sealed class MqttNetLogMessagePublishedEventArgs : EventArgs
{ {
public MqttNetLogMessagePublishedEventArgs(MqttNetLogMessage logMessage) public MqttNetLogMessagePublishedEventArgs(MqttNetLogMessage logMessage)
{ {
LogMessage = logMessage ?? throw new ArgumentNullException(nameof(logMessage)); LogMessage = logMessage ?? throw new ArgumentNullException(nameof(logMessage));
#pragma warning disable CS0618 // Type or member is obsolete #pragma warning disable CS0618 // Type or member is obsolete
TraceMessage = logMessage ?? throw new ArgumentNullException(nameof(logMessage)); TraceMessage = logMessage;
#pragma warning restore CS0618 // Type or member is obsolete #pragma warning restore CS0618 // Type or member is obsolete
} }
[Obsolete("Use new proeprty LogMessage instead.")] [Obsolete("Use new property LogMessage instead.")]
public MqttNetLogMessage TraceMessage { get; } public MqttNetLogMessage TraceMessage { get; }
public MqttNetLogMessage LogMessage { get; } public MqttNetLogMessage LogMessage { get; }
+9 -6
View File
@@ -36,13 +36,16 @@ namespace MQTTnet.Diagnostics
return; return;
} }
try if (parameters?.Length > 0 && message?.Length > 0)
{ {
message = string.Format(message ?? string.Empty, parameters); try
} {
catch (FormatException) message = string.Format(message, parameters);
{ }
message = "MESSAGE FORMAT INVALID: " + message; catch (FormatException)
{
message = "MESSAGE FORMAT INVALID: " + message;
}
} }
var logMessage = new MqttNetLogMessage var logMessage = new MqttNetLogMessage
@@ -113,7 +113,7 @@ namespace MQTTnet.Implementations
} }
catch (Exception exception) catch (Exception exception)
{ {
_logger.Error(exception, "Error while cleaning up client connection"); _logger.Error(exception, "Error while cleaning up client connection.");
} }
} }
} }
@@ -59,7 +59,7 @@ namespace MQTTnet.Implementations
_localEndPoint = new IPEndPoint(boundIp, _options.Port); _localEndPoint = new IPEndPoint(boundIp, _options.Port);
_logger.Info($"Starting TCP listener for {_localEndPoint} TLS={_tlsCertificate != null}."); _logger.Info("Starting TCP listener for {0} TLS={1}.", _localEndPoint, _tlsCertificate != null);
_socket = new CrossPlatformSocket(_addressFamily); _socket = new CrossPlatformSocket(_addressFamily);
@@ -131,7 +131,7 @@ namespace MQTTnet.Implementations
} }
} }
_logger.Error(exception, $"Error while accepting connection at TCP listener {_localEndPoint} TLS={_tlsCertificate != null}."); _logger.Error(exception, "Error while accepting connection at TCP listener {0} TLS={1}.", _localEndPoint, _tlsCertificate != null);
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken).ConfigureAwait(false); await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken).ConfigureAwait(false);
} }
} }
@@ -39,7 +39,7 @@ namespace MQTTnet.LowLevelClient
try try
{ {
_logger.Verbose($"Trying to connect with server '{options.ChannelOptions}' (Timeout={options.CommunicationTimeout})."); _logger.Verbose("Trying to connect with server '{0}' (Timeout={1}).", options.ChannelOptions, options.CommunicationTimeout);
await newAdapter.ConnectAsync(options.CommunicationTimeout, cancellationToken).ConfigureAwait(false); await newAdapter.ConnectAsync(options.CommunicationTimeout, cancellationToken).ConfigureAwait(false);
_logger.Verbose("Connection with server established."); _logger.Verbose("Connection with server established.");
@@ -453,7 +453,7 @@ namespace MQTTnet.Server
} }
} }
_logger.Verbose("Queued application message sent (ClientId: {0}).", ClientId); _logger.Verbose("Client '{0}': Queued application message sent.", ClientId);
} }
} }
catch (Exception exception) catch (Exception exception)
@@ -463,15 +463,15 @@ namespace MQTTnet.Server
} }
else if (exception is MqttCommunicationTimedOutException) else if (exception is MqttCommunicationTimedOutException)
{ {
_logger.Warning(exception, "Sending publish packet failed: Timeout (ClientId: {0}).", ClientId); _logger.Warning(exception, "Client '{0}': Sending publish packet failed: Timeout.", ClientId);
} }
else if (exception is MqttCommunicationException) else if (exception is MqttCommunicationException)
{ {
_logger.Warning(exception, "Sending publish packet failed: Communication exception (ClientId: {0}).", ClientId); _logger.Warning(exception, "Client '{0}': Sending publish packet failed: Communication exception.", ClientId);
} }
else else
{ {
_logger.Error(exception, "Sending publish packet failed (ClientId: {0}).", ClientId); _logger.Error(exception, "Client '{0}': Sending publish packet failed.", ClientId);
} }
if (publishPacket?.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce) if (publishPacket?.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce)
+1 -1
View File
@@ -54,7 +54,7 @@ namespace MQTTnet.Server
return false; return false;
} }
_logger.Verbose("Queued application message with topic '{0}' (ClientId: {1}).", applicationMessage.Topic, ClientId); _logger.Verbose("Client '{0}': Queued application message with topic '{1}'.", ClientId, applicationMessage.Topic);
ApplicationMessagesQueue.Enqueue(applicationMessage, senderClientId, checkSubscriptionsResult.QualityOfServiceLevel, isRetainedApplicationMessage); ApplicationMessagesQueue.Enqueue(applicationMessage, senderClientId, checkSubscriptionsResult.QualityOfServiceLevel, isRetainedApplicationMessage);
@@ -67,20 +67,21 @@ namespace MQTTnet.Server
if (connectPacket == null) if (connectPacket == null)
{ {
_logger.Warning(null, _logger.Warning(null,
"The first packet from client '{0}' was no 'CONNECT' packet [MQTT-3.1.0-1].", "Client '{0}': First received packet was no 'CONNECT' packet [MQTT-3.1.0-1].",
channelAdapter.Endpoint); channelAdapter.Endpoint);
return; return;
} }
} }
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
_logger.Warning(null, "Client '{0}' connected but did not sent a CONNECT packet.", channelAdapter.Endpoint); _logger.Warning(null, "Client '{0}': Connected but did not sent a CONNECT packet.", channelAdapter.Endpoint);
return; return;
} }
catch (MqttCommunicationTimedOutException) catch (MqttCommunicationTimedOutException)
{ {
_logger.Warning(null, "Client '{0}' connected but did not sent a CONNECT packet.", channelAdapter.Endpoint); _logger.Warning(null, "Client '{0}': Connected but did not sent a CONNECT packet.", channelAdapter.Endpoint);
return; return;
} }
@@ -30,9 +30,9 @@ namespace MQTTnet.Tests.Mockups
public int ServerPort { get; set; } = 1888; public int ServerPort { get; set; } = 1888;
public IMqttNetLogger ServerLogger { get; } = new MqttNetLogger("server"); public MqttNetLogger ServerLogger { get; } = new MqttNetLogger("server");
public IMqttNetLogger ClientLogger { get; } = new MqttNetLogger("client"); public MqttNetLogger ClientLogger { get; } = new MqttNetLogger("client");
public TestContext TestContext { get; } public TestContext TestContext { get; }
+1 -1
View File
@@ -445,7 +445,7 @@ namespace MQTTnet.Tests
await c1.PublishAsync(message).ConfigureAwait(false); await c1.PublishAsync(message).ConfigureAwait(false);
} }
SpinWait.SpinUntil(() => receivedMessagesCount == 1000, TimeSpan.FromSeconds(15)); SpinWait.SpinUntil(() => receivedMessagesCount == 1000, TimeSpan.FromSeconds(20));
Assert.AreEqual(1000, receivedMessagesCount); Assert.AreEqual(1000, receivedMessagesCount);
} }