Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

other: Code refactoring for LoggingService.cs #1217

Merged
merged 5 commits into from
Apr 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions src/Notepads/Services/LoggingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public static class LoggingService
{
private const string MessageFormat = "{0} [{1}] {2}"; // {timestamp} [{level}] {message}
private const string MessageFormatString = "{0} [{1}] {2}"; // {timestamp} [{level}] {message}

private static readonly ConcurrentQueue<string> MessageQueue = new ConcurrentQueue<string>();
private static readonly SemaphoreSlim SemaphoreSlim = new SemaphoreSlim(1, 1);
Expand Down Expand Up @@ -69,7 +69,8 @@ public static void LogException(Exception ex, bool consoleOnly = false)

private static void LogMessage(string level, string message, bool consoleOnly)
{
string formattedMessage = string.Format(MessageFormat, DateTime.UtcNow.ToString(CultureInfo.InvariantCulture), level, message);
string timeStamp = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture);
string formattedMessage = string.Format(MessageFormatString, timeStamp, level, message);

// Print to console
Debug.WriteLine(formattedMessage);
Expand Down Expand Up @@ -105,21 +106,7 @@ private static async Task<bool> InitializeLogFileWriterBackgroundTaskAsync()
DateTime.UtcNow.ToString("yyyyMMddTHHmmss", CultureInfo.InvariantCulture) + ".log");
}

_backgroundTask = Task.Run(
async () =>
{
while (true)
{
Thread.Sleep(LoggingInterval);

// We will try to write all pending messages in our next attempt, if the current attempt failed
// However, if the size of messages has become abnormally big, we know something is wrong and should abort at this point
if (!await TryFlushMessageQueueAsync() && Messages.Count > 1000)
{
break;
}
}
});
_backgroundTask = Task.Run(WriteLogMessages);

_initialized = true;
LogInfo($"Log file location: {_logFile.Path}", true);
Expand All @@ -133,10 +120,24 @@ private static async Task<bool> InitializeLogFileWriterBackgroundTaskAsync()
{
SemaphoreSlim.Release();
}

return false;
}
0x7c13 marked this conversation as resolved.
Show resolved Hide resolved

private static async Task WriteLogMessages()
{
while (true)
{
Thread.Sleep(LoggingInterval);

// We will try to write all pending messages in our next attempt, if the current attempt failed
// However, if the size of messages has become abnormally big, we know something is wrong and should abort at this point
if (!await TryFlushMessageQueueAsync() && Messages.Count > 1000)
{
break;
}
}
}

private static async Task<bool> TryFlushMessageQueueAsync()
{
if (!_initialized)
Expand Down