One of the great advantages of programming in Unity is that it uses a (mostly) standard .NET implementation. This means you can find lots of third party code that is written for .NET but not necessarily Unity and still incorporate it into your app. This kind of code typically uses System.Console.Write or System.Console.WriteLine to print to standard output, but Unity doesn’t display it in its Console panel or redirect it to platform-specific logging like Android’s logcat. This article provides a class you can easily integrate into your app to redirect System.Console writes to Unity’s standard Debug logging so it’ll show up like you’d expect.

The key to redirecting System.Console writes is to use the System.Console.SetOut function. It takes a System.IO.TextWriter which has many Write and WriteLine functions for all kinds of types: int, string, double, etc.

Defining this TextWriter is most of the work. Thankfully, most of them redirect to just three Write functions:

public override void Write(string value)
public override void Write(char value)
public override void Write(char[] value, int index, int count)

Since writes might not be the entire line, a buffer must be kept to accumulate writes until a newline is written. System.Text.StringBuilder is great for that.

Here’s the final source for the UnitySystemConsoleRedirector:

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 == '\n')
					{
						Flush();
					}
				}
			}
		}
 
		public override void Write(char value)
		{
			buffer.Append(value);
			if (value == '\n')
			{
				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());
	}
}

Using it is trivial:

UnitySystemConsoleRedirector.Redirect();

Then use Console like normal in either your own code or the third party’s code, such as a DLL.

Here’s some code to test it out along with the output you’ll see in the Console panel of the Unity Editor:

UnitySystemConsoleRedirector.Redirect();
Console.WriteLine("str");
// output: str
 
Console.WriteLine('A');
// output: A
 
Console.Write('B');
Console.Write('C');
Console.Write('D');
Console.Write('\n');
// output: BCD
 
Console.Write(123);
Console.Write('\n');
// output: 123
 
Console.Write(3.14f);
Console.Write('\n');
// output: 3.14
 
Console.Write(2.2);
Console.Write('\n');
// output: 2.2
 
Console.Write(42.42m);
Console.Write('\n');
// output: 42.42
 
Console.Write(new char[]{'H','I','J'});
Console.Write('\n');
// output: HIJ
 
Console.Write(true);
Console.Write('\n');
// output: True
 
Console.Write(new object());
Console.Write('\n');
// output: System.Object
 
Console.Write("some int: {0}", 123);
Console.Write('\n');
// output: some int: 123
 
Console.Write(new char[]{'H','E','L','L','O'}, 1, 2);
Console.Write('\n');
// output: EL

It’s really quite easy to integrate and minimal extra code in the project, so you might as well drop this in any time some third-party code is using System.Console. You can also easily customize it if you’d rather redirect to a different logging system, file, or even network. Just replace the one Debug.Log line with your own logging function.

Got any other logging tips for Unity? Ways to make working with third party code easier? If you do, go ahead and post a comment!