开发者

Using PostSharp to intercept ADO.Net

开发者 https://www.devze.com 2023-03-03 00:28 出处:网络
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).

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

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.

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/wra开发者_StackOverflow社区p the ADO.Net calls... Is this possible?


You can globally intercept any methods that you have access to (ie: your generated models & context). If you're needing to intercept methods in framework BCL then no.

If you just want to get the SQL generated from your EF models then intercept one of the desired methods with the OnMethodBoundaryAspect and you can do your logging in the OnEntry and OnExit methods.

Remember, you can intercept only code you have access to. Generated EF code is accessable but will over write any changes you make to it so you will need to apply the aspect using either a partial class or with an assembly declaration. I would suggest the latter since you want global interception.

Just my 2 cents: You might want to look at other alternatives for this such as SQL profiler or redesigning your architecture.


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.

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.

Jamie Thomas (primary author of Afterthought)

0

精彩评论

暂无评论...
验证码 换一张
取 消