Trace Console Output

One common behavior for Linux GUI applications when ran from console is to write log text directly there. It’s not really a mandatory feature but a lot of frameworks do it so it became de-facto standard. However, C# applications often don’t follow this principle. I will take Avalonia for example. While Avalonia itself will write log to console, user code will not.

If you are using some logging framework (e.g. Serilog), this is trivial to configure. But, what about simple applications? Can we do something there?

Well, you can always add a console listener.

#if DEBUG
  if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
    Trace.Listeners.Add(new ConsoleTraceListener());
  }
#endif

With this, any Trace.WriteLine will write its output to console in addition to other trace listeners.

Why only on Linux? Well, under Windows there is a nice DebugView utility that can capture this information without going through console listener.