@@ -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); | ||||
} | } | ||||
@@ -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)); | ||||
@@ -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()) | |||||
{ | { | ||||
} | } | ||||
@@ -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; | ||||
@@ -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 | ||||
@@ -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"); | |||||
} | |||||
} | } | ||||
} | } |