From 81b547f3868861fdbc5be37d2ebb6e553e2903f8 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Wed, 1 Apr 2020 23:15:08 +0200 Subject: [PATCH] Fix logging issues. --- .../Logging/MqttNetLoggerWrapper.cs | 4 ++-- Source/MQTTnet/Diagnostics/MqttNetLogger.cs | 7 ++++++- Source/MQTTnet/MqttFactory.cs | 2 +- Tests/MQTTnet.Benchmarks/LoggerBenchmark.cs | 2 +- Tests/MQTTnet.Core.Tests/MqttFactory_Tests.cs | 2 +- Tests/MQTTnet.Core.Tests/MqttNetLogger_Tests.cs | 17 +++++++++++++++++ 6 files changed, 28 insertions(+), 6 deletions(-) diff --git a/Source/MQTTnet.Server/Logging/MqttNetLoggerWrapper.cs b/Source/MQTTnet.Server/Logging/MqttNetLoggerWrapper.cs index adaa363..9c71e28 100644 --- a/Source/MQTTnet.Server/Logging/MqttNetLoggerWrapper.cs +++ b/Source/MQTTnet.Server/Logging/MqttNetLoggerWrapper.cs @@ -7,7 +7,7 @@ namespace MQTTnet.Server.Logging { public class MqttNetLoggerWrapper : IMqttNetLogger { - private readonly ILogger _logger; + readonly ILogger _logger; public MqttNetLoggerWrapper(ILogger logger) { @@ -16,7 +16,7 @@ namespace MQTTnet.Server.Logging public event EventHandler LogMessagePublished; - public IMqttNetLogger CreateChildLogger(string source = null) + public IMqttNetLogger CreateChildLogger(string source) { return new MqttNetLogger(source); } diff --git a/Source/MQTTnet/Diagnostics/MqttNetLogger.cs b/Source/MQTTnet/Diagnostics/MqttNetLogger.cs index 71610d4..bfd4c42 100644 --- a/Source/MQTTnet/Diagnostics/MqttNetLogger.cs +++ b/Source/MQTTnet/Diagnostics/MqttNetLogger.cs @@ -9,7 +9,7 @@ namespace MQTTnet.Diagnostics readonly MqttNetLogger _parentLogger; - public MqttNetLogger(string source, string logId = null) + public MqttNetLogger(string source, string logId) { _source = source; _logId = logId; @@ -19,6 +19,11 @@ namespace MQTTnet.Diagnostics { } + public MqttNetLogger(string logId) + { + _logId = logId; + } + MqttNetLogger(MqttNetLogger parentLogger, string logId, string source) { _parentLogger = parentLogger ?? throw new ArgumentNullException(nameof(parentLogger)); diff --git a/Source/MQTTnet/MqttFactory.cs b/Source/MQTTnet/MqttFactory.cs index ed70fd5..ffc8f90 100644 --- a/Source/MQTTnet/MqttFactory.cs +++ b/Source/MQTTnet/MqttFactory.cs @@ -13,7 +13,7 @@ namespace MQTTnet { IMqttClientAdapterFactory _clientAdapterFactory = new MqttClientAdapterFactory(); - public MqttFactory() : this(new MqttNetLogger(null, null)) + public MqttFactory() : this(new MqttNetLogger()) { } diff --git a/Tests/MQTTnet.Benchmarks/LoggerBenchmark.cs b/Tests/MQTTnet.Benchmarks/LoggerBenchmark.cs index 9be84bd..da77299 100644 --- a/Tests/MQTTnet.Benchmarks/LoggerBenchmark.cs +++ b/Tests/MQTTnet.Benchmarks/LoggerBenchmark.cs @@ -16,7 +16,7 @@ namespace MQTTnet.Benchmarks [GlobalSetup] public void Setup() { - _logger = new MqttNetLogger("1"); + _logger = new MqttNetLogger(); _childLogger = _logger.CreateChildLogger("child"); MqttNetGlobalLogger.LogMessagePublished += OnLogMessagePublished; diff --git a/Tests/MQTTnet.Core.Tests/MqttFactory_Tests.cs b/Tests/MQTTnet.Core.Tests/MqttFactory_Tests.cs index d586d60..5ec2f9f 100644 --- a/Tests/MQTTnet.Core.Tests/MqttFactory_Tests.cs +++ b/Tests/MQTTnet.Core.Tests/MqttFactory_Tests.cs @@ -24,7 +24,7 @@ namespace MQTTnet.Tests //var globalLogCount = 0; var localLogCount = 0; - var logger = new MqttNetLogger(logId); + var logger = new MqttNetLogger(null, 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 diff --git a/Tests/MQTTnet.Core.Tests/MqttNetLogger_Tests.cs b/Tests/MQTTnet.Core.Tests/MqttNetLogger_Tests.cs index 760d70f..8a36ff3 100644 --- a/Tests/MQTTnet.Core.Tests/MqttNetLogger_Tests.cs +++ b/Tests/MQTTnet.Core.Tests/MqttNetLogger_Tests.cs @@ -46,5 +46,22 @@ namespace MQTTnet.Tests Assert.AreEqual(4, logMessagesCount); } + + [TestMethod] + public void Set_Custom_Log_ID() + { + var logger = new MqttNetLogger(null, "logId"); + var childLogger = logger.CreateChildLogger("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"); + } } }