"monitoring" C# methods invocation
Author |
Message |
Janu #1 / 4
|
"monitoring" C# methods invocation
Hello ! I am new to C# and I have encountered the following problem. I would like to develop classes which would 'monitor' users' operations on the database using ADO.NET data providers classes. Let's say user operates on db using Sql/OleDbConnection, Sql/OleDbCommand, Sql/OleDbDataAdapter, etc. I would like to monitor users method invocations on these classes e.g. Sql/OleDbConnection.Open()/Close(), Sql/OleDbCommand.ExecuteReader()/ExecuteNonQuery(), (by monitoring I mean logging messages about these methods invocation to file or sending messages via socket) 1. Is there any means I can 'detect' the users method invocation on objects Sql/OleDbConnection, Sql/OleDbCommand etc. ? Is any event published which I could listen to ? Maybe I could somehow listen to CLR ? 2. The simple solution which comes to my mind is to extend Sql/OleDbConnection, Sql/OleDbCommand classes by my ProxyConnection, ProxyDbCommand and overwrite methods I would like to monitor. The users would use these classes to connect to database. In the methods I would log the message and call super. But unfortunately Sql/OleDbConnection, Sql/OleDbCommand are sealed, and cannot be extended. I would also like to avoid writing too many new classes (ProxyConnection, ProxyDbCommand, ProxyDbAdapter, ProxyDbReader) but I would like to monitor methods invocations on them. I suspect all of the classes have private reference to Sql/OleDbConnection to database, and all operations concering database goes through this reference. Maybe I could somehow listen to Sql/OleDbConnection operations/method invocations. I would be very grateful for any help with my problem, Best Regards, Janusz
|
Sun, 21 Nov 2004 19:29:06 GMT |
|
|
Nicholas Paldino [.NET/C# MVP #2 / 4
|
"monitoring" C# methods invocation
Janusz, Your best bet would be to hook into the CLR. There was a great article in MSDN magazine titled "Microsoft .NET: Implement a Custom Common Language Runtime Host for Your Managed App". The article can be found online at (watch for line wrap): http://msdn.microsoft.com/msdnmag/issues/01/03/clr/clr.asp Mind you, this is no small undertaking, and you have to make sure to get it right, if you don't, you can run the risk of bombing out the running process (which would be a bad thing). Hope this helps. -- - Nicholas Paldino [.NET MVP]
Quote: > Hello ! > I am new to C# and I have encountered the following problem. > I would like to develop classes which would 'monitor' users' > operations on the database using ADO.NET data providers classes. > Let's say user operates on db using Sql/OleDbConnection, > Sql/OleDbCommand, Sql/OleDbDataAdapter, etc. > I would like to monitor users method invocations on these classes e.g. > Sql/OleDbConnection.Open()/Close(), > Sql/OleDbCommand.ExecuteReader()/ExecuteNonQuery(), > (by monitoring I mean logging messages about these methods invocation > to file or sending messages via socket) > 1. Is there any means I can 'detect' the users method invocation on > objects Sql/OleDbConnection, Sql/OleDbCommand etc. ? Is any event > published which I could listen to ? Maybe I could somehow listen to > CLR ? > 2. The simple solution which comes to my mind is to extend > Sql/OleDbConnection, Sql/OleDbCommand classes by my ProxyConnection, > ProxyDbCommand and overwrite methods I would like to monitor. The > users would use these classes to connect to database. In the methods I > would log the message and call super. But unfortunately > Sql/OleDbConnection, Sql/OleDbCommand are sealed, and cannot be > extended. > I would also like to avoid writing too many new classes > (ProxyConnection, ProxyDbCommand, ProxyDbAdapter, ProxyDbReader) but I > would like to monitor methods invocations on them. I suspect all of > the classes have private reference to Sql/OleDbConnection to database, > and all operations concering database goes through this reference. > Maybe I could somehow listen to Sql/OleDbConnection operations/method > invocations. > I would be very grateful for any help with my problem, > Best Regards, > Janusz
|
Sun, 21 Nov 2004 20:07:21 GMT |
|
|
Andreas H?kansso #3 / 4
|
"monitoring" C# methods invocation
He could also derive his own objects from ContectBoundObject and use Aspect-Oientated Programming[1]. There are quite a few introduction examples for this on various sites, and most of them build a small tracer that you add with the help of a customattribute [1] http://msdn.microsoft.com/msdnmag/issues/02/03/AOP/AOP.asp -- Andreas H?kansson Student of Software Engineering andreas (at) selfinflicted.org In a world in which we are all slaves to the laws of gravity, I'm proud to be counted as one of the freedom fighters.
Quote: > Janusz, > Your best bet would be to hook into the CLR. There was a great article > in MSDN magazine titled "Microsoft .NET: Implement a Custom Common Language > Runtime Host for Your Managed App". The article can be found online at > (watch for line wrap): > http://msdn.microsoft.com/msdnmag/issues/01/03/clr/clr.asp > Mind you, this is no small undertaking, and you have to make sure to get > it right, if you don't, you can run the risk of bombing out the running > process (which would be a bad thing). > Hope this helps. > -- > - Nicholas Paldino [.NET MVP]
> > Hello ! > > I am new to C# and I have encountered the following problem. > > I would like to develop classes which would 'monitor' users' > > operations on the database using ADO.NET data providers classes. > > Let's say user operates on db using Sql/OleDbConnection, > > Sql/OleDbCommand, Sql/OleDbDataAdapter, etc. > > I would like to monitor users method invocations on these classes e.g. > > Sql/OleDbConnection.Open()/Close(), > > Sql/OleDbCommand.ExecuteReader()/ExecuteNonQuery(), > > (by monitoring I mean logging messages about these methods invocation > > to file or sending messages via socket) > > 1. Is there any means I can 'detect' the users method invocation on > > objects Sql/OleDbConnection, Sql/OleDbCommand etc. ? Is any event > > published which I could listen to ? Maybe I could somehow listen to > > CLR ? > > 2. The simple solution which comes to my mind is to extend > > Sql/OleDbConnection, Sql/OleDbCommand classes by my ProxyConnection, > > ProxyDbCommand and overwrite methods I would like to monitor. The > > users would use these classes to connect to database. In the methods I > > would log the message and call super. But unfortunately > > Sql/OleDbConnection, Sql/OleDbCommand are sealed, and cannot be > > extended. > > I would also like to avoid writing too many new classes > > (ProxyConnection, ProxyDbCommand, ProxyDbAdapter, ProxyDbReader) but I > > would like to monitor methods invocations on them. I suspect all of > > the classes have private reference to Sql/OleDbConnection to database, > > and all operations concering database goes through this reference. > > Maybe I could somehow listen to Sql/OleDbConnection operations/method > > invocations. > > I would be very grateful for any help with my problem, > > Best Regards, > > Janusz
|
Sun, 21 Nov 2004 20:36:46 GMT |
|
|
Nicholas Paldino [.NET/C# MVP #4 / 4
|
"monitoring" C# methods invocation
Yes, but in the end, AOP needs to hook into the CLR in order to do so. =) While I like the concept behind AOP, I remember reading that article and not liking the implementation of it in .NET. -- - Nicholas Paldino [.NET MVP]
Quote: > He could also derive his own objects from ContectBoundObject > and use Aspect-Oientated Programming[1]. There are quite a few > introduction examples for this on various sites, and most of them > build a small tracer that you add with the help of a > customattribute > [1] http://msdn.microsoft.com/msdnmag/issues/02/03/AOP/AOP.asp > -- > Andreas H?kansson > Student of Software Engineering > andreas (at) selfinflicted.org > In a world in which we are all slaves to the laws of gravity, > I'm proud to be counted as one of the freedom fighters.
wrote
> > Janusz, > > Your best bet would be to hook into the CLR. There was a great > article > > in MSDN magazine titled "Microsoft .NET: Implement a Custom Common > Language > > Runtime Host for Your Managed App". The article can be found online at > > (watch for line wrap): > > http://msdn.microsoft.com/msdnmag/issues/01/03/clr/clr.asp > > Mind you, this is no small undertaking, and you have to make sure to > get > > it right, if you don't, you can run the risk of bombing out the running > > process (which would be a bad thing). > > Hope this helps. > > -- > > - Nicholas Paldino [.NET MVP]
> > > Hello ! > > > I am new to C# and I have encountered the following problem. > > > I would like to develop classes which would 'monitor' users' > > > operations on the database using ADO.NET data providers classes. > > > Let's say user operates on db using Sql/OleDbConnection, > > > Sql/OleDbCommand, Sql/OleDbDataAdapter, etc. > > > I would like to monitor users method invocations on these classes e.g. > > > Sql/OleDbConnection.Open()/Close(), > > > Sql/OleDbCommand.ExecuteReader()/ExecuteNonQuery(), > > > (by monitoring I mean logging messages about these methods invocation > > > to file or sending messages via socket) > > > 1. Is there any means I can 'detect' the users method invocation on > > > objects Sql/OleDbConnection, Sql/OleDbCommand etc. ? Is any event > > > published which I could listen to ? Maybe I could somehow listen to > > > CLR ? > > > 2. The simple solution which comes to my mind is to extend > > > Sql/OleDbConnection, Sql/OleDbCommand classes by my ProxyConnection, > > > ProxyDbCommand and overwrite methods I would like to monitor. The > > > users would use these classes to connect to database. In the methods I > > > would log the message and call super. But unfortunately > > > Sql/OleDbConnection, Sql/OleDbCommand are sealed, and cannot be > > > extended. > > > I would also like to avoid writing too many new classes > > > (ProxyConnection, ProxyDbCommand, ProxyDbAdapter, ProxyDbReader) but I > > > would like to monitor methods invocations on them. I suspect all of > > > the classes have private reference to Sql/OleDbConnection to database, > > > and all operations concering database goes through this reference. > > > Maybe I could somehow listen to Sql/OleDbConnection operations/method > > > invocations. > > > I would be very grateful for any help with my problem, > > > Best Regards, > > > Janusz
|
Sun, 21 Nov 2004 20:42:21 GMT |
|
|
|