// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using MQTTnet.Diagnostics; namespace MQTTnet.Samples.Shared; public sealed class ConsoleLogger : IMqttNetLogger { readonly object _consoleSyncRoot = new(); public bool IsEnabled => true; public void Publish(MqttNetLogLevel logLevel, string source, string message, object[]? parameters, Exception? exception) { var foregroundColor = ConsoleColor.White; switch (logLevel) { case MqttNetLogLevel.Verbose: foregroundColor = ConsoleColor.White; break; case MqttNetLogLevel.Info: foregroundColor = ConsoleColor.Green; break; case MqttNetLogLevel.Warning: foregroundColor = ConsoleColor.DarkYellow; break; case MqttNetLogLevel.Error: foregroundColor = ConsoleColor.Red; break; } if (parameters?.Length > 0) { message = string.Format(message, parameters); } lock (_consoleSyncRoot) { Console.ForegroundColor = foregroundColor; Console.WriteLine(message); if (exception != null) { Console.WriteLine(exception); } } } }