开发者

How to count all the queries executed by a page in asp.net

I want to list out all the queries executed by a page and log the all queries executed by a page. I am using 开发者_StackOverflowMYSQL. Can any one help me with this?


You should consider removing the queries from the page and abstracting them to a common data provider with an integrated logging mechanism. There are a variety of patterns (MVP, MVC, MVVM) that you can learn from to see how this problem has been solved before. When you separate your activities (data access, business logic, presentation) you will find it easier to manage each individual piece.

If you are constrained from adapting such a data access practice for whatever reason, you can simply unify the query process in a few functions (based on your return types) so that you have something that looks like this in your aspx.cs:

protected DataSet getDataSet(iDataCommand command)
{
//log the request
try
{
//open the connection
//execute the command
}
catch
{
//log failure
}
//log success
//return the result
}

Then you just have to be certain to route all of your requests through these process.


You can create special user for mysql, with name like a log_asp_user. than add needs permission and set few parameters in db:

SET GLOBAL general_log = ON;
SET GLOBAL log_output = 'FILE,TABLE';
SET GLOBAL log_queries_not_using_indexes = ON;
SET GLOBAL long_query_time = 1;

it's need for log all query to mysql. than save in asp start and end time of generation page and select all query from db:

SELECT * FROM mysql.general_log  WHERE Time > min AND Time < max AND user_host LIKE '%log_asp_user%'


When i need to find queries being Executed in SQL server I use profiler to capture all the queries. I dont think this will work with MYSQL but you may be able to find an equivalent.

If there is no equivalent then assuming all of your queries are handled by a single class to execute them on a database then it would not take too much to log commands that passed through it.

You could add a property to the class to determine whether commands are logged or not and just turn it on for the current page.


MySQL can produce a log file of queries, so if you are able to run your application in isolation (ie without other users hitting the database at the same time), you should get a very clear picture of what your code is doing simply by running the app and then looking at the query log.

See here for more info on MySQL logging: http://dev.mysql.com/doc/refman/5.1/en/query-log.html

But it sounds to me like you want to do some sort of profiling excersise on your system, presumably to investigate and improve performance. In that case, I'd recommend using proper code profiling tools to investigate. Here's a link to a relevant article on MSDN to get you started: http://msdn.microsoft.com/en-us/library/3xxk09t8%28v=vs.71%29.aspx

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜