Search Unity

Feature Request Need to exclude a method from the console stack trace

Discussion in 'Editor & General Support' started by Seith, Feb 20, 2021.

  1. Seith

    Seith

    Joined:
    Nov 3, 2012
    Posts:
    755
    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:

    Code (csharp):
    1. [HideInStackTrace]
    2. public void MyCustomLogger(string _message)
    3.     {
    4.     ...
    5.     }
    (By the way, we do know about the callback to catch when something is logged (https://forum.unity.com/threads/excluding-a-method-frame-from-stack-trace-in-debug-log.259638/) 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)
     
    Neiist, 479813005, Eristen and 3 others like this.
  2. sinaari

    sinaari

    Joined:
    Jan 28, 2019
    Posts:
    47
    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...912.2118931545.1640108494-59164945.1639945624
     
    Monsoonexe likes this.
  3. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    374
    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:
    upload_2022-1-1_23-0-48.png

    EDIT: I also found it on their Engineering roadmap too.
     
    jolix, RARgames and david910 like this.
  4. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    476
  5. claudiaantoun

    claudiaantoun

    Unity Technologies

    Joined:
    May 14, 2021
    Posts:
    9
  6. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    374
    Ooh, thanks for sharing!
    This is very helpful :D 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 ;)
     
  7. redcurrantsjam

    redcurrantsjam

    Joined:
    Nov 21, 2022
    Posts:
    4
    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].
     
    cmdexecutor likes this.
  8. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    476
    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).
     
  9. cmdexecutor

    cmdexecutor

    Joined:
    Sep 30, 2014
    Posts:
    21
    Xarbrough and Ghat-Smith like this.
  10. katenelson092

    katenelson092

    Joined:
    Jan 28, 2023
    Posts:
    1
    Good one, but can it be added to LTS versions? The same question like the guy above.
     
    Xarbrough likes this.
  11. Racso_JC

    Racso_JC

    Joined:
    May 18, 2019
    Posts:
    3
    Same question. It would be great to have this in 2021 LTS.
     
    rusildo and unity_awUpacKcgHt1UA like this.
  12. JTAU

    JTAU

    Joined:
    May 12, 2019
    Posts:
    24
    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.
     
    ModLunar likes this.
  13. MoonMoritz

    MoonMoritz

    Joined:
    Jan 30, 2022
    Posts:
    18
    That attribute doesn't work for me, if implemented exactly as the example given in the APi doc. What gives? :)

    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. :(

    (2022.2.18f1)
     
    Last edited: May 13, 2023
    jniac, marek_m and KyleOlsen like this.
  14. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    476
    Yup, broken for me as well (2022.2.16f1)
     
  15. DF3Boris

    DF3Boris

    Joined:
    May 2, 2023
    Posts:
    2
    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.
     
  16. donkey0t

    donkey0t

    Joined:
    Oct 23, 2016
    Posts:
    71
    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??
     
  17. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,859
    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.
     
    donkey0t likes this.
  18. donkey0t

    donkey0t

    Joined:
    Oct 23, 2016
    Posts:
    71
    Maybe just a docs slip up, thanks for confirming.
     
  19. bkuker

    bkuker

    Joined:
    Feb 20, 2023
    Posts:
    1
    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!
     
    d1ke likes this.
  20. d1ke

    d1ke

    Joined:
    Apr 1, 2014
    Posts:
    5
    Booo! I also want double click on log entry ignore method with [HideInCallstack].
     
  21. Mark-Currie

    Mark-Currie

    Joined:
    Sep 6, 2012
    Posts:
    54
    My replacement console has something like this. You exclude wrapper methods by right-clicking on them (in the callstack) and marking them as wrappers. Then when you double-click the main entry, it takes you to the correct place.
     
    Last edited: Feb 21, 2024
    ModLunar, Whatever560 and CodeRonnie like this.
  22. Whatever560

    Whatever560

    Joined:
    Jan 5, 2016
    Posts:
    515
    Just tried it owning the asset for sometimes now, it works perfectly and its non intrusive ... best solution IMO.
     
  23. ThomLaurent

    ThomLaurent

    Joined:
    Sep 27, 2014
    Posts:
    10
    It doesn't even work for exception stack traces :/
    I use an extension method for preserving stack trace when doing an exception rethrow (
    ExceptionDispatchInfo.Capture(e.InnerException ?? e).Throw();
    ) which I don't want to show in the callstack.
     
  24. elhongo

    elhongo

    Joined:
    Aug 13, 2015
    Posts:
    47
    would love to get the method ignored when double clicking.
    We also have some custom logging methods and opening these when double clicking in the Editor is just painful.
    We need to navigate to the root custom log call
     
  25. zenbin3d

    zenbin3d

    Joined:
    Jul 7, 2023
    Posts:
    25
    It's a great start but 50% of it's purpose is missing making it useless as a whole.
    When you double click the log entry it should also respect the modified stacktrace. Now, you get stripped stacktrace but double-clicking the log entry it takes you to the hidden method call.
    Is it a bug or by design?
     
    Zyke and Ghat-Smith like this.
  26. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,511
    I'm also looking for a solution for this. I want to redirect a console click to different location away from a wrapper method. :(