Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Increasing Console Log Size

Tags:

c#

i have a basic C# Console application, it retrives logs from a website, but after like 100 lines, the old logs are deleted and the user cant scroll up and see them anymore, is there any way to increase the size it will save?

like image 406
user1769042 Avatar asked May 03 '26 16:05

user1769042


1 Answers

Console.SetBufferSize() buys you a bigger buffer. There are a few complications however. There's an upper limit on the maximum size and there's a bug in the argument validation code in the SetBufferSize() method. It reports that Int16.MaxValue is the largest number of lines you can ask for. That's an off-by-one bug. On Windows 7 you can use:

static void Main(string[] args) {
    Console.SetBufferSize(Console.BufferWidth, 32766);
    // etc..
}

On older operating systems (like XP) there a much tighter limit, the buffer size used to be restricted to 65536 bytes. I don't have them around anymore to check. The possibly valid code on those is:

    static void Main(string[] args) {
        var lines = 65536 / 2 / Console.BufferWidth;
        Console.SetBufferSize(Console.BufferWidth, lines);
        // etc..
    }
like image 83
Hans Passant Avatar answered May 06 '26 05:05

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!