Skip to content

Commit

Permalink
added filelogger
Browse files Browse the repository at this point in the history
  • Loading branch information
miltt committed Jun 18, 2023
1 parent 455c9d4 commit 99dc765
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 48 deletions.
2 changes: 2 additions & 0 deletions console/AppConfig/AppConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<AppConfigManager> CreateAsync(IJsonReader jsonReader, CancellationToken cancellationToken)
{
Expand Down
12 changes: 12 additions & 0 deletions console/AppConfig/LoggerConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Sync.Config
{
public sealed class LoggerConfig
{
public string? FilePath { get; }

public LoggerConfig(string? filePath)
{
FilePath = filePath;
}
}
}
43 changes: 0 additions & 43 deletions console/Logger.cs

This file was deleted.

11 changes: 6 additions & 5 deletions console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}
}
26 changes: 26 additions & 0 deletions console/Utilities/Logger/BaseLogger.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
15 changes: 15 additions & 0 deletions console/Utilities/Logger/ConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Sync.Log
{
public sealed class ConsoleLogger : BaseLogger
{
public ConsoleLogger()
: base()
{
}

protected override void AddInternal(string message)
{
Console.WriteLine(message);
}
}
}
36 changes: 36 additions & 0 deletions console/Utilities/Logger/FIleLogger.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}

0 comments on commit 99dc765

Please sign in to comment.