using System; namespace MQTTnet.Diagnostics { public class MqttNetLogger : IMqttNetLogger { private readonly string _logId; public MqttNetLogger(string logId = null) { _logId = logId; } public event EventHandler LogMessagePublished; public void Trace(string message, params object[] parameters) { Publish(MqttNetLogLevel.Verbose, null, message, parameters); } public void Info(string message, params object[] parameters) { Publish(MqttNetLogLevel.Info, null, message, parameters); } public void Warning(Exception exception, string message, params object[] parameters) { Publish(MqttNetLogLevel.Warning, exception, message, parameters); } public void Warning(string message, params object[] parameters) { Warning(null, message, parameters); } public void Error(Exception exception, string message, params object[] parameters) { Publish(MqttNetLogLevel.Error, exception, message, parameters); } public void Error(string message, params object[] parameters) { Warning(null, message, parameters); } private void Publish(MqttNetLogLevel logLevel, Exception exception, string message, object[] parameters) { var hasLocalListeners = LogMessagePublished != null; var hasGlobalListeners = MqttNetGlobalLogger.HasListeners; if (!hasLocalListeners && !hasGlobalListeners) { return; } if (parameters.Length > 0) { message = string.Format(message, parameters); } var traceMessage = new MqttNetLogMessage(_logId, DateTime.Now, Environment.CurrentManagedThreadId, typeof(TSource).Name, logLevel, message, exception); if (hasGlobalListeners) { MqttNetGlobalLogger.Publish(traceMessage); } if (hasLocalListeners) { LogMessagePublished?.Invoke(this, new MqttNetLogMessagePublishedEventArgs(traceMessage)); } } } }