Skip to content

Commit ebdb045

Browse files
authored
#50575 colorbehavior default should disable colors in android/applemobile (#74496)
* #50575 colorbehavior default should disable colors in android/applemobile
1 parent 442c137 commit ebdb045

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/libraries/Microsoft.Extensions.Logging.Console/src/SimpleConsoleFormatter.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO;
77
using Microsoft.Extensions.Logging.Abstractions;
88
using Microsoft.Extensions.Options;
9+
using System.Runtime.InteropServices;
910

1011
namespace Microsoft.Extensions.Logging.Console
1112
{
@@ -14,6 +15,10 @@ internal sealed class SimpleConsoleFormatter : ConsoleFormatter, IDisposable
1415
private const string LoglevelPadding = ": ";
1516
private static readonly string _messagePadding = new string(' ', GetLogLevelString(LogLevel.Information).Length + LoglevelPadding.Length);
1617
private static readonly string _newLineWithMessagePadding = Environment.NewLine + _messagePadding;
18+
private static readonly bool _isAndroidOrAppleMobile = RuntimeInformation.IsOSPlatform(OSPlatform.Create("ANDROID"))
19+
|| RuntimeInformation.IsOSPlatform(OSPlatform.Create("TVOS"))
20+
|| RuntimeInformation.IsOSPlatform(OSPlatform.Create("IOS"))
21+
|| RuntimeInformation.IsOSPlatform(OSPlatform.Create("MACCATALYST"));
1722
private IDisposable? _optionsReloadToken;
1823

1924
public SimpleConsoleFormatter(IOptionsMonitor<SimpleConsoleFormatterOptions> options)
@@ -157,8 +162,10 @@ private static string GetLogLevelString(LogLevel logLevel)
157162

158163
private ConsoleColors GetLogLevelConsoleColors(LogLevel logLevel)
159164
{
165+
// We shouldn't be outputting color codes for Android/Apple mobile platforms,
166+
// they have no shell (adb shell is not meant for running apps) and all the output gets redirected to some log file.
160167
bool disableColors = (FormatterOptions.ColorBehavior == LoggerColorBehavior.Disabled) ||
161-
(FormatterOptions.ColorBehavior == LoggerColorBehavior.Default && !ConsoleUtils.EmitAnsiColorCodes);
168+
(FormatterOptions.ColorBehavior == LoggerColorBehavior.Default && (!ConsoleUtils.EmitAnsiColorCodes || _isAndroidOrAppleMobile));
162169
if (disableColors)
163170
{
164171
return new ConsoleColors(null, null);

src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/SimpleConsoleFormatterTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ public class SimpleConsoleFormatterTests : ConsoleFormatterTests
1313
[InlineData(LoggerColorBehavior.Default)]
1414
[InlineData(LoggerColorBehavior.Enabled)]
1515
[InlineData(LoggerColorBehavior.Disabled)]
16-
[ActiveIssue("https://github.com/dotnet/runtime/issues/50575", TestPlatforms.Android)]
17-
[ActiveIssue("https://github.com/dotnet/runtime/issues/51398", TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst)]
1816
[ActiveIssue("https://github.com/dotnet/runtime/issues/73436", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
1917
public void Log_WritingScopes_LogsWithCorrectColorsWhenColorEnabled(LoggerColorBehavior colorBehavior)
2018
{

0 commit comments

Comments
 (0)