Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit 64724ea

Browse files
authored
Avoid calling ResetColor when colors are disabled (#930)
1 parent 8bc28d3 commit 64724ea

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/Microsoft.Extensions.Logging.Console/Internal/WindowsLogConsole.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Microsoft.Extensions.Logging.Console.Internal
77
{
88
public class WindowsLogConsole : IConsole
99
{
10-
private void SetColor(ConsoleColor? background, ConsoleColor? foreground)
10+
private bool SetColor(ConsoleColor? background, ConsoleColor? foreground)
1111
{
1212
if (background.HasValue)
1313
{
@@ -18,6 +18,8 @@ private void SetColor(ConsoleColor? background, ConsoleColor? foreground)
1818
{
1919
System.Console.ForegroundColor = foreground.Value;
2020
}
21+
22+
return background.HasValue || foreground.HasValue;
2123
}
2224

2325
private void ResetColor()
@@ -27,16 +29,22 @@ private void ResetColor()
2729

2830
public void Write(string message, ConsoleColor? background, ConsoleColor? foreground)
2931
{
30-
SetColor(background, foreground);
32+
var colorChanged = SetColor(background, foreground);
3133
System.Console.Out.Write(message);
32-
ResetColor();
34+
if (colorChanged)
35+
{
36+
ResetColor();
37+
}
3338
}
3439

3540
public void WriteLine(string message, ConsoleColor? background, ConsoleColor? foreground)
3641
{
37-
SetColor(background, foreground);
42+
var colorChanged = SetColor(background, foreground);
3843
System.Console.Out.WriteLine(message);
39-
ResetColor();
44+
if (colorChanged)
45+
{
46+
ResetColor();
47+
}
4048
}
4149

4250
public void Flush()

0 commit comments

Comments
 (0)