C# not outputting to console

2019-08-27 21:06发布

问题:

I'm sure there's some simple answer, but none of the other Stack Overflow posts has helped me. My code will not log to the Console, and it's hard to do anything useful with that state of affairs.

using System;
using System.Diagnostics;

namespace Learning
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Debug.Log ("this?");
            Debug.Print ("How about this?");
            Console.WriteLine ("WORK");
            Console.ReadLine ();
        }
    }
}

I've been able to write to the console before, I don't know why it's being persnickety now.

回答1:

Probably because your code doesn't actually compile. Log() is a static method of Debugger, not Debug, and it takes three arguments: level, category, and message.

public static void Main (string[] args)
{
    System.Diagnostics.Debugger.Log(1, "category", "this?");
    System.Diagnostics.Debug.Print ("How about this?");
    Console.WriteLine ("WORK");
    Console.ReadLine ();
}

It's worth noting that Debug/Debugger methods will not do you any good unless you are Debugging. To start a debugging session in mono, go to the Run -> Debug



回答2:

You may want to check what kind of application you are using. For example, if you are making a Forms Application, you won't have access to the Console functions.

You can change this by going into the Solution Properties, and changing it from a Windows Forms Application to a Console Application. This won't have any effect on your program, other than it will run a Console alongside.