Browse Source

Fix logging issues.

release/3.x.x
Christian Kratky 4 years ago
parent
commit
81b547f386
6 changed files with 28 additions and 6 deletions
  1. +2
    -2
      Source/MQTTnet.Server/Logging/MqttNetLoggerWrapper.cs
  2. +6
    -1
      Source/MQTTnet/Diagnostics/MqttNetLogger.cs
  3. +1
    -1
      Source/MQTTnet/MqttFactory.cs
  4. +1
    -1
      Tests/MQTTnet.Benchmarks/LoggerBenchmark.cs
  5. +1
    -1
      Tests/MQTTnet.Core.Tests/MqttFactory_Tests.cs
  6. +17
    -0
      Tests/MQTTnet.Core.Tests/MqttNetLogger_Tests.cs

+ 2
- 2
Source/MQTTnet.Server/Logging/MqttNetLoggerWrapper.cs View File

@@ -7,7 +7,7 @@ namespace MQTTnet.Server.Logging
{ {
public class MqttNetLoggerWrapper : IMqttNetLogger public class MqttNetLoggerWrapper : IMqttNetLogger
{ {
private readonly ILogger<MqttServer> _logger;
readonly ILogger<MqttServer> _logger;


public MqttNetLoggerWrapper(ILogger<MqttServer> logger) public MqttNetLoggerWrapper(ILogger<MqttServer> logger)
{ {
@@ -16,7 +16,7 @@ namespace MQTTnet.Server.Logging


public event EventHandler<MqttNetLogMessagePublishedEventArgs> LogMessagePublished; public event EventHandler<MqttNetLogMessagePublishedEventArgs> LogMessagePublished;


public IMqttNetLogger CreateChildLogger(string source = null)
public IMqttNetLogger CreateChildLogger(string source)
{ {
return new MqttNetLogger(source); return new MqttNetLogger(source);
} }


+ 6
- 1
Source/MQTTnet/Diagnostics/MqttNetLogger.cs View File

@@ -9,7 +9,7 @@ namespace MQTTnet.Diagnostics


readonly MqttNetLogger _parentLogger; readonly MqttNetLogger _parentLogger;


public MqttNetLogger(string source, string logId = null)
public MqttNetLogger(string source, string logId)
{ {
_source = source; _source = source;
_logId = logId; _logId = logId;
@@ -19,6 +19,11 @@ namespace MQTTnet.Diagnostics
{ {
} }


public MqttNetLogger(string logId)
{
_logId = logId;
}

MqttNetLogger(MqttNetLogger parentLogger, string logId, string source) MqttNetLogger(MqttNetLogger parentLogger, string logId, string source)
{ {
_parentLogger = parentLogger ?? throw new ArgumentNullException(nameof(parentLogger)); _parentLogger = parentLogger ?? throw new ArgumentNullException(nameof(parentLogger));


+ 1
- 1
Source/MQTTnet/MqttFactory.cs View File

@@ -13,7 +13,7 @@ namespace MQTTnet
{ {
IMqttClientAdapterFactory _clientAdapterFactory = new MqttClientAdapterFactory(); IMqttClientAdapterFactory _clientAdapterFactory = new MqttClientAdapterFactory();


public MqttFactory() : this(new MqttNetLogger(null, null))
public MqttFactory() : this(new MqttNetLogger())
{ {
} }




+ 1
- 1
Tests/MQTTnet.Benchmarks/LoggerBenchmark.cs View File

@@ -16,7 +16,7 @@ namespace MQTTnet.Benchmarks
[GlobalSetup] [GlobalSetup]
public void Setup() public void Setup()
{ {
_logger = new MqttNetLogger("1");
_logger = new MqttNetLogger();
_childLogger = _logger.CreateChildLogger("child"); _childLogger = _logger.CreateChildLogger("child");


MqttNetGlobalLogger.LogMessagePublished += OnLogMessagePublished; MqttNetGlobalLogger.LogMessagePublished += OnLogMessagePublished;


+ 1
- 1
Tests/MQTTnet.Core.Tests/MqttFactory_Tests.cs View File

@@ -24,7 +24,7 @@ namespace MQTTnet.Tests
//var globalLogCount = 0; //var globalLogCount = 0;
var localLogCount = 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. // 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 //// we have a theoretical bug here if a concurrent test is also logging


+ 17
- 0
Tests/MQTTnet.Core.Tests/MqttNetLogger_Tests.cs View File

@@ -46,5 +46,22 @@ namespace MQTTnet.Tests


Assert.AreEqual(4, logMessagesCount); 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");
}
} }
} }

Loading…
Cancel
Save