SyMqtt logging
Diagnostic logging
Section titled “Diagnostic logging”SyMqtt handles internal diagnostic events, connection states, and suppressed exceptions via an abstract logging interface. This architecture provides out-of-the-box visibility for debugging while remaining completely decoupled from specific logging frameworks in production.
Using the built-in console logger
Section titled “Using the built-in console logger”By default, the Logger property is initialized with SyMqttConsoleLogger. If your application runs in a terminal environment, you do not need any additional configuration. The built-in logger formats internal framework milestones and color-codes messages based on their severity level (e.g., warnings in yellow, exceptions in red).
If you need to change the verbosity or completely silence the console output, cast the property to configure its minimum level:
var box = new SyMqttBox();
// Set minimum level to only show warnings and errors, or SyMqttLogLevel.None to mute ((SyMqttConsoleLogger)box.Logger).MinLevel = SyMqttLogLevel.Warning;The default value of MinLevel is SyMqttLogLevel.Information.
Using an external logger (The bridge pattern)
Section titled “Using an external logger (The bridge pattern)”To pipe SyMqtt internal logs into an enterprise aggregation stack (such as Serilog, NLog, or Microsoft.Extensions.Logging), implement the ISyMqttLogger interface. This allows you to build a lightweight adapter that translates framework severity levels into your preferred logging engine.
The interface requires a single method implementation:
public interface ISyMqttLogger { void Log(SyMqttLogLevel level, string message, Exception exception = null); }Integration example: Serilog with a console sink
Section titled “Integration example: Serilog with a console sink”If your application uses Serilog configured to write to a console sink, create a dedicated bridge class to route the telemetry. Note the use of the explicit Serilog.Log namespace to avoid naming conflicts with the interface method:
public class SerilogSyMqttBridge : ISyMqttLogger { public void Log(SyMqttLogLevel level, string message, Exception exception = null) { switch (level) { case SyMqttLogLevel.Debug: Serilog.Log.Debug(message); break; case SyMqttLogLevel.Information: Serilog.Log.Information(message); break; case SyMqttLogLevel.Warning: Serilog.Log.Warning(message); break; case SyMqttLogLevel.Error: Serilog.Log.Error(exception, message); break; } } }To activate the bridge, assign your adapter instance to the Logger property:
var box = new SyMqttBox(); box.Logger = new SerilogSyMqttBridge();