逐行拦截方法
在 C# 中,是否有任何可行的方法可以在运行时逐行拦截方法?
感兴趣的具体应用是动态日志记录:
如果方法体内的某些内容抛出异常,那么在后续执行时,该方法的每一行都会以某种方式记录下来。在编译时不知道哪些特定方法会抛出异常,因此该技术应该能够干扰源代码中的大多数方法(可以预先以某种通用方式准备)
,在想象的世界中,拦截器将还公开参数,以便轻松识别和评估所执行的最后一行代码的细节。
但鉴于这可能根本不可能,是否有可用于此类工具的粗略代理?
一如既往的感激。
in c#, is there any practicable way to intercept a method line after line, at run-time?
the specific application of interest would be dynamic logging:
if something within a method body threw an exception then on subsequent executions every line of that method would be logged some way or another. it is not known at compile-time which specific methods will throw an exception, so the technique should be able to interfere with most methods in the source code (which could be prepared in some generic way beforehand)
in the imaginary world, the interceptor would also expose parameters for easy identification and evaluation of the specifics of the last line of code executed.
but given this may not be possible at all, is there any rough proxy available for such a tool?
grateful as always.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信唯一接近你想要的就是 ELMAH。您可以修改 ELMAH 来做一些事情——所以我想您可以使用它作为起点。然而,获得逐行类型功能的唯一方法是编写自己的调试器 - 在调试信息(您提到的 pdb 文件)中定义的每一行上弹出 int 13 - 这是很多工作。
使用 ELMAH,您可以看到有关系统状态的大量信息,最重要的是堆栈跟踪。
I believe the only thing that comes close to what you want is ELMAH. You can modify ELMAH to do stuff -- so I guess you could use it as a starting point. However, the only way to get the line by line type functionality would be to write your own debugger -- pop in the int 13s on every line as defined in the debugging info (the pdb files you mention) -- it is a lot of work.
With ELMAH you can see a whole lot about the state of the system most importantly the stack trace.