Просмотр исходного кода

Make logger parameters generic.

release/3.x.x
Christian 3 лет назад
Родитель
Сommit
b362b2afc2
9 измененных файлов: 140 добавлений и 20 удалений
  1. +2
    -0
      Source/MQTTnet/Diagnostics/Logger/IMqttNetLogger.cs
  2. +2
    -0
      Source/MQTTnet/Diagnostics/Logger/MqttNetEventLogger.cs
  3. +2
    -0
      Source/MQTTnet/Diagnostics/Logger/MqttNetNullLogger.cs
  4. +2
    -0
      Source/MQTTnet/Diagnostics/Logger/MqttNetSourceLogger.cs
  5. +125
    -15
      Source/MQTTnet/Diagnostics/Logger/MqttNetSourceLoggerExtensions.cs
  6. +2
    -2
      Tests/MQTTnet.Core.Tests/Logger/Logger_Tests.cs
  7. +1
    -1
      Tests/MQTTnet.Core.Tests/Logger/SourceLogger_Tests.cs
  8. +3
    -1
      Tests/MQTTnet.Core.Tests/Mockups/TestLogger.cs
  9. +1
    -1
      Tests/MQTTnet.Core.Tests/RPC_Tests.cs

+ 2
- 0
Source/MQTTnet/Diagnostics/Logger/IMqttNetLogger.cs Просмотреть файл

@@ -4,6 +4,8 @@ namespace MQTTnet.Diagnostics.Logger
{
public interface IMqttNetLogger
{
bool IsEnabled { get; }
void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception);
}
}

+ 2
- 0
Source/MQTTnet/Diagnostics/Logger/MqttNetEventLogger.cs Просмотреть файл

@@ -16,6 +16,8 @@ namespace MQTTnet.Diagnostics.Logger

public string LogId { get; }

public bool IsEnabled { get; set; } = true;

public void Publish(MqttNetLogLevel level, string source, string message, object[] parameters, Exception exception)
{
var eventHandler = LogMessagePublished;


+ 2
- 0
Source/MQTTnet/Diagnostics/Logger/MqttNetNullLogger.cs Просмотреть файл

@@ -7,6 +7,8 @@ namespace MQTTnet.Diagnostics.Logger
/// </summary>
public sealed class MqttNetNullLogger : IMqttNetLogger
{
public bool IsEnabled { get; }

public void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception)
{
}


+ 2
- 0
Source/MQTTnet/Diagnostics/Logger/MqttNetSourceLogger.cs Просмотреть файл

@@ -13,6 +13,8 @@ namespace MQTTnet.Diagnostics.Logger
_source = source;
}

public bool IsEnabled => _logger.IsEnabled;
public void Publish(MqttNetLogLevel logLevel, string message, object[] parameters, Exception exception)
{
_logger.Publish(logLevel, _source, message, parameters, exception);


+ 125
- 15
Source/MQTTnet/Diagnostics/Logger/MqttNetSourceLoggerExtensions.cs Просмотреть файл

@@ -4,66 +4,176 @@ namespace MQTTnet.Diagnostics.Logger
{
public static class MqttNetSourceLoggerExtensions
{
/*
* The logger uses generic parameters in order to avoid boxing of parameter values like integers etc.
*/

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)
public static void Verbose<TParameter1>(this MqttNetSourceLogger logger, string message, TParameter1 parameter1)
{
logger.Publish(MqttNetLogLevel.Verbose, message, parameters, null);
if (!logger.IsEnabled)
{
return;
}

logger.Publish(MqttNetLogLevel.Verbose, message, new object[] {parameter1}, null);
}

public static void Verbose<TParameter1, TParameter2>(this MqttNetSourceLogger logger, string message, TParameter1 parameter1, TParameter2 parameter2)
{
if (!logger.IsEnabled)
{
return;
}

logger.Publish(MqttNetLogLevel.Verbose, message, new object[] {parameter1, parameter2}, null);
}

public static void Verbose<TParameter1, TParameter2, TParameter3>(this MqttNetSourceLogger logger, string message, TParameter1 parameter1, TParameter2 parameter2,
TParameter3 parameter3)
{
if (!logger.IsEnabled)
{
return;
}

logger.Publish(MqttNetLogLevel.Verbose, message, new object[] {parameter1, parameter2, parameter3}, null);
}

public static void Verbose(this MqttNetSourceLogger logger, string message)
{
if (!logger.IsEnabled)
{
return;
}

logger.Publish(MqttNetLogLevel.Verbose, message, null, null);
}

public static void Info(this MqttNetSourceLogger logger, string message, params object[] parameters)
public static void Info<TParameter1>(this MqttNetSourceLogger logger, string message, TParameter1 parameter1)
{
logger.Publish(MqttNetLogLevel.Info, message, parameters, null);
if (!logger.IsEnabled)
{
return;
}

logger.Publish(MqttNetLogLevel.Info, message, new object[] {parameter1}, null);
}
public static void Info<TParameter1, TParameter2>(this MqttNetSourceLogger logger, string message, TParameter1 parameter1, TParameter2 parameter2)
{
if (!logger.IsEnabled)
{
return;
}

logger.Publish(MqttNetLogLevel.Info, message, new object[] {parameter1, parameter2}, null);
}

public static void Info(this MqttNetSourceLogger logger, string message)
{
if (!logger.IsEnabled)
{
return;
}
logger.Publish(MqttNetLogLevel.Info, message, null, null);
}

public static void Warning(this MqttNetSourceLogger logger, Exception exception, string message, params object[] parameters)
public static void Warning<TParameter1>(this MqttNetSourceLogger logger, Exception exception, string message, TParameter1 parameter1)
{
if (!logger.IsEnabled)
{
return;
}
logger.Publish(MqttNetLogLevel.Warning, message, new object[] {parameter1}, exception);
}
public static void Warning<TParameter1, TParameter2>(this MqttNetSourceLogger logger, Exception exception, string message, TParameter1 parameter1, TParameter2 parameter2)
{
logger.Publish(MqttNetLogLevel.Warning, message, parameters, exception);
if (!logger.IsEnabled)
{
return;
}
logger.Publish(MqttNetLogLevel.Warning, message, new object[] {parameter1, parameter2}, exception);
}
public static void Warning(this MqttNetSourceLogger logger, Exception exception, string message)
{
if (!logger.IsEnabled)
{
return;
}
logger.Publish(MqttNetLogLevel.Warning, message, null, exception);
}
public static void Warning(this MqttNetSourceLogger logger, string message, params object[] parameters)
public static void Warning<TParameter1>(this MqttNetSourceLogger logger, string message, TParameter1 parameter1)
{
logger.Publish(MqttNetLogLevel.Warning, message, parameters, null);
if (!logger.IsEnabled)
{
return;
}
logger.Publish(MqttNetLogLevel.Warning, message, new object[] {parameter1}, null);
}
public static void Warning(this MqttNetSourceLogger logger, string message)
{
if (!logger.IsEnabled)
{
return;
}
logger.Publish(MqttNetLogLevel.Warning, message, null, null);
}

public static void Error(this MqttNetSourceLogger logger, Exception exception, string message, params object[] parameters)
public static void Error<TParameter1>(this MqttNetSourceLogger logger, Exception exception, string message, TParameter1 parameter1)
{
logger.Publish(MqttNetLogLevel.Error, message, parameters, exception);
if (!logger.IsEnabled)
{
return;
}
logger.Publish(MqttNetLogLevel.Error, message, new object[] {parameter1}, exception);
}
public static void Error<TParameter1, TParameter2>(this MqttNetSourceLogger logger, Exception exception, string message, TParameter1 parameter1, TParameter2 parameter2)
{
if (!logger.IsEnabled)
{
return;
}
logger.Publish(MqttNetLogLevel.Error, message, new object[] {parameter1, parameter2}, exception);
}

public static void Error(this MqttNetSourceLogger logger, Exception exception, string message)
{
if (!logger.IsEnabled)
{
return;
}
logger.Publish(MqttNetLogLevel.Error, message, null, exception);
}
public static void Error(this MqttNetSourceLogger logger, string message)
{
if (!logger.IsEnabled)
{
return;
}
logger.Publish(MqttNetLogLevel.Error, message, null, null);
}
}
}
}

+ 2
- 2
Tests/MQTTnet.Core.Tests/Logger/Logger_Tests.cs Просмотреть файл

@@ -35,7 +35,7 @@ namespace MQTTnet.Tests.Logger

childLogger.Verbose("Verbose");
childLogger.Info("Info");
childLogger.Warning(null, "Warning");
childLogger.Warning((Exception)null, "Warning");
childLogger.Error(null, "Error");

Assert.AreEqual(4, logMessagesCount);
@@ -55,7 +55,7 @@ namespace MQTTnet.Tests.Logger
childLogger.Verbose("Verbose");
childLogger.Info("Info");
childLogger.Warning(null, "Warning");
childLogger.Warning((Exception)null, "Warning");
childLogger.Error(null, "Error");
}
}

+ 1
- 1
Tests/MQTTnet.Core.Tests/Logger/SourceLogger_Tests.cs Просмотреть файл

@@ -18,7 +18,7 @@ namespace MQTTnet.Tests.Logger
};
var sourceLogger = logger.WithSource("The_Source");
sourceLogger.Info("MESSAGE", null, null);
sourceLogger.Info("MESSAGE", (object)null, (object)null);
Assert.AreEqual("The_Source", logMessage.Source);
}


+ 3
- 1
Tests/MQTTnet.Core.Tests/Mockups/TestLogger.cs Просмотреть файл

@@ -6,7 +6,9 @@ namespace MQTTnet.Tests.Mockups
public sealed class TestLogger : IMqttNetLogger
{
public event EventHandler<MqttNetLogMessagePublishedEventArgs> LogMessagePublished;

public bool IsEnabled { get; } = true;

public void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception)
{
LogMessagePublished?.Invoke(this, new MqttNetLogMessagePublishedEventArgs(new MqttNetLogMessage


+ 1
- 1
Tests/MQTTnet.Core.Tests/RPC_Tests.cs Просмотреть файл

@@ -190,7 +190,7 @@ namespace MQTTnet.Tests
var responseSender = await testEnvironment.ConnectClient();
await responseSender.SubscribeAsync("MQTTnet.RPC/+/ping", MqttQualityOfServiceLevel.AtMostOnce);

responseSender.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(async e =>
responseSender.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e =>
{
Assert.IsNull(e.ApplicationMessage.ResponseTopic);
});


Загрузка…
Отмена
Сохранить