6

I have a dead simple one-liner method that I don't want to see in the stack trace: is that possible? maybe mark it with an attribute?

Just to clarify, I'm not trying to print the trace, or rethrow, or have auto step-through in the debugger. I'd like the method to not show up in the trace in the first place, because the trace is then handled by some third-party code. I want control at runtime, I'm not interested about debugging. I'm saying this because most of what I've read on StackTrace seem to be about these topics.

benblo
  • 877
  • 8
  • 18
  • If it is a simple one-liner then it is automatic. The JIT optimizer will inline it. But not while you've got the debugger attached. – Hans Passant Apr 21 '10 at 13:08
  • Hans, see my comment to Steven's answer. – benblo Apr 22 '10 at 11:14
  • Don't forget to flag your favorite answer ;-) – Steven Jun 22 '10 at 11:27
  • 1
    Does this answer your question? [How to hide the current method from exception stack trace in .NET?](https://stackoverflow.com/questions/2973343/how-to-hide-the-current-method-from-exception-stack-trace-in-net) – MuiBienCarlota Dec 04 '19 at 14:02
  • @MuiBienCarlota you mean your comment at the bottom (https://stackoverflow.com/a/59177766/322119) regarding [StackTraceHidden]? Yes, looks like exactly what I want!... when it's made public. I wonder if we could hack around the publicness by redeclaring the attribute ourselves, like the good old ExtensionAttribute hack (https://www.simplethread.com/using-extension-methods-in-net-20/)... – benblo Dec 05 '19 at 08:32
  • @benblo I thought it had to be possible (it seems to be included in Rx.Net - https://github.com/dotnet/reactive/commit/a6a49d8863ae4df71c29332d675978b0913aed9e) but I can not get it working even using a recent compiler (latest VS 2019). – MuiBienCarlota Dec 05 '19 at 10:21
  • @MuiBienCarlota yeah I just tried it here, couldn't make it work either... – benblo Dec 06 '19 at 13:59

2 Answers2

2

This can only be done when your method is inlined. Either the JIT does it, or you do it yourself (change the code). The JIT however, is pretty conservative about inlining methods and practically only does that when the method is very small, or when the method is used in a loop.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • Hm... to be honest I doubt it: even if it's inlined wouldn't the StackTrace keep track somehow? And, it doesn't check out in practice since my method is there, both in debug and release, and it's only a one-line wrapper. – benblo Apr 22 '10 at 11:13
  • 1
    No. When a method is inlined, it is really gone. There is no method invocation therefore, a stack walk will not reveal it. Because a stack trace is built using a stack walk, the stack trace will not show your method anymore. – Steven Apr 22 '10 at 12:25
0

As it is said in answer for other question Most Useful Attributes, you need to specify System.Diagnostics.DebuggerHidden attribute for your method.

Community
  • 1
  • 1
Alexander Kuzin
  • 463
  • 4
  • 13
  • 1
    Unfortunately that doesn't apply here, as the name suggests (and the documentation states) it's only a debugger attribute, it's up to the debugger to filter out the marked methods, but the method is still registered in the stack trace in the first place. My context is not debug-time, this is for Unity (game engine), which outputs the full stack of logs or exceptions, so Unity would have to filter out the methods intentionally. I'm afraid what I was looking to do (not register in the stack trace at all) is simply impossible. – benblo May 13 '14 at 18:48