2

I wish to be able to display log messages for e.g. Trace level in single console line instead of stacking them one on top of another.

One Trace log message should be displayed in console and then replaced by another Trace log message on the same line. When another log message came in e.g. Warn, message stacking should continue as usual.

Is there a way to define such single line overwrite for Console logging in NLog?

2 Answers2

2

This appears to work if you wrap NLog

public class Logger : NLog.Logger
{
    public void TraceStackConsole(string message)
    {
        this.Trace(message);
        Console.SetCursorPosition(0, Console.CursorTop-1);
    }
}

You also might be able to get some interesting results with https://github.com/NLog/NLog/wiki/Replace-NewLines-Layout-Renderer although i haven't had success yet. It might work to replace it with "\r"

How can I update the current line in a C# Windows Console App? "If you print only "\r" to the console the cursor goes back to the begining of the current line and then you can rewrite it. "

Community
  • 1
  • 1
Alex Bezek
  • 467
  • 2
  • 7
1

Thank you very much @alexbezek it's just what I wanted! :)

Although I decide it's more convenient for me to make extension method for logger:

public static class LoggerExtension
{
    public static void ConsoleStack(this Logger logger, string message)
    {
        logger.Trace(message);
        Console.SetCursorPosition(0, Console.CursorTop-1);
    }
}

So then I'm able to call it like this: Logger.ConsoleStack("Log message");