C#:如何从堆栈跟踪中隐藏方法?
我有一个非常简单的单行方法,我不想在堆栈跟踪中看到它:这可能吗?也许用属性标记它?
只是为了澄清,我并没有尝试打印跟踪,或重新抛出,或在调试器中自动单步执行。我希望该方法首先不显示在跟踪中,因为跟踪随后由一些第三方代码处理。我想要在运行时进行控制,我对调试不感兴趣。 我这么说是因为我在 StackTrace 上读到的大部分内容似乎都与这些主题有关。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这只能在内联您的方法时完成。要么 JIT 来做,要么你自己做(更改代码)。然而,JIT 对于内联方法相当保守,实际上只在方法非常小时或在循环中使用方法时才这样做。
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.
从 dotnet 6 开始,您可以使用 StackTraceHiddenAttribute
Starting with dotnet 6, you can use StackTraceHiddenAttribute
正如在回答其他问题 最有用的属性 中所说,您需要指定 System.Diagnostics.DebuggerHidden您的方法的属性。
As it is said in answer for other question Most Useful Attributes, you need to specify System.Diagnostics.DebuggerHidden attribute for your method.