How do you print or output text in Monogame?
I googled how to display text in monogame and was led to this:
Debug.WriteLine
Which says: "By default, the output is written to an instance of DefaultTraceListener."(and that page just confused me more).
So, if someone could direct me to a method of displaying DefaultTraceListener, or another method of outputting text in monogame, I would appreciate it.
I found it!
Using Debug.WriteLine writes to the debugger, which is in the output window in Visual Studio(by default at the bottom). It appears when you close the program(press F5 to start, Esc to close) by default in an OpenGL project.
If you like, you can use Console.WriteLine like you would in a normal C# console application, assuming you're developing a desktop application. There are a couple of steps.
Open the Properties for your MonoGame project
Select the Application tab
Change the Output Type to Console Application.
Your application should run as normal, only a console window should appear when you start the game.
Alternatively, you can use Debug.WriteLine, which will write to the output window in Visual Studio (it should appear when you start debugging your game).
If you use the standard Debug.WriteLine or Trace.WriteLine, then output goes to the default trace listener which can be viewed in the Visual Studio output window. Outside of Visual Studio, you can use programs such as DebugView (SysInternals) or LogFusion (Binary Fortress) to display the output. DebugView even has a feature for viewing debug output from a remote machine.
There are other trace listeners that can send output to a file, or to the Windows event log, or you can write your own trace listeners fairly easily.
You could also consider using a ready-made logging framework such as NLog, which would give you a great deal of flexibility. I have found in practice that using NLog turns out to be a lot easier than the built in stuff in .NET, because of the way it lets you easily reconfigure things and control/filter the output in a much more flexible way.
I know this has been answered, but if anyone else stumbles upon this, you can also use Console.Write(thing in here); or Console.WriteLine(thing in here); to write to the console window. WriteLine adds a line ending and Write does not.
Related
Have a look at the picture. No matter where i put console.writeline, nothing is showing in output. Where could be a mistake? Is something wrong with my output editor or what?
Use Debug.WriteLine to write to the Output window as you want.
See also What's the difference between Console.WriteLine() vs Debug.WriteLine()?
So you have 2 questions instead of one: your main one and the comment "How can i display console window instead of Output window?"
You can write to VS output by using Debug.WriteLine. This requires System.Diagnostics namespace.
You can also run console window alongside with your windows application. Right click the project in Solution Explorer, select Properties, change Output Type to "Console Application" like this.
I am sure there is an answer to this somewhere, alas I am too much of a Noob to know exactly what to ask. I have been studying Computer Science since January of this year, and coming from Python to C# has caused me to think of everything in "pythonic" terms. I am taking a class on Coursera.org (Colorado state class by Tim A.) called "Beginning Game Programming with C#" I am using XNA game studio and so there is no Console.WriteLine available.
From my extremely limited working knowledge (<= 6 months Python, ~1.5 months C#), is there a way to write out values akin to a Console.WriteLine, when you are not working on a console application? How do you "dynamically" print values in C#? Is there even a way. In Python you can simply type "Print X" in the interpreter or in the editor and it will print the value. Is there an equivalent in C#? Now I know of Console.WriteLine() and Messagebox.Show() but I am working in XNA game studio and I cannot use them. Console.WriteLine doesn't seem to do anything, and MBox doesn't even let me use System.Windows namespace. Is there a way to get a copy of the Console.WriteLine even though it didn't show up in my game window?
There is windows in Visual Studio that like "immediate window" and "Watch" that I have never seen used. Is there a way to write out a value to one of these windows during runtime so that I can see or use it? This is just a guess.
use
System.Diagnostics.Trace.WriteLine("I'm adding the cost of this answer to your tab.");
The System.Diagnostics.Debug class provides a set of methods and properties that help debug code. The System.Diagnostics.Debug.Write and System.Diagnostics.Debug.WriteLine methods can be used to send text to the debug output window. The debug output window can be displayed by selecting View - Other Windows - Output in the Visual Studio .NET menu. The debug output can only be viewed in this window if the debugger is attached to the process that is writing to the output window. source: http://msdn.microsoft.com/en-us/library/ms698739%28v=vs.85%29.aspx
I am trying to self teach myself C# and wondering if anyone can help me with what seems to be a basic C# question. I created a C# file with this code, started debugging but don’t see “Hello World” anywhere.
using System;
class Hello
{
static void Main() {
Console.WriteLine("hello, world");
}
}
http://msdn.microsoft.com/en-us/library/aa664628(v=vs.71).aspx
So I guess my question is this. Where should I expect to see “Hello World”? I have checked the Console and the Browser. Is there some setup that needs to be done to properly debug C# files. I am probably missing the big picture as to how C# works. I am use to PHP where I can just do something like this...
<?php
include 'my file';
echo 'my file included';
?>
Any help would be much appreciated. Thanks.
EDIT:
Thanks everybody for all of the help. You have all helped me understand and realize a number of things about C# / .NET. After extensive troubleshooting it is evident that the problem is not a mater of the debugging working, but the fact that my C# file doesn't appear to be properly hooked/included (not sure what its called in .NET terms) to the rest of the project. Anyways I am accepting keyboardP's answer as he answered first and technically gave me all the right answers. Also thanks to dasblinkenlight who was also extra helpful.
Additional Solution:
After insight from SO users. This article helped point me in the right direction.
http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3
I'm guessing it's because the command prompt window is immediately closing. You can avoid this by adding Console.ReadLine(); after your WriteLine statement. This will wait for you to press return before closing the prompt window.
Alternatively, assuming you're using Visual Studio, you can run the build without a debugger attached by pressing CTRL + F5.
Edit - Based on the extra information added that you're using ASP.NET and not a console application.
Firstly, what are you trying to achieve? If you want to output debug information, then you can Debug.WriteLine instead of Console.WriteLine
System.Diagnostics.Debug.WriteLine("Hello World");
This will output the text to the "Output" window at the bottom of Visual Studio (by default).
Edit 2 Since you just want to write random text to the page, you can use
HttpContext.Current.Response.Write("Hello World");
There are sometimes issues with Response.Write but it should be okay for what you want to do here.
Use breakpoints. Set a breakpoint at the end of your method by clicking in the "gutter" area. A red circle will appear that looks like this:
Now run your program in debug mode by clicking the button with the green triangle or pressing F5. The program will run, producing the output in the console (a separate window). Once it hits your breakpoint, you can examine the console for the output, like this:
You are reading a tutorial for Console Application, however you are trying to create a ASP.NET application. I would reccomend reading a tutorial for ASP.NET
Like many before said: it goes by too fast, so either use breakpoints, or use a Read...
You can also write to your Visual Studio output window with System.Diagnostics.Debug.Write
you need to put break point over the line which you want to debug
short cut to placing break point is ctrl D,n
then you can step over or step into the code with f10 and f11 function keys
I am going to do some code in c# using visual studio 2010. The problem is that every time I run program it does appear in command prompt.
I would like place the output of the program in the console like in the Eclipse. System.Diagnostics.Debug.Write("abc"); this does not work cause if I look at the output I see much more information than I want. I would like to see only data which would show in the command prompt.
Check out "Tools->Options->Debug->Output Window" - you should be able to decrease amount of traces to your liking.
Side note: normally "output of program" means results of Console.WriteXXXX methods, so maybe you are looking for something completely different.
I think you wish to turn off verbose Output logging so that only your Debug.Write's will be written to the output window.
To do this you goto Tools > Options > Debugging > Output Window > and turn off the verbose logging:
If you want to keep all of your output, you can redirect your Debug.Print to output to the Immediate Window instead.
Tools -> Options -> Debugging -> General | Click on Redirect all Output Window text to the Immediate Window.
This does not mean everything will be outputted to it, just the output that you designate in your code and any exception messages that get thrown. It's a lot less chatty and still keeps your Output window with all the standard messages.
I am developing some projects in C# and Silverlight.
I am trying to print lines of code in order to debug, but Console.Write() doesn't seem to work.
I've created a Silverlight Application, not a Console Application where Console.Write() did work. How can I print in a console or in the output window in a Silverlight Application project?
Use System.Diagnostics.Debug.Write to print in the debug output window.
See http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.write.aspx
Using System.Diagnostics.Debug.WriteLine(); like Fox32 and VoodooChild mentioned did not work for me initially. Apparently my debugging wasn't working at all (breakpoints did not hit for example).
I was able to fix the debugging by using IE instead of FireFox (see Debugging Silverlight not hitting breakpoints)
As soon as that was fixed, the System.Diagnostics.Debug.WriteLine(); worked perfectly :)