serilog-sinks-console icon indicating copy to clipboard operation
serilog-sinks-console copied to clipboard

How to make a colored tex into the console called by AllocConsole method?

Open Hiprox opened this issue 1 year ago • 4 comments

I want to make "developer console" called in WinForm application by pressing hotkeys. The console is opened by calling AllocConsole, but the problem is that the text is displayed only in white color. Is there any way to solve this problem? I assume that I need to write something else in the code.

Hiprox avatar Feb 09 '24 05:02 Hiprox

Exactly the same question. No any reaction on app.config settings.

ZedZipDev avatar Feb 04 '25 11:02 ZedZipDev

Exactly the same question. No any reaction on app.config settings.

I have not found a solution. I'm happy with what I have and I'm already used to it.

Hiprox avatar Feb 08 '25 18:02 Hiprox

@Hiprox , @ZedZipDev What about enabling the virtual terminal processing. Something like this.

            AllocConsole()
            auto hOut = GetStdHandle(STD_OUTPUT_HANDLE);
            auto hIn = GetStdHandle(STD_INPUT_HANDLE);
            auto hRealOut = CreateFile(L"CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE,0, CREATE_NEW, 0, 0);
            auto hRealIn = CreateFile(L"CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_NEW, 0, 0);
            if (hRealOut != hOut)
            {
                SetStdHandle(STD_OUTPUT_HANDLE, hRealOut);
            }
            if (hRealIn != hIn)
            {
                SetStdHandle(STD_INPUT_HANDLE, hRealIn);
            }

            if (GetConsoleMode(hOut, &dConsoleMode))
            {
                dConsoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
                SetConsoleMode(hOut, dConsoleMode);
            }

j2inet avatar Mar 27 '25 02:03 j2inet

You only need to create logger after using AllocConsole() for colors to work correctly. Virtual terminal support should be enabled by ConsoleSink:

https://github.com/serilog/serilog-sinks-console/blob/dev/src/Serilog.Sinks.Console/Sinks/SystemConsole/ConsoleSink.cs#L37

Dzhake avatar May 01 '25 12:05 Dzhake