From 99dc7655246557e01d3d5d2699bfe577c68f6e0b Mon Sep 17 00:00:00 2001 From: miltt Date: Sun, 18 Jun 2023 12:18:41 +0200 Subject: [PATCH] added filelogger --- console/AppConfig/AppConfigManager.cs | 2 ++ console/AppConfig/LoggerConfig.cs | 12 +++++++ console/Logger.cs | 43 ----------------------- console/Program.cs | 11 +++--- console/Utilities/Logger/BaseLogger.cs | 26 ++++++++++++++ console/Utilities/Logger/ConsoleLogger.cs | 15 ++++++++ console/Utilities/Logger/FIleLogger.cs | 36 +++++++++++++++++++ 7 files changed, 97 insertions(+), 48 deletions(-) create mode 100644 console/AppConfig/LoggerConfig.cs delete mode 100644 console/Logger.cs create mode 100644 console/Utilities/Logger/BaseLogger.cs create mode 100644 console/Utilities/Logger/ConsoleLogger.cs create mode 100644 console/Utilities/Logger/FIleLogger.cs diff --git a/console/AppConfig/AppConfigManager.cs b/console/AppConfig/AppConfigManager.cs index 7594066..bd311d1 100644 --- a/console/AppConfig/AppConfigManager.cs +++ b/console/AppConfig/AppConfigManager.cs @@ -7,6 +7,7 @@ private sealed class AppConfig public FitbitConfig? FitbitConfig { get; set; } public IntervalsConfig? IntervalsConfig { get; set; } public DateTime LastUpdateDate { get; set; } + public LoggerConfig? LoggerConfig { get; set; } } private const string FilePath = "../console/AppConfig/config.json"; @@ -15,6 +16,7 @@ private sealed class AppConfig public IFitbitConfig? FitbitConfig => _appConfig.FitbitConfig; public IIntervalsConfig? IntervalsConfig => _appConfig.IntervalsConfig; public DateTime LastUpdateDate => _appConfig.LastUpdateDate; + public LoggerConfig? LoggerConfig => _appConfig.LoggerConfig; public static async Task CreateAsync(IJsonReader jsonReader, CancellationToken cancellationToken) { diff --git a/console/AppConfig/LoggerConfig.cs b/console/AppConfig/LoggerConfig.cs new file mode 100644 index 0000000..69cd752 --- /dev/null +++ b/console/AppConfig/LoggerConfig.cs @@ -0,0 +1,12 @@ +namespace Sync.Config +{ + public sealed class LoggerConfig + { + public string? FilePath { get; } + + public LoggerConfig(string? filePath) + { + FilePath = filePath; + } + } +} \ No newline at end of file diff --git a/console/Logger.cs b/console/Logger.cs deleted file mode 100644 index 3614f09..0000000 --- a/console/Logger.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Text; - -namespace Sync.Log -{ - public class Logger : ILogger - { - public enum OutputType - { - Console = 0, - File, - } - - private readonly StringBuilder _stringBuilder; - private readonly OutputType _type; - - public Logger(OutputType type) - { - _stringBuilder = new StringBuilder(); - _type = type; - } - - public void Add(string message) - { - if (string.IsNullOrEmpty(message)) - return; - - switch (_type) - { - case OutputType.File: - _stringBuilder.AppendLine(message); - break; - default: - Console.WriteLine(message); - break; - } - } - - public void Flush() - { - // TODO: - } - } -} \ No newline at end of file diff --git a/console/Program.cs b/console/Program.cs index 57c3722..b722c73 100644 --- a/console/Program.cs +++ b/console/Program.cs @@ -12,20 +12,18 @@ static void Main(string[] args) { // TODO: args - var logger = new Logger(Logger.OutputType.Console); var jsonFileManager = new JsonFileManager(); - UpdateAsync(logger, jsonFileManager, CancellationToken.None).WaitAndUnwrapException(); + UpdateAsync(jsonFileManager, CancellationToken.None).WaitAndUnwrapException(); } - private static async Task UpdateAsync(Logger logger, JsonFileManager jsonFileManager, CancellationToken cancellationToken) + private static async Task UpdateAsync(JsonFileManager jsonFileManager, CancellationToken cancellationToken) { - if (logger is null) - throw new ArgumentNullException(nameof(logger)); if (jsonFileManager is null) throw new ArgumentNullException(nameof(jsonFileManager)); var configManager = await AppConfigManager.CreateAsync(jsonFileManager, cancellationToken); + var logger = BaseLogger.Create(configManager.LoggerConfig?.FilePath); var datePicker = new DatePicker(configManager.LastUpdateDate); foreach (var date in datePicker) @@ -58,6 +56,9 @@ private static async Task UpdateAsync(Logger logger, JsonFileManager jsonFileMan logger.Add(e.Message); } } + + if (logger is FileLogger fileLogger) + await fileLogger.FlushAsync(cancellationToken); } } } \ No newline at end of file diff --git a/console/Utilities/Logger/BaseLogger.cs b/console/Utilities/Logger/BaseLogger.cs new file mode 100644 index 0000000..b2fba0c --- /dev/null +++ b/console/Utilities/Logger/BaseLogger.cs @@ -0,0 +1,26 @@ +namespace Sync.Log +{ + public abstract class BaseLogger : ILogger + { + public static ILogger Create(string? filePath) + { + return !string.IsNullOrEmpty(filePath) + ? new FileLogger(filePath) + : new ConsoleLogger(); + } + + protected BaseLogger() + { + } + + public void Add(string message) + { + if (string.IsNullOrEmpty(message)) + return; + + AddInternal(message); + } + + protected abstract void AddInternal(string message); + } +} \ No newline at end of file diff --git a/console/Utilities/Logger/ConsoleLogger.cs b/console/Utilities/Logger/ConsoleLogger.cs new file mode 100644 index 0000000..b0041dc --- /dev/null +++ b/console/Utilities/Logger/ConsoleLogger.cs @@ -0,0 +1,15 @@ +namespace Sync.Log +{ + public sealed class ConsoleLogger : BaseLogger + { + public ConsoleLogger() + : base() + { + } + + protected override void AddInternal(string message) + { + Console.WriteLine(message); + } + } +} \ No newline at end of file diff --git a/console/Utilities/Logger/FIleLogger.cs b/console/Utilities/Logger/FIleLogger.cs new file mode 100644 index 0000000..bfe757e --- /dev/null +++ b/console/Utilities/Logger/FIleLogger.cs @@ -0,0 +1,36 @@ +using System.Text; + +namespace Sync.Log +{ + public sealed class FileLogger : BaseLogger + { + private const string FileName = "IntervalsSyncLog.txt"; + private readonly StringBuilder _stringBuilder; + private readonly string _path; + + public FileLogger(string? filePath) + : base() + { + if (string.IsNullOrEmpty(filePath)) + throw new ArgumentException($"'{nameof(filePath)}' cannot be null or empty.", nameof(filePath)); + + _stringBuilder = new StringBuilder(); + _path = $"{filePath}\\{FileName}"; + } + + protected override void AddInternal(string message) + { + _stringBuilder.Append(DateTime.Now.ToString()).Append(": ").AppendLine(message); + } + + public async Task FlushAsync(CancellationToken cancellationToken) + { + using (var stream = File.Exists(_path) + ? File.AppendText(_path) + : new StreamWriter(_path)) + { + await stream.WriteLineAsync(_stringBuilder, cancellationToken); + } + } + } +} \ No newline at end of file