How do I write to the console?

I'm writing some C# scripts for a Windows Standalone application in Unity. I'd like to print some debug messages to the Unity console, but I can't figure out how. I've read that I should be able to do this using `print()`, `Debug.Log`, or `System.Console.WriteLine()`, but the first two just output to the log file, not the console, and the last doesn't appear to do anything at all.

I'm new to both Unity and C#, so don't hesitate to mention potential solutions that should be obvious enough for me to already have tried them :)

For the consolation prize...

As a workaround, I've been using BareTail to watch the log file, but it kinda sucks because Unity outputs the whole stack trace every time I `print()`. Any way for me to suppress that?

1 Like

print is an alias for Debug.Log, which does appear in the Unity console. There's also Debug.LogWarning and Debug.LogError, which (strangely enough) print lines labeled as warning and error messages. The latter is particularly useful if you have Error Pause selected in the console.

It seems that the key piece of information I was missing was that Debug.Log writes to the console only when you run the game from within unity by pressing the "play" button above the game view.

I was running my game by pressing Ctrl-B, or pressing "Build & Run" in the Build Settings window. In that case, it will only output to the log file.

It seems that the debug.log and console.writeline is routed who knows where.

There is a little snippet to reroute console.writeline it to the real Unity console. I tried it it does work:

using System;
using System.IO;
using System.Text;
 
using UnityEngine;
 
/// <summary>
/// Redirects writes to System.Console to Unity3D's Debug.Log.
/// </summary>
/// <author>
/// Jackson Dunstan, http://jacksondunstan.com/articles/2986
/// </author>
public static class UnitySystemConsoleRedirector
{
	private class UnityTextWriter : TextWriter
	{
		private StringBuilder buffer = new StringBuilder();
 
		public override void Flush()
		{
			Debug.Log(buffer.ToString());
			buffer.Length = 0;
		}
 
		public override void Write(string value)
		{
			buffer.Append(value);
			if (value != null)
			{
				var len = value.Length;
				if (len > 0)
				{
					var lastChar = value [len - 1];
					if (lastChar == '

')
{
Flush();
}
}
}
}

		public override void Write(char value)
		{
			buffer.Append(value);
			if (value == '

')
{
Flush();
}
}

		public override void Write(char[] value, int index, int count)
		{
			Write(new string (value, index, count));
		}
 
		public override Encoding Encoding
		{
			get { return Encoding.Default; }
		}
	}
 
	public static void Redirect()
	{
		Console.SetOut(new UnityTextWriter());
	}
}

To use it put the sample in assets and write
UnitySystemConsoleRedirector.Redirect();
in your c# file

This video explains it well: How To Print To The Unity Console

You can use Workaround when you use the System.Console.WriteLine()
In reality the Unity editor save it to it log file. but doesn’t show it in the standard Unity Debug.Console
Common place of this log is C:\Users\AppData\Local\Unity\Editor (Editor.log). You can watch it by every text Editor (more helpful Notepad++, because it can automatically update the showing content).
Unity Editor also send to this file the all output of its Debug.Console (in summary its look little weird but for short checking is normal)