diff --git a/Samples/Client/Client_Connection_Samples.cs b/Samples/Client/Client_Connection_Samples.cs index 62feb648..83c55d21 100644 --- a/Samples/Client/Client_Connection_Samples.cs +++ b/Samples/Client/Client_Connection_Samples.cs @@ -10,7 +10,7 @@ using System.Security.Authentication; using MQTTnet.Client; using MQTTnet.Extensions.WebSocket4Net; using MQTTnet.Formatter; -using MQTTnet.Samples.Helpers; +using MQTTnet.Samples.Shared; namespace MQTTnet.Samples.Client; diff --git a/Samples/Client/Client_Subscribe_Samples.cs b/Samples/Client/Client_Subscribe_Samples.cs index fdde2719..552b8d0e 100644 --- a/Samples/Client/Client_Subscribe_Samples.cs +++ b/Samples/Client/Client_Subscribe_Samples.cs @@ -7,7 +7,7 @@ // ReSharper disable InconsistentNaming using MQTTnet.Client; -using MQTTnet.Samples.Helpers; +using MQTTnet.Samples.Shared; namespace MQTTnet.Samples.Client; diff --git a/Samples/Diagnostics/Logger_Samples.cs b/Samples/Diagnostics/Logger_Samples.cs index 6cd0aa3e..51dbaad8 100644 --- a/Samples/Diagnostics/Logger_Samples.cs +++ b/Samples/Diagnostics/Logger_Samples.cs @@ -8,6 +8,7 @@ using System.Text; using MQTTnet.Diagnostics; +using MQTTnet.Samples.Shared; namespace MQTTnet.Samples.Diagnostics; @@ -22,9 +23,7 @@ public static class Logger_Samples var mqttFactory = new MqttFactory(new MyLogger()); - var mqttClientOptions = mqttFactory.CreateClientOptionsBuilder() - .WithTcpServer("broker.hivemq.com") - .Build(); + var mqttClientOptions = mqttFactory.CreateClientOptionsBuilder().WithTcpServer("broker.hivemq.com").Build(); using (var mqttClient = mqttFactory.CreateMqttClient()) { @@ -32,13 +31,31 @@ public static class Logger_Samples Console.WriteLine("MQTT client is connected."); - var mqttClientDisconnectOptions = mqttFactory.CreateClientDisconnectOptionsBuilder() - .Build(); + var mqttClientDisconnectOptions = mqttFactory.CreateClientDisconnectOptionsBuilder().Build(); await mqttClient.DisconnectAsync(mqttClientDisconnectOptions, CancellationToken.None); } } + public static async Task Log_To_Console() + { + /* + * This sample covers the creation of a custom logger which is used to log to the console. + */ + + var mqttFactory = new MqttFactory(new ConsoleLogger()); + + var mqttClientOptions = mqttFactory.CreateClientOptionsBuilder().WithTcpServer("broker.hivemq.com").Build(); + + using (var mqttClient = mqttFactory.CreateMqttClient()) + { + await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None); + + Console.WriteLine("MQTT client is connected. Press any key to exit."); + Console.ReadKey(true); + } + } + public static async Task Use_Event_Logger() { /* @@ -64,9 +81,7 @@ public static class Logger_Samples var mqttFactory = new MqttFactory(mqttEventLogger); - var mqttClientOptions = mqttFactory.CreateClientOptionsBuilder() - .WithTcpServer("broker.hivemq.com") - .Build(); + var mqttClientOptions = mqttFactory.CreateClientOptionsBuilder().WithTcpServer("broker.hivemq.com").Build(); using (var mqttClient = mqttFactory.CreateMqttClient()) { @@ -74,8 +89,7 @@ public static class Logger_Samples Console.WriteLine("MQTT client is connected."); - var mqttClientDisconnectOptions = mqttFactory.CreateClientDisconnectOptionsBuilder() - .Build(); + var mqttClientDisconnectOptions = mqttFactory.CreateClientDisconnectOptionsBuilder().Build(); await mqttClient.DisconnectAsync(mqttClientDisconnectOptions, CancellationToken.None); } @@ -83,7 +97,7 @@ public static class Logger_Samples sealed class MyLogger : IMqttNetLogger { - public bool IsEnabled { get; set; } = true; + public bool IsEnabled { get; } = true; public void Publish(MqttNetLogLevel logLevel, string source, string message, object[] parameters, Exception exception) { diff --git a/Samples/Program.cs b/Samples/Program.cs index 78f5b255..46bc74c3 100644 --- a/Samples/Program.cs +++ b/Samples/Program.cs @@ -8,6 +8,7 @@ Console.WriteLine("Welcome to MQTTnet samples!"); Console.WriteLine(); var sampleClasses = Assembly.GetExecutingAssembly().GetExportedTypes().OrderBy(c => c.Name).ToList(); +sampleClasses.RemoveAll(c => c.Namespace.Contains(".Shared")); var index = 0; foreach (var sampleClass in sampleClasses) diff --git a/Samples/Server/Server_Simple_Samples.cs b/Samples/Server/Server_Simple_Samples.cs index b2d6787c..99cbeb8d 100644 --- a/Samples/Server/Server_Simple_Samples.cs +++ b/Samples/Server/Server_Simple_Samples.cs @@ -6,8 +6,8 @@ // ReSharper disable UnusedMember.Global // ReSharper disable InconsistentNaming -using MQTTnet.Diagnostics; using MQTTnet.Protocol; +using MQTTnet.Samples.Shared; using MQTTnet.Server; namespace MQTTnet.Samples.Server; @@ -170,50 +170,4 @@ public static class Server_Simple_Samples await server.StartAsync(); return server; } - - 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); - } - } - } - } } diff --git a/Samples/Shared/ConsoleLogger.cs b/Samples/Shared/ConsoleLogger.cs new file mode 100644 index 00000000..38c63b5c --- /dev/null +++ b/Samples/Shared/ConsoleLogger.cs @@ -0,0 +1,53 @@ +// 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); + } + } + } +} \ No newline at end of file diff --git a/Samples/Helpers/ObjectExtensions.cs b/Samples/Shared/ObjectExtensions.cs similarity index 95% rename from Samples/Helpers/ObjectExtensions.cs rename to Samples/Shared/ObjectExtensions.cs index 4384fa92..cc045f69 100644 --- a/Samples/Helpers/ObjectExtensions.cs +++ b/Samples/Shared/ObjectExtensions.cs @@ -4,7 +4,7 @@ using System.Text.Json; -namespace MQTTnet.Samples.Helpers; +namespace MQTTnet.Samples.Shared; internal static class ObjectExtensions {