Need to exclude a method from the console stack trace

We wrote our own custom logger which is a wrapper for Debug.Log() and we currently have to use it in a .dll form so that the wrapper itself does not appear in the console stack trace. This is so that when we double-click on a console log we are taken directly to the proper script/line instead of landing in the logger code itself.

Which is kind of a hack.

What we would need instead is a custom method attribute called (for example) “HideInStackTrace” in order to simply exclude a method from the console stack trace.

Needless to say, double-clicking on the log itself would send the user to the script/line that called the method (not the “hidden” method itself).

Something like this:

[HideInStackTrace]
public void MyCustomLogger(string _message)
    {
    ...
    }

(By the way, we do know about the callback to catch when something is logged ( Excluding a method frame from stack trace in Debug.Log()? ) but that doesn’t give us what we need as this only gets us the log content but has no effects on what the console actually shows)

6 Likes

I was looking for something like this and it turned out this feature was already present in the asset I’ve been using for 2+ years already xD. It’s the Editor Console Pro asset: https://assetstore.unity.com/packages/tools/utilities/editor-console-pro-11889. Just checked, and the feature works!

Clearly, it only works for the editor though, so if you want something like that in custom log files then you’d need to register a custom callback for Application.logMessageReceived, but I’ve never tried that myself.

EDIT: also see https://answers.unity.com/questions/1655575/wrapping-debugassert-and-skip-wrap-method-in-the-s.html?_ga=2.206568912.2118931545.1640108494-59164945.1639945624

1 Like

This would be really helpful for me, because I use/make C# libraries that might have a dependency on NLog for example.
If I want the library to work in my Unity project, I can write a custom target to output to the UnityEngine.Debug.LogXXX methods, but it has all these NLog methods in the stacktrace that I don’t care about.

However, there might be hope coming soon for this feature in a year or two?
I found this on Unity’s Editor roadmap, under the “Under Consideration” section at the time of writing today:
7774728--980424--upload_2022-1-1_23-0-48.png

EDIT: I also found it on their Engineering roadmap too.

3 Likes

[StackTraceHidden] was made public in .NET 6 (actually this existed before, but internal), so when Unity upgrade to it (which could take a while), it should come for free…

3 Likes

Hello! :slight_smile:

We added a new attribute, HideInCallstackAttribute, that you can use to exclude a method from the console stack trace. Here is the documentation for that attribute: Unity - Scripting API: HideInCallstackAttribute

Hope it helps!

14 Likes

Ooh, thanks for sharing!
This is very helpful :smile: and the docs page is great with the explanation and code example.

The only thing is I wish it could take in a bool to also apply its effect to any methods called recursively underneath the method that I mark as [HideInCallstack].

For example, if I’m calling into some logging class I made, which calls NLog, I can apply [HideInCallstack] to my logging method, but I’ll still see all the details of NLog’s method calls that I don’t want to see. Maybe a bool recursive parameter or something, where I could write [HideInCallstack(recursive: true)] would solve this :wink:

The only thing is that I wish it could accept a bool so that it would also affect any recursively called methods below the method I mark as [HideInCallstack].
backrooms game

1 Like

That’s cool, although I wish you’d used the standard one instead. Such attributes can usually be redeclared without conflict (like we used to inject ExtensionMethodAttribute in old .NET versions).

Great stuff, can it be included into LTS versions?

2 Likes

Good one, but can it be added to LTS versions? The same question like the guy above.
backrooms

1 Like

Same question. It would be great to have this in 2021 LTS.

2 Likes

This is handy, but is there a way to have this also so when you double click on the entry in the console, it doesn’t take you to that method if it had [HideInCallstackAttribute]. We are usually using this for debugging with custom print methods, so its not useful for the double click in the console to open up the Hidden print method.

2 Likes

That attribute doesn’t work for me, if implemented exactly as the example given in the APi doc. What gives? :slight_smile:

Neither the callstack is filtered, nor does double-click take me to the highest expected point in the callstack, it takes me to the very debug log line. :frowning:

(2022.2.18f1)

4 Likes

Yup, broken for me as well (2022.2.16f1)

Don’t forget to enable call stack stripping in the console options. Seems to work as of 2022.2.21.

For usability this feature should absolutely bring you to the correct line in editor when double-clicking. Seems to me than everyone looking for this feature is being a bit deceived.

4 Likes

I’m puzzled, I have 2021.3.21f but no sign of [HideInCallstackAttribute], the API docs says it should exist but nothing on the UnityEngine namespace??

I highly suspect it was added only very recently to the 2021.3 LTS version, as I remember the docs showing it was 2022+ only for a while.

1 Like

Maybe just a docs slip up, thanks for confirming.

It does not help at all.

Once I set the Strip Logging Callstack option in the Console menu it no longer APPEARS in the text.

But double clicking the log entry still takes me to the function annotated as hidden!

1 Like

Booo! I also want double click on log entry ignore method with [HideInCallstack].

1 Like