Skip to content

Commit

Permalink
Fix Stdout tearing in debug console (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkbennett authored Nov 7, 2023
1 parent 16e9ee1 commit 9ead3b9
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/Logging/listeners/StdoutListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public class StdoutListener : ListenerBase
private static readonly ConsoleColor CElapsedColor = ConsoleColor.Green;
private static readonly ConsoleColor CSourceColor = ConsoleColor.Cyan;

// Static lock object so different instances of the Stdout listener do not simultaneously write
// to stdout and have interleaved tearing of messages.
private static readonly object _stdoutLock = new ();

public StdoutListener(string name)
: base(name)
{
Expand Down Expand Up @@ -65,9 +69,19 @@ private void ConsoleHandleLogEvent(LogEvent evt, bool newline, TimeSpan elapsed)
line.Add(Tuple.Create(CDefaultColor, Environment.NewLine));
}

WriteColor(line);
Console.ResetColor();
Console.Out.Flush();
// Do this in a static lock to prevent tearing.
lock (_stdoutLock)
{
try
{
WriteColor(line);
Console.ResetColor();
Console.Out.Flush();
}
catch
{
}
}
}

private bool MeetsFilter(LogEvent evt)
Expand Down

0 comments on commit 9ead3b9

Please sign in to comment.