Search code examples
c#xamarinloggingconsoleilogger

Strange symbols in console when using ILogger in Xamarin


In a Xamarin application, I'm using ILogger from the Microsoft.Extensions.Logging package, like this:

public partial class App : Application {
    public App() {
        // Dependency injection
        var serviceCollection = new ServiceCollection();
        serviceCollection.AddLogging(builder => builder.AddConsole());

and somewhere else (Logger is provided via dependency injection), simply

Logger.LogDebug("OK");

When I run my code, I see all kinds of strange symbols like \^[[40m in my Visual Studio for Mac console (highlighted):

2022-05-16 14:41:32.338270+0200 Example.iOS[33578:809127] \^[[40m\^[[37mdbug\^[[39m\^[[22m\^[[49m: Example.Communication.AuthenticationClient[0]
      OK

What are they, and how do I remove them?


Solution

  • These symbols are used to color up your console, but Visual Studio doesn't support them (at least on macOS).

    It's easy to disable them via the DisableColors property of the console logger options, like this:

    serviceCollection.AddLogging(builder => builder.AddConsole(options => options.DisableColors = true));
    

    (in later .NET versions, this property has been superseded by ColorBehavior)