diff --git a/CLI/Program.cs b/CLI/Program.cs index 2a2ec2a..9543773 100644 --- a/CLI/Program.cs +++ b/CLI/Program.cs @@ -41,9 +41,10 @@ internal sealed class TrangaCli : Command public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings) { List enabledLoggers = new(); - if(settings.fileLogger.HasValue && settings.fileLogger.Value == true) + if(settings.fileLogger is true) enabledLoggers.Add(Logger.LoggerType.FileLogger); - string? logFilePath = settings.fileLoggerPath ?? "";//TODO path + + string? logFilePath = settings.fileLoggerPath ?? ""; Logger logger = new(enabledLoggers.ToArray(), Console.Out, Console.OutputEncoding, logFilePath); TrangaSettings trangaSettings = new (settings.downloadLocation, settings.workingDirectory, settings.apiPort); diff --git a/Logging/Logger.cs b/Logging/Logger.cs index f250d5b..a4ed3d5 100644 --- a/Logging/Logger.cs +++ b/Logging/Logger.cs @@ -1,9 +1,13 @@ -using System.Text; +using System.Runtime.InteropServices; +using System.Text; namespace Logging; public class Logger : TextWriter { + private static readonly string LogDirectoryPath = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) + ? "/var/log/tranga-api" + : Path.Join(Directory.GetCurrentDirectory(), "logs"); public override Encoding Encoding { get; } public enum LoggerType { @@ -17,13 +21,14 @@ public class Logger : TextWriter public Logger(LoggerType[] enabledLoggers, TextWriter? stdOut, Encoding? encoding, string? logFilePath) { - this.Encoding = encoding ?? Encoding.ASCII; + this.Encoding = encoding ?? Encoding.UTF8; if (enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is not null) _fileLogger = new FileLogger(logFilePath, encoding); else if(enabledLoggers.Contains(LoggerType.FileLogger) && logFilePath is null) { - _fileLogger = null; - throw new ArgumentException($"logFilePath can not be null for LoggerType {LoggerType.FileLogger}"); + logFilePath = Path.Join(LogDirectoryPath, + $"{DateTime.Now.ToShortDateString()} {DateTime.Now.ToShortTimeString()}.log"); + _fileLogger = new FileLogger(logFilePath, encoding); } if (enabledLoggers.Contains(LoggerType.ConsoleLogger) && stdOut is not null)