diff --git a/Source/MQTTnet.Server/Configuration/SettingsModel.cs b/Source/MQTTnet.Server/Configuration/SettingsModel.cs index 713f664..7aeab46 100644 --- a/Source/MQTTnet.Server/Configuration/SettingsModel.cs +++ b/Source/MQTTnet.Server/Configuration/SettingsModel.cs @@ -44,5 +44,7 @@ /// The settings for retained messages. /// public RetainedApplicationMessagesModel RetainedApplicationMessages { get; set; } = new RetainedApplicationMessagesModel(); + + public bool EnableDebugLogging { get; set; } = false; } } \ No newline at end of file diff --git a/Source/MQTTnet.Server/Logging/MqttNetLoggerWrapper.cs b/Source/MQTTnet.Server/Logging/MqttNetLoggerWrapper.cs index 2623451..802ba48 100644 --- a/Source/MQTTnet.Server/Logging/MqttNetLoggerWrapper.cs +++ b/Source/MQTTnet.Server/Logging/MqttNetLoggerWrapper.cs @@ -23,12 +23,6 @@ namespace MQTTnet.Server.Logging public void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception) { var convertedLogLevel = ConvertLogLevel(logLevel); - - if (!_logger.IsEnabled(convertedLogLevel)) - { - return; - } - _logger.Log(convertedLogLevel, exception, message, parameters); } @@ -39,7 +33,7 @@ namespace MQTTnet.Server.Logging case MqttNetLogLevel.Error: return LogLevel.Error; case MqttNetLogLevel.Warning: return LogLevel.Warning; case MqttNetLogLevel.Info: return LogLevel.Information; - case MqttNetLogLevel.Verbose: return LogLevel.Trace; + case MqttNetLogLevel.Verbose: return LogLevel.Debug; } return LogLevel.Debug; diff --git a/Source/MQTTnet.Server/MQTTnet.Server.csproj b/Source/MQTTnet.Server/MQTTnet.Server.csproj index 26ba48a..4ccbe28 100644 --- a/Source/MQTTnet.Server/MQTTnet.Server.csproj +++ b/Source/MQTTnet.Server/MQTTnet.Server.csproj @@ -66,6 +66,9 @@ Always + + Always + Always diff --git a/Source/MQTTnet.Server/Mqtt/CustomMqttFactory.cs b/Source/MQTTnet.Server/Mqtt/CustomMqttFactory.cs index 156871c..06bf546 100644 --- a/Source/MQTTnet.Server/Mqtt/CustomMqttFactory.cs +++ b/Source/MQTTnet.Server/Mqtt/CustomMqttFactory.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using Microsoft.Extensions.Logging; using MQTTnet.Adapter; using MQTTnet.Diagnostics; +using MQTTnet.Server.Configuration; using MQTTnet.Server.Logging; namespace MQTTnet.Server.Mqtt @@ -11,13 +12,28 @@ namespace MQTTnet.Server.Mqtt { private readonly MqttFactory _mqttFactory; - public CustomMqttFactory(ILogger logger) + public CustomMqttFactory(SettingsModel settings, ILogger logger) { + if (settings == null) throw new ArgumentNullException(nameof(settings)); if (logger == null) throw new ArgumentNullException(nameof(logger)); + + // It is important to avoid injecting the logger wrapper to ensure that no + // unused log messages are generated by the MQTTnet library. Debug logging + // has a huge performance impact. + if (settings.EnableDebugLogging) + { + var mqttNetLogger = new MqttNetLoggerWrapper(logger); + _mqttFactory = new MqttFactory(mqttNetLogger); - Logger = new MqttNetLoggerWrapper(logger); + logger.LogWarning("Debug logging is enabled. Performance of MQTTnet Server is decreased!"); + } + else + { + _mqttFactory = new MqttFactory(); + } + + Logger = _mqttFactory.DefaultLogger; - _mqttFactory = new MqttFactory(Logger); } public IMqttNetLogger Logger { get; } diff --git a/Source/MQTTnet.Server/Run.bat b/Source/MQTTnet.Server/Run.bat new file mode 100644 index 0000000..016447e --- /dev/null +++ b/Source/MQTTnet.Server/Run.bat @@ -0,0 +1,2 @@ +@echo off +START "MQTTnet Server" dotnet .\MQTTnet.Server.dll \ No newline at end of file diff --git a/Source/MQTTnet.Server/appsettings.json b/Source/MQTTnet.Server/appsettings.json index d3fdf9f..1cf0bc4 100644 --- a/Source/MQTTnet.Server/appsettings.json +++ b/Source/MQTTnet.Server/appsettings.json @@ -43,12 +43,15 @@ "RetainedApplicationMessages": { "Persist": true, "Filename": "RetainedApplicationMessages.json", - "WriteInterval": 10 // In seconds. - } + "WriteInterval": 10 // In seconds. + }, + "EnableDebugLogging": false }, "Logging": { "LogLevel": { - "Default": "Warning" + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" } }, "AllowedHosts": "*"