mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 17:23:04 +08:00
53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
// 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);
|
|
}
|
|
}
|
|
}
|
|
} |