Capturing log messages

I tried to capture the log output using Application.RegisterLogCallback but it did not work:

int logcalled ;

// Use this for initialization
void OnEnable() {
    UnityEngine.Application.RegisterLogCallback(new Application.LogCallback(MyCallback));
}

private void MyCallback(string condition, string stacktrace, UnityEngine.LogType type)
{
	logcalled++;
}

void OnGUI(){ 
	GUI.Label(new Rect(10,10,100,100), logcalled.ToString());

	if(GUI.Button(new Rect(100,100,100,100),"press"))
	{
		Debug.LogError("iada iada");
		Debug.Log("iada iada 2");
		object a = null;
		a.ToString();
	}
}

The above code dont work, the variable logcalled not incremented.

  • Tried Debug.Log, Debug.LogError and a even a null exception, neither was captured, but they where all logged to the editor console.
  • Tried to register the callback on "Start" with the same results.
  • Tried a Debug.Log on the "Update" method, generating dozens of log msg/second. In this case 'logcalled' was called 2 times at the very begining and that was it.
  • Tried using javascript instead of C#, didnt work either.

Am I missing something?

(question was edited to clarify it)

There seems to be a bug with this.

The only way I could get RegisterLogCallback to work with our in-game logging system was by calling RegisterLogCallback every frame. If I just called it at startup, it would only call the logs that were in that initial frame.

RegisterLogCallback should not redirect the log output - it should install an additional handler, so the log output goes to the editor log, and your function should be called. Are you seeing logcalled being incremented as log messages are printed? If so, then everything works as expected.

The JavaScript example in Unity's documentation also does not work (unless you use the delayed register workaround) . Has anyone logged a bug report?