2023-08-31 17:06:57 +02:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
2023-05-20 21:47:54 +02:00
|
|
|
|
|
|
|
|
|
namespace Logging;
|
|
|
|
|
|
|
|
|
|
public class Logger : TextWriter
|
|
|
|
|
{
|
2023-08-31 17:06:57 +02:00
|
|
|
|
private static readonly string LogDirectoryPath = RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
|
|
|
|
|
? "/var/log/tranga-api"
|
|
|
|
|
: Path.Join(Directory.GetCurrentDirectory(), "logs");
|
2023-09-05 20:02:12 +02:00
|
|
|
|
public string? logFilePath => _fileLogger?.logFilePath;
|
2023-05-20 21:47:54 +02:00
|
|
|
|
public override Encoding Encoding { get; }
|
|
|
|
|
public enum LoggerType
|
|
|
|
|
{
|
|
|
|
|
FileLogger,
|
2023-05-21 02:10:32 +02:00
|
|
|
|
ConsoleLogger
|
2023-05-20 21:47:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-16 17:33:15 +02:00
|
|
|
|
private readonly FileLogger? _fileLogger;
|
|
|
|
|
private readonly FormattedConsoleLogger? _formattedConsoleLogger;
|
|
|
|
|
private readonly MemoryLogger _memoryLogger;
|
2023-05-20 21:47:54 +02:00
|
|
|
|
|
|
|
|
|
public Logger(LoggerType[] enabledLoggers, TextWriter? stdOut, Encoding? encoding, string? logFilePath)
|
|
|
|
|
{
|
2023-08-31 17:06:57 +02:00
|
|
|
|
this.Encoding = encoding ?? Encoding.UTF8;
|
2023-05-20 21:47:54 +02:00
|
|
|
|
if (enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is not null)
|
2023-07-16 17:33:15 +02:00
|
|
|
|
_fileLogger = new FileLogger(logFilePath, encoding);
|
2023-08-24 12:13:34 +02:00
|
|
|
|
else if(enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is null)
|
2023-05-20 21:47:54 +02:00
|
|
|
|
{
|
2023-08-31 17:06:57 +02:00
|
|
|
|
logFilePath = Path.Join(LogDirectoryPath,
|
|
|
|
|
$"{DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()}.log");
|
|
|
|
|
_fileLogger = new FileLogger(logFilePath, encoding);
|
2023-05-20 21:47:54 +02:00
|
|
|
|
}
|
2023-07-16 17:33:15 +02:00
|
|
|
|
|
|
|
|
|
if (enabledLoggers.Contains(LoggerType.ConsoleLogger) && stdOut is not null)
|
|
|
|
|
{
|
|
|
|
|
_formattedConsoleLogger = new FormattedConsoleLogger(stdOut, encoding);
|
|
|
|
|
}
|
2023-07-31 00:31:19 +02:00
|
|
|
|
else if (enabledLoggers.Contains(LoggerType.ConsoleLogger) && stdOut is null)
|
2023-07-16 17:33:15 +02:00
|
|
|
|
{
|
|
|
|
|
_formattedConsoleLogger = null;
|
|
|
|
|
throw new ArgumentException($"stdOut can not be null for LoggerType {LoggerType.ConsoleLogger}");
|
|
|
|
|
}
|
|
|
|
|
_memoryLogger = new MemoryLogger(encoding);
|
2023-05-20 21:47:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 22:10:24 +02:00
|
|
|
|
public void WriteLine(string caller, string? value)
|
2023-05-20 21:47:54 +02:00
|
|
|
|
{
|
|
|
|
|
value = value is null ? Environment.NewLine : string.Concat(value, Environment.NewLine);
|
|
|
|
|
|
|
|
|
|
Write(caller, value);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 22:10:24 +02:00
|
|
|
|
public void Write(string caller, string? value)
|
2023-05-20 21:47:54 +02:00
|
|
|
|
{
|
|
|
|
|
if (value is null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_fileLogger?.Write(caller, value);
|
|
|
|
|
_formattedConsoleLogger?.Write(caller, value);
|
2023-05-21 02:10:32 +02:00
|
|
|
|
_memoryLogger.Write(caller, value);
|
2023-05-20 21:47:54 +02:00
|
|
|
|
}
|
2023-05-21 02:10:32 +02:00
|
|
|
|
|
|
|
|
|
public string[] Tail(uint? lines)
|
|
|
|
|
{
|
|
|
|
|
return _memoryLogger.Tail(lines);
|
|
|
|
|
}
|
2023-05-21 14:44:33 +02:00
|
|
|
|
|
|
|
|
|
public string[] GetNewLines()
|
|
|
|
|
{
|
|
|
|
|
return _memoryLogger.GetNewLines();
|
|
|
|
|
}
|
2023-09-05 20:02:12 +02:00
|
|
|
|
|
|
|
|
|
public string[] GetLog()
|
|
|
|
|
{
|
|
|
|
|
return _memoryLogger.GetLogMessages();
|
|
|
|
|
}
|
2023-05-20 21:47:54 +02:00
|
|
|
|
}
|