how to programmatically clean up the console (log) in version 4.2.2?

how to programmatically clean up the console (log) in version 4.2.2?
before in version 4.1.5 cleaned so

public static void F_ClearLog()
    {
        //Debug.ClearDeveloperConsole();
        Assembly SampleAssembly = Assembly.GetAssembly(typeof(UnityEditorInternal.Macros));
        if (SampleAssembly == null)
        {
            Debug.LogError("!!! (SampleAssembly == null)");
        }
        else
        {
            Type type = SampleAssembly.GetType("UnityEditorInternal.LogEntries");
            if (type == null)
            {
                Debug.LogError("!!! (type == null)");
            }
            else
            {
                MethodInfo method = type.GetMethod("Clear");
                if (method == null)
                {
                    Debug.LogError("!!! (method == null)");
                }
                else
                {
                    method.Invoke(new object(), null);
                }
            }
        }
    }

Now there is an error on line
Assembly SampleAssembly = Assembly.GetAssembly(typeof(UnityEditorInternal.Macros));

agreed

public static void F_ClearLog()
{
//Debug.ClearDeveloperConsole();
//Assembly SampleAssembly = Assembly.GetAssembly(typeof(UnityEditorInternal.Macros));
Assembly SampleAssembly = Assembly.GetAssembly(typeof(UnityEditorInternal.ComponentUtility));
if (SampleAssembly == null)
{
Debug.LogError(“!!! (SampleAssembly == null)”);
}
else
{
Type type = SampleAssembly.GetType(“UnityEditorInternal.LogEntries”);
if (type == null)
{
Debug.LogError(“!!! (type == null)”);
}
else
{
MethodInfo method = type.GetMethod(“Clear”);
if (method == null)
{
Debug.LogError(“!!! (method == null)”);
}
else
{
method.Invoke(new object(), null);
}
}
}
}

Omfg finally something that works thnx vva vva !

I’m posting this here since I needed this badly due to not using multi-scene editing and having it where I had a loading screen scene that it needed to switch to if it didn’t find a main game object, ( (basically this makes it so that there are tons of red errors in my code of not found objects needed from the loading screen) so I just wanted to be able to clear it…I know this isn’t recommended but uh…complex story, but for those of you searching on google you’ll end up finding this topic… so here is the code for this. They changed the type name to UnityEditorInternal for Unity 5.5+:

        using UnityEditor;
        using System.Reflection;

        public static void Misc_ClearLogConsole() {
            Assembly assembly = Assembly.GetAssembly (typeof(SceneView));
            Type logEntries = assembly.GetType ("UnityEditorInternal.LogEntries");
            MethodInfo clearConsoleMethod = logEntries.GetMethod ("Clear");
            clearConsoleMethod.Invoke (new object (), null);
        }
1 Like

HI ! I know this is a very old post, but it still gets first in search result with this query “Unity clear console from code”.
The previous code no longer works.
I found a new (very similar) way on this website: https://www.codegrepper.com/code-examples/csharp/how+to+clear+console+through+script+unity

I’ll post the code in case the link dies some day.

using System.Reflection;
using UnityEditor;

public static void ClearLogConsole()
{
    Assembly assembly = Assembly.GetAssembly(typeof(Editor));
    Type logEntries = assembly.GetType("UnityEditor.LogEntries");
    MethodInfo clearConsoleMethod = logEntries.GetMethod("Clear");
    clearConsoleMethod.Invoke(new object(), null);
}

This code works as of Unity 2020.2.0b10

3 Likes