mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-24 22:53:04 +08:00
Add MQTTnet debug logging.
This commit is contained in:
@@ -44,5 +44,7 @@
|
|||||||
/// The settings for retained messages.
|
/// The settings for retained messages.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public RetainedApplicationMessagesModel RetainedApplicationMessages { get; set; } = new RetainedApplicationMessagesModel();
|
public RetainedApplicationMessagesModel RetainedApplicationMessages { get; set; } = new RetainedApplicationMessagesModel();
|
||||||
|
|
||||||
|
public bool EnableDebugLogging { get; set; } = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -23,12 +23,6 @@ namespace MQTTnet.Server.Logging
|
|||||||
public void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception)
|
public void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception)
|
||||||
{
|
{
|
||||||
var convertedLogLevel = ConvertLogLevel(logLevel);
|
var convertedLogLevel = ConvertLogLevel(logLevel);
|
||||||
|
|
||||||
if (!_logger.IsEnabled(convertedLogLevel))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.Log(convertedLogLevel, exception, message, parameters);
|
_logger.Log(convertedLogLevel, exception, message, parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,7 +33,7 @@ namespace MQTTnet.Server.Logging
|
|||||||
case MqttNetLogLevel.Error: return LogLevel.Error;
|
case MqttNetLogLevel.Error: return LogLevel.Error;
|
||||||
case MqttNetLogLevel.Warning: return LogLevel.Warning;
|
case MqttNetLogLevel.Warning: return LogLevel.Warning;
|
||||||
case MqttNetLogLevel.Info: return LogLevel.Information;
|
case MqttNetLogLevel.Info: return LogLevel.Information;
|
||||||
case MqttNetLogLevel.Verbose: return LogLevel.Trace;
|
case MqttNetLogLevel.Verbose: return LogLevel.Debug;
|
||||||
}
|
}
|
||||||
|
|
||||||
return LogLevel.Debug;
|
return LogLevel.Debug;
|
||||||
|
|||||||
@@ -66,6 +66,9 @@
|
|||||||
<None Update="LICENSE">
|
<None Update="LICENSE">
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
<None Update="Run.bat">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
<None Update="Scripts\readme.md">
|
<None Update="Scripts\readme.md">
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using MQTTnet.Adapter;
|
using MQTTnet.Adapter;
|
||||||
using MQTTnet.Diagnostics;
|
using MQTTnet.Diagnostics;
|
||||||
|
using MQTTnet.Server.Configuration;
|
||||||
using MQTTnet.Server.Logging;
|
using MQTTnet.Server.Logging;
|
||||||
|
|
||||||
namespace MQTTnet.Server.Mqtt
|
namespace MQTTnet.Server.Mqtt
|
||||||
@@ -11,13 +12,28 @@ namespace MQTTnet.Server.Mqtt
|
|||||||
{
|
{
|
||||||
private readonly MqttFactory _mqttFactory;
|
private readonly MqttFactory _mqttFactory;
|
||||||
|
|
||||||
public CustomMqttFactory(ILogger<MqttServer> logger)
|
public CustomMqttFactory(SettingsModel settings, ILogger<MqttServer> logger)
|
||||||
{
|
{
|
||||||
|
if (settings == null) throw new ArgumentNullException(nameof(settings));
|
||||||
if (logger == null) throw new ArgumentNullException(nameof(logger));
|
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; }
|
public IMqttNetLogger Logger { get; }
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
@echo off
|
||||||
|
START "MQTTnet Server" dotnet .\MQTTnet.Server.dll
|
||||||
@@ -43,12 +43,15 @@
|
|||||||
"RetainedApplicationMessages": {
|
"RetainedApplicationMessages": {
|
||||||
"Persist": true,
|
"Persist": true,
|
||||||
"Filename": "RetainedApplicationMessages.json",
|
"Filename": "RetainedApplicationMessages.json",
|
||||||
"WriteInterval": 10 // In seconds.
|
"WriteInterval": 10 // In seconds.
|
||||||
}
|
},
|
||||||
|
"EnableDebugLogging": false
|
||||||
},
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Warning"
|
"Default": "Debug",
|
||||||
|
"System": "Information",
|
||||||
|
"Microsoft": "Information"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*"
|
||||||
|
|||||||
Reference in New Issue
Block a user