Changed default log-folder path, and log-encoding to utf8

This commit is contained in:
glax 2023-08-31 17:06:57 +02:00
parent 89c5f4b820
commit 3167f6c3e6
2 changed files with 12 additions and 6 deletions

View File

@ -41,9 +41,10 @@ internal sealed class TrangaCli : Command<TrangaCli.Settings>
public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings)
{
List<Logger.LoggerType> 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);

View File

@ -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)