mirror of
https://github.com/AlexMacocian/MTSC.git
synced 2026-07-22 18:29:31 +00:00
Integrate Microsoft.Logging.Abstractions
This commit is contained in:
@@ -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<SomeRoutingModule>(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));
|
||||
|
||||
@@ -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<IHandler> handlers = new();
|
||||
private readonly List<ILogger> loggers = new();
|
||||
private readonly List<IExceptionHandler> exceptionHandlers = new();
|
||||
private readonly Queue<byte[]> messageQueue = new();
|
||||
private SslStream sslStream = null;
|
||||
@@ -123,10 +122,7 @@ namespace MTSC.Client
|
||||
/// <param name="log">Message to be logged.</param>
|
||||
public void Log(string log)
|
||||
{
|
||||
foreach (var logger in this.loggers)
|
||||
{
|
||||
logger.Log(log);
|
||||
}
|
||||
this.logger?.LogInformation(log);
|
||||
}
|
||||
/// <summary>
|
||||
/// Logs the debug message onto the associated loggers.
|
||||
@@ -134,10 +130,7 @@ namespace MTSC.Client
|
||||
/// <param name="debugMessage"></param>
|
||||
public void LogDebug(string debugMessage)
|
||||
{
|
||||
foreach (var logger in this.loggers)
|
||||
{
|
||||
logger.LogDebug(debugMessage);
|
||||
}
|
||||
this.logger?.LogDebug(debugMessage);
|
||||
}
|
||||
/// <summary>
|
||||
/// Sets the server address.
|
||||
@@ -180,13 +173,13 @@ namespace MTSC.Client
|
||||
return this;
|
||||
}
|
||||
/// <summary>
|
||||
/// Adds a logger onto the client.
|
||||
/// Sets logger for the client.
|
||||
/// </summary>
|
||||
/// <param name="logger">Logger to be added.</param>
|
||||
/// <returns>This client object.</returns>
|
||||
public Client AddLogger(ILogger logger)
|
||||
public Client WithLogger(ILogger logger)
|
||||
{
|
||||
this.loggers.Add(logger);
|
||||
this.logger = logger;
|
||||
return this;
|
||||
}
|
||||
/// <summary>
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace MTSC.Logging
|
||||
{
|
||||
/// <summary>
|
||||
/// Basic logger that outputs the log messages to the console.
|
||||
/// </summary>
|
||||
public sealed class ConsoleLogger : ILogger
|
||||
{
|
||||
/// <summary>
|
||||
/// Outputs the message to the console.
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
public bool Log(string message)
|
||||
{
|
||||
Console.WriteLine(message);
|
||||
return false;
|
||||
}
|
||||
/// <summary>
|
||||
/// Ignores debug messages.
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <returns>False</returns>
|
||||
public bool LogDebug(string message)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace MTSC.Logging
|
||||
{
|
||||
public sealed class DebugConsoleLogger : ILogger
|
||||
{
|
||||
/// <summary>
|
||||
/// Ignores simple logging messages.
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <returns>False</returns>
|
||||
public bool Log(string message)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
/// <summary>
|
||||
/// Outputs the debug message to console.
|
||||
/// </summary>
|
||||
/// <param name="message">Message to be output.</param>
|
||||
/// <returns>False</returns>
|
||||
public bool LogDebug(string message)
|
||||
{
|
||||
Console.WriteLine(message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace MTSC.Logging
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for loggers.
|
||||
/// </summary>
|
||||
public interface ILogger
|
||||
{
|
||||
/// <summary>
|
||||
/// Logs the received message.
|
||||
/// </summary>
|
||||
/// <param name="message">Message to be received.</param>
|
||||
/// <returns>True if the message has been logged and no other logger should log this message.</returns>
|
||||
bool Log(string message);
|
||||
/// <summary>
|
||||
/// Logs the received debug message.
|
||||
/// </summary>
|
||||
/// <param name="message">Debug message to be logged.</param>
|
||||
/// <returns>True if the message has been logged and no other logger should log this message.</returns>
|
||||
bool LogDebug(string message);
|
||||
}
|
||||
}
|
||||
+4
-3
@@ -5,13 +5,13 @@
|
||||
<TargetFrameworks>netcoreapp2.1;net48;netstandard2.0;netcoreapp3.1;net5.0</TargetFrameworks>
|
||||
<ApplicationIcon />
|
||||
<StartupObject />
|
||||
<Version>4.1.2</Version>
|
||||
<Version>4.2.0</Version>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Authors>Alexandru-Victor Macocian</Authors>
|
||||
<Product>MTSC</Product>
|
||||
<Description>Modular TCP Server and Client</Description>
|
||||
<AssemblyVersion>4.1.2.0</AssemblyVersion>
|
||||
<FileVersion>4.1.2.0</FileVersion>
|
||||
<AssemblyVersion>4.2.0.0</AssemblyVersion>
|
||||
<FileVersion>4.2.0.0</FileVersion>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Platforms>AnyCPU;x64</Platforms>
|
||||
<PackageProjectUrl>https://github.com/AlexMacocian/MTSC</PackageProjectUrl>
|
||||
@@ -27,6 +27,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
<PackageReference Include="Slim" Version="1.5.2" />
|
||||
</ItemGroup>
|
||||
|
||||
+30
-45
@@ -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<ClientData> clients = new();
|
||||
private readonly List<ClientData> toRemove = new();
|
||||
private readonly List<IHandler> handlers = new();
|
||||
private readonly List<ILogger> loggers = new();
|
||||
private readonly List<IExceptionHandler> exceptionHandlers = new();
|
||||
private readonly List<IServerUsageMonitor> serverUsageMonitors = new();
|
||||
private readonly ProducerConsumerQueue<(ClientData, byte[])> messageOutQueue = new();
|
||||
private ILogger logger = null;
|
||||
#endregion
|
||||
#region Private Properties
|
||||
private IConsumerQueue<ClientData> ConsumerClientQueue { get => this.addQueue; }
|
||||
@@ -368,16 +367,6 @@ namespace MTSC.ServerSide
|
||||
return this;
|
||||
}
|
||||
/// <summary>
|
||||
/// Adds a <see cref="ILogger"/> to the server.
|
||||
/// </summary>
|
||||
/// <param name="logger">Logger to be added.</param>
|
||||
/// <returns>This server object.</returns>
|
||||
public Server AddLogger(ILogger logger)
|
||||
{
|
||||
this.loggers.Add(logger);
|
||||
return this;
|
||||
}
|
||||
/// <summary>
|
||||
/// Adds an <see cref="IExceptionHandler"/> to the server.
|
||||
/// </summary>
|
||||
/// <param name="handler">Handler to be added.</param>
|
||||
@@ -442,23 +431,6 @@ namespace MTSC.ServerSide
|
||||
return null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Get logger of provided type
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public T GetLogger<T>() where T : class
|
||||
{
|
||||
foreach (var logger in this.loggers)
|
||||
{
|
||||
if (logger is T)
|
||||
{
|
||||
return logger as T;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Get server usage monitor of provided type
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
@@ -487,30 +459,24 @@ namespace MTSC.ServerSide
|
||||
/// <summary>
|
||||
/// Adds a message to be logged by the associated loggers.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method will not log anything if no <see cref="ILogger"/> or <see cref="ILogger{Server}"/> can be resolved by the <see cref="IServiceManager"/>.
|
||||
/// </remarks>
|
||||
/// <param name="log">Message to be logged</param>
|
||||
public void Log(string log)
|
||||
{
|
||||
foreach (var logger in this.loggers)
|
||||
{
|
||||
if (logger.Log(log))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.logger?.LogInformation(log);
|
||||
}
|
||||
/// <summary>
|
||||
/// Adds a debug message to be logged by the associated loggers.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method will not log anything if no <see cref="ILogger"/> or <see cref="ILogger{TCategoryName}"/> can be resolved by the <see cref="IServiceManager"/>.
|
||||
/// </remarks>
|
||||
/// <param name="debugMessage">Debug message to be logged</param>
|
||||
public void LogDebug(string debugMessage)
|
||||
{
|
||||
foreach (var logger in this.loggers)
|
||||
{
|
||||
if (logger.LogDebug(debugMessage))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.logger?.LogDebug(debugMessage);
|
||||
}
|
||||
/// <summary>
|
||||
/// 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<ILogger<Server>>();
|
||||
}
|
||||
catch
|
||||
{
|
||||
try
|
||||
{
|
||||
this.logger = this.ServiceManager.GetService<ILogger>();
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.listener = new TcpListener(this.IPAddress, this.Port);
|
||||
this.listener.Start();
|
||||
this.running = true;
|
||||
|
||||
Reference in New Issue
Block a user