Using LogParser to Find Most Frequently Accessed Pages of an ASP.NET Website

Log parser is a powerful, versatile tool that a SQL-like query language to access a wide variety of text-based data sources such as log files, XML files and CSV files, as well as the Event Log, the Registry, binary files, and Active Directory. You can download LogParser here. The installer also contains samples and documentation about the different commands of Log Parser.

In this short article, we will use LogParser to parse the log file generated by IIS to find 10 most frequently accessed resource on your website. Once we have this information, we can fine tune these pages/resources to increase the overall performance of the site. The log file I am using here is from a live website and contains 3 days of data.

Step 1: Assuming your log file exists on your local machine, create an empty query file which will contain commands to access data from this log file. In this example, the query file is called top10.sql and the log file is called dnc.log. Both these files have been kept in the D: Drive of my machine.


Step 2: Open top10.sql and type the following command

Select Top 10
    cs-uri-stem as [Request URI],
    COUNT(*) AS Hits
FROM D:\dnc.log
Group by cs-uri-stem ORDER BY Hits DESC


The query is quite simple. It selects the top 10 cs-uri-stem (url’s) from the log files and groups them by the number of hits each one got in three days.

Step 3: After installing LogParser, go to Start > All Programs > LogParser. A command line window appears. Type the following command to execute the query

log-parser-command

LOGPARSER -i:IISW3C file:D:\top10.sql -o:DataGrid -q:off

Here the command -i:IISW3C signifies that we are querying the IIS W3C logs. -o:DataGrid outputs the result in a data grid.

On  executing the query, I received the following information

log-parser-output

Note: If you are thinking why did we use a .sql file in the first place instead of typing the command directly in the command window, then remember that there is a limitation of 260 characters in the command line window. Keeping the query in a separate query file helps us workaround this limitation.

This was just an example of how to use LogParser to retrieve useful performance-related information from your IIS logs. You can use it to gather information about search engines, browsers, file sizes, IP Addresses, response time and much much more.




About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: