From 032edb81d62ef0f2b46cc8fc132830fedfb049bf Mon Sep 17 00:00:00 2001 From: Alexandru Macocian Date: Fri, 1 Oct 2021 14:56:38 +0200 Subject: [PATCH] Integrate Microsoft.Logging.Abstractions --- MTSC.UnitTests/E2ETests.cs | 8 ---- MTSC/ClientSide/Client.cs | 28 ++++------- MTSC/Logging/ConsoleLogger.cs | 29 ------------ MTSC/Logging/DebugConsoleLogger.cs | 27 ----------- MTSC/Logging/ILogger.cs | 25 ---------- MTSC/MTSC.csproj | 7 +-- MTSC/ServerSide/Server.cs | 75 ++++++++++++------------------ 7 files changed, 43 insertions(+), 156 deletions(-) delete mode 100644 MTSC/Logging/ConsoleLogger.cs delete mode 100644 MTSC/Logging/DebugConsoleLogger.cs delete mode 100644 MTSC/Logging/ILogger.cs diff --git a/MTSC.UnitTests/E2ETests.cs b/MTSC.UnitTests/E2ETests.cs index 48ea7e7..1c027a2 100644 --- a/MTSC.UnitTests/E2ETests.cs +++ b/MTSC.UnitTests/E2ETests.cs @@ -1,12 +1,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using MTSC.Common.Ftp; -using MTSC.Common.Ftp.FtpModules; using MTSC.Common.Http; using MTSC.Common.Http.RoutingModules; -using MTSC.Common.Http.ServerModules; -using MTSC.Common.WebSockets; using MTSC.Exceptions; -using MTSC.Logging; using MTSC.ServerSide.Handlers; using MTSC.ServerSide.Schedulers; using MTSC.UnitTests.RoutingModules; @@ -18,7 +13,6 @@ using System.Linq; using System.Net; using System.Net.Http; using System.Net.WebSockets; -using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -59,8 +53,6 @@ namespace MTSC.UnitTests .AddRoute(HttpMessage.HttpMethods.Post, "some-module/{someValue}/test/{intValue}/test") .WithFragmentsExpirationTime(TimeSpan.FromMilliseconds(3000)) .WithMaximumSize(250000)) - .AddLogger(new ConsoleLogger()) - .AddLogger(new DebugConsoleLogger()) .AddExceptionHandler(new ExceptionConsoleLogger()) .SetScheduler(new ParallelScheduler()) .WithSslAuthenticationTimeout(TimeSpan.FromMilliseconds(100)); diff --git a/MTSC/ClientSide/Client.cs b/MTSC/ClientSide/Client.cs index 30ede81..8ed8fb2 100644 --- a/MTSC/ClientSide/Client.cs +++ b/MTSC/ClientSide/Client.cs @@ -1,10 +1,9 @@ -using MTSC.Client.Handlers; +using Microsoft.Extensions.Logging; +using MTSC.Client.Handlers; using MTSC.ClientSide; using MTSC.Common; using MTSC.Exceptions; -using MTSC.Logging; using System; -using System.Collections; using System.Collections.Generic; using System.IO; using System.Net; @@ -22,10 +21,10 @@ namespace MTSC.Client public sealed class Client { #region Fields + private ILogger logger; private TcpClient tcpClient; private CancellationTokenSource cancelMonitorToken; private readonly List handlers = new(); - private readonly List loggers = new(); private readonly List exceptionHandlers = new(); private readonly Queue messageQueue = new(); private SslStream sslStream = null; @@ -123,10 +122,7 @@ namespace MTSC.Client /// Message to be logged. public void Log(string log) { - foreach (var logger in this.loggers) - { - logger.Log(log); - } + this.logger?.LogInformation(log); } /// /// Logs the debug message onto the associated loggers. @@ -134,10 +130,7 @@ namespace MTSC.Client /// public void LogDebug(string debugMessage) { - foreach (var logger in this.loggers) - { - logger.LogDebug(debugMessage); - } + this.logger?.LogDebug(debugMessage); } /// /// Sets the server address. @@ -180,13 +173,13 @@ namespace MTSC.Client return this; } /// - /// Adds a logger onto the client. + /// Sets logger for the client. /// /// Logger to be added. /// This client object. - public Client AddLogger(ILogger logger) + public Client WithLogger(ILogger logger) { - this.loggers.Add(logger); + this.logger = logger; return this; } /// @@ -237,10 +230,7 @@ namespace MTSC.Client this.sslStream.AuthenticateAsClient(this.Address); } - foreach(var logger in this.loggers) - { - logger.Log("Connected to: " + this.tcpClient.Client.RemoteEndPoint.ToString()); - } + this.Log("Connected to: " + this.tcpClient.Client.RemoteEndPoint.ToString()); foreach (var handler in this.handlers) { diff --git a/MTSC/Logging/ConsoleLogger.cs b/MTSC/Logging/ConsoleLogger.cs deleted file mode 100644 index 6f325a3..0000000 --- a/MTSC/Logging/ConsoleLogger.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; - -namespace MTSC.Logging -{ - /// - /// Basic logger that outputs the log messages to the console. - /// - public sealed class ConsoleLogger : ILogger - { - /// - /// Outputs the message to the console. - /// - /// - public bool Log(string message) - { - Console.WriteLine(message); - return false; - } - /// - /// Ignores debug messages. - /// - /// - /// False - public bool LogDebug(string message) - { - return false; - } - } -} diff --git a/MTSC/Logging/DebugConsoleLogger.cs b/MTSC/Logging/DebugConsoleLogger.cs deleted file mode 100644 index a974cba..0000000 --- a/MTSC/Logging/DebugConsoleLogger.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace MTSC.Logging -{ - public sealed class DebugConsoleLogger : ILogger - { - /// - /// Ignores simple logging messages. - /// - /// - /// False - public bool Log(string message) - { - return false; - } - /// - /// Outputs the debug message to console. - /// - /// Message to be output. - /// False - public bool LogDebug(string message) - { - Console.WriteLine(message); - return false; - } - } -} diff --git a/MTSC/Logging/ILogger.cs b/MTSC/Logging/ILogger.cs deleted file mode 100644 index 050a128..0000000 --- a/MTSC/Logging/ILogger.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace MTSC.Logging -{ - /// - /// Interface for loggers. - /// - public interface ILogger - { - /// - /// Logs the received message. - /// - /// Message to be received. - /// True if the message has been logged and no other logger should log this message. - bool Log(string message); - /// - /// Logs the received debug message. - /// - /// Debug message to be logged. - /// True if the message has been logged and no other logger should log this message. - bool LogDebug(string message); - } -} diff --git a/MTSC/MTSC.csproj b/MTSC/MTSC.csproj index dbecaca..d2dc24d 100644 --- a/MTSC/MTSC.csproj +++ b/MTSC/MTSC.csproj @@ -5,13 +5,13 @@ netcoreapp2.1;net48;netstandard2.0;netcoreapp3.1;net5.0 - 4.1.2 + 4.2.0 latest Alexandru-Victor Macocian MTSC Modular TCP Server and Client - 4.1.2.0 - 4.1.2.0 + 4.2.0.0 + 4.2.0.0 true AnyCPU;x64 https://github.com/AlexMacocian/MTSC @@ -27,6 +27,7 @@ + diff --git a/MTSC/ServerSide/Server.cs b/MTSC/ServerSide/Server.cs index 0286dd2..78bc6af 100644 --- a/MTSC/ServerSide/Server.cs +++ b/MTSC/ServerSide/Server.cs @@ -1,8 +1,7 @@ -using MTSC.Common; +using Microsoft.Extensions.Logging; +using MTSC.Common; using MTSC.Exceptions; -using MTSC.Logging; using MTSC.ServerSide.Handlers; -using MTSC.ServerSide.Resources; using MTSC.ServerSide.Schedulers; using MTSC.ServerSide.UsageMonitors; using Slim; @@ -32,10 +31,10 @@ namespace MTSC.ServerSide private readonly List clients = new(); private readonly List toRemove = new(); private readonly List handlers = new(); - private readonly List loggers = new(); private readonly List exceptionHandlers = new(); private readonly List serverUsageMonitors = new(); private readonly ProducerConsumerQueue<(ClientData, byte[])> messageOutQueue = new(); + private ILogger logger = null; #endregion #region Private Properties private IConsumerQueue ConsumerClientQueue { get => this.addQueue; } @@ -368,16 +367,6 @@ namespace MTSC.ServerSide return this; } /// - /// Adds a to the server. - /// - /// Logger to be added. - /// This server object. - public Server AddLogger(ILogger logger) - { - this.loggers.Add(logger); - return this; - } - /// /// Adds an to the server. /// /// Handler to be added. @@ -442,23 +431,6 @@ namespace MTSC.ServerSide return null; } /// - /// Get logger of provided type - /// - /// - /// - public T GetLogger() where T : class - { - foreach (var logger in this.loggers) - { - if (logger is T) - { - return logger as T; - } - } - - return null; - } - /// /// Get server usage monitor of provided type /// /// @@ -487,30 +459,24 @@ namespace MTSC.ServerSide /// /// Adds a message to be logged by the associated loggers. /// + /// + /// This method will not log anything if no or can be resolved by the . + /// /// Message to be logged public void Log(string log) { - foreach (var logger in this.loggers) - { - if (logger.Log(log)) - { - break; - } - } + this.logger?.LogInformation(log); } /// /// Adds a debug message to be logged by the associated loggers. /// + /// + /// This method will not log anything if no or can be resolved by the . + /// /// Debug message to be logged public void LogDebug(string debugMessage) { - foreach (var logger in this.loggers) - { - if (logger.LogDebug(debugMessage)) - { - break; - } - } + this.logger?.LogDebug(debugMessage); } /// /// Blocking method. Runs the server on the current thread. @@ -518,6 +484,25 @@ namespace MTSC.ServerSide public void Run() { this.listener?.Stop(); + if (this.logger is null) + { + // Try to get a logger, in case it exists. If not, keep it null. + try + { + this.logger = this.ServiceManager.GetService>(); + } + catch + { + try + { + this.logger = this.ServiceManager.GetService(); + } + catch + { + } + } + } + this.listener = new TcpListener(this.IPAddress, this.Port); this.listener.Start(); this.running = true;