-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
miltt
committed
Jun 18, 2023
1 parent
455c9d4
commit 99dc765
Showing
7 changed files
with
97 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |