问题描述
我有相当大的代码库,使用各种不同的 ADO 技术(即一些 EF,在某些情况下直接使用 ADO.Net).
I have quite a large code base using a variety of different ADO technologies (i.e. some EF and in some cases using ADO.Net directly).
我想知道是否有任何方法可以全局拦截任何 ADO.Net 调用,以便我可以开始审核信息 - 执行的确切 SQL 语句、花费的时间、返回的结果等.
I'm wondering if there is any way to globally intercept any ADO.Net calls so that I can start auditing information - exact SQL statements that executed, time taken, results returned, etc.
主要想法是,如果我能做到这一点,我就不必更改任何现有代码,而且我应该能够拦截/包装 ADO.Net 调用......这可能吗?
The main idea being that if I can do this, I shouldn't have to change any of my existing code and that I should be able to just intercept/wrap the ADO.Net calls... Is this possible?
推荐答案
Afterthought 是一个开源工具它支持修改现有的 dll,而无需您从源代码重新编译以添加方面属性.为此,您需要在单独的 dll 中创建修改(在 Afterthought 中描述更改的方式),并且该 dll 需要具有实现 IAmendmentAttribute 的程序集级属性,该属性将标识目标程序集中的类型以过程.
Afterthought is an open source tool that supports modifying an existing dll without requiring you to recompile from source to add aspect attributes. For this to work, you would need to create amendments (the way you describe your changes in Afterthought) in a separate dll, and this dll would need to have an assembly-level attribute implementing IAmendmentAttribute that would identify the types in your target assembly to process.
查看日志示例,了解其工作原理如果您有任何疑问/问题,请告诉我.
Take a look at the logging example to see how this works and let me know if you have any questions/issues.
请注意,Afterthought 会修改您的目标程序集以调用另一个程序集(您的工具)中的静态方法.如果您想通过以任何方式修改目标程序集来拦截调用,那么我建议您查看 .NET 分析 API.
Please note that Afterthought modifies your target assembly to make calls to static methods in another assembly (your tool). If you want to intercept calls with modifying the target assembly in any way, then I recommending looking into the .NET profiling API.
杰米·托马斯(Afterthought 的主要作者)
Jamie Thomas (primary author of Afterthought)
这篇关于使用 PostSharp 拦截 ADO.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!