Search code examples
.netado.netaopcastledynamic-proxy

Using AOP techniques to intercept 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).

I'm wondering if there is any way to globally intercept any ADO.Net calls so that I can start auditing information like, 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/wrap the ADO.Net... Is this possible?

EDIT

Its been suggested that I look into PostSharp, CciSharp or Afterthought, but how do I use one of these to get the desired results?


Solution

  • No. If you're coding directly against ADO.NET types you can't just tell DynamicProxy: "hey, put a proxy wherever I use SqlConnection in all these DLLs". DynamicProxy is a runtime proxy generator. It seems that you want post-compilation AOP, so look into PostSharp, CciSharp or Afterthought instead.

    EDIT: if none of those tools are enough, you could use Mono.Cecil to directly alter your IL, but it's not easy. See for example: