Search code examples
c#console

Console.Writeline stops my program... C#


I have just got the wierdest problem in my Windows Froms C# application

I got a few Console.WriteLine in my code for debug, but suddenly this stopped working. For example

try{
   line(of.code);
   Console.WriteLine("HERE");
   other.line(of.code);
}
catch (Exception e)
{
   logger.logg(e.ToString());
}

I will not get to the other.line(of.code); line, and I do not get the "HERE" in the console.

There is a few places in the code, the same happens on all of them. It just stops, it does NOT get to the catch...

And the worst part, it worked earlier. I have worked on applications for a long time, and have never seen anything like it.


Solution

  • If you need it just for debugging, try

    System.Diagnostics.Debug.WriteLine("HERE");
    

    instead.

    This will write the output into the output window of your development environment and, more important, it will work regardless of the type of application you are developing (console app, win forms, web app etc).

    As soon as you change to "release", the debug information will be ignored and not compiled into the code. If you would require it there, too, you should try Trace.Writeline instead.

    Note: You can make your life easier if you declare Systems.Diagnostics as follows:

    using System.Diagnostics; // at the top of your code
                              
    void Main()
    {
        // Then you can say:
        Debug.WriteLine("Some message 1");
        Debug.WriteLine("Some message 2");
    }
    

    You can even do a conditional debug print like so:

    for (int i = 0; i < 100; i++)
    {
        Debug.WriteLineIf(i>90, $"i is greater than 90: {i}");
    }