Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

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)
     
  2. sinaari

    sinaari

    Joined:
    Jan 28, 2019
    Posts:
    32
    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
     
  3. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    327
    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:
    473
    ModLunar and Ghat-Smith like this.
  5. claudiaantoun

    claudiaantoun

    Unity Technologies

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

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    327
    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:
    473
    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:
    14
    Ghat-Smith likes this.
  10. katenelson092

    katenelson092

    Joined:
    Today
    Posts:
    1
    Good one, but can it be added to LTS versions? The same question like the guy above.