6

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.

jonsca
  • 10,218
  • 26
  • 54
  • 62
Nick3
  • 639
  • 1
  • 14
  • 40
  • Maybe you set console output (with Console.SetOut Method) to something that casues the exception? – Theraot Sep 11 '12 at 06:01
  • I get no exception, it just does not continue... And I dont use SetOutMethod. – Nick3 Sep 11 '12 at 06:03
  • Please, show us previous code. If you try to change `Console.WriteLine("HERE");` with `Debug.WriteLine("HERE");` do you see something? – Marco Sep 11 '12 at 06:03
  • Its alot of code, and this does not happen in one place in it. Everywhere I have Console.WriteLine its the same deal. If I remove it, it works like a charm... – Nick3 Sep 11 '12 at 06:04
  • The problem is obviously somewhere in your code, not in the snippet you provided in the question – horgh Sep 11 '12 at 06:13
  • 1
    What console are you talking about? Windows Forms application is not supposed to have a console – horgh Sep 11 '12 at 06:20
  • 1
    using cw for debug is silly. use system.diagnostics.debug – Nahum Sep 11 '12 at 06:20
  • @KonstantinVasilcov perhaps he means visual studio output window. – Nahum Sep 11 '12 at 06:21
  • @Nick3 If it *worked earlier*, maybe you just need to reboot VS or the PC? – horgh Sep 11 '12 at 06:33
  • Maybe you're able to rephrase the code snippet to something we (the community) can run and execute. This way, we don't know what `line(of.code);` and `other.line(of.code);` does and we also do not know how you're logging it with `logger.log(e.ToString());` - Which logging framework do you use? – Matt Dec 15 '22 at 07:27

2 Answers2

6

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}");
}
Matt
  • 25,467
  • 18
  • 120
  • 187
  • This works great, but still really wierd it worked earlier :P – Nick3 Sep 11 '12 at 06:38
  • Indeed ... Note that you can change the output by `Console.SetOut(sw);`, where `sw` is a StreamWriter. If you need the console output, ensure you are using a console app. Also, look for the `.RedirectStandardOutput` property in your process - check if it is set for the right output stream. – Matt Sep 12 '12 at 07:51
  • Once I did this, then it is already working also when I change it back. It happened to me again and again, every time it's being fixed by running it once with this Debog instead of Console. – Mayer Spitz Dec 14 '22 at 03:35
  • @MayerSpitz - Since the code snippet Nick3 provided isn't really executable, I asked him to provide an example we can execute (see the comments below the question). Then it is easier to get some clarity on this issue. If you can provide something, it might also be helpful. – Matt Dec 15 '22 at 07:30
  • @Matt I can share my repo with you if you want, I don't have much there, I do simple API requests to Google's Matrix API, nothing out of the ordinary, I don't remember having this issue in the past, simply at a certain point Console.WriteLine() didn't work, and not only it didn't work but stopped the program (not sure if really stopped or just didn't show any console, I tried putting a breakpoint and it didn't reach either - so the debugger didn't work either?), once I used Debug.WriteLine() it worked even after I switched back to Console.WriteLine(), it happened once again and I did the same. – Mayer Spitz Dec 19 '22 at 16:40
  • @MayerSpitz - That sounds like something happend to your Visual Studio installation. Try to open Visual Studio Installer, go to the installed instance, click on the "More" button and select "Repair". It will try to re-install all components of Visual Studio. Then reboot, and check if the issue remains or not. – Matt Jan 02 '23 at 07:47
3

I can think of a few scenarios that will cause Console.WriteLine to fail:

  1. The output stream has been set to an invalid object or something that will misbehave (in your case stop the program).

  2. You are running in an environment that doesn't give you permission to use the console*.

  3. It is not System.Console.WriteLine but another method with the same name that gets invoked thanks to some using directives.

*: I can't think of such environment, but may be some plug-in system?

If you are making a Windows Froms Application and not setting the output stream for your Console then Console.WriteLine should do nothing. So, if you need debugging follow Matt's recomendation.

Community
  • 1
  • 1
Theraot
  • 31,890
  • 5
  • 57
  • 86