views:

454

answers:

4

On an ASP.NET MVC website, what's the best way to implement access logging? I want to answer the following questions:

  1. What are the most popular pages?
  2. Which pages were accessed in the last 24 hours?
  3. etc.

I could log in to the server using Remote Desktop and poke through the IIS logs myself (even using Microsoft Logfile Parser), but I'd like to be able to run reports from the admin pages of my site.

What's the best way to do this? Should I:

  1. Add tracing to all of my controller methods?
  2. Add an IHttpFilter (or whatever) and have that do the logging?
  3. Configure IIS (IIS7 on Windows 2008) to log to a table in my database, and then define an NHibernate mapping for this table?
  4. Something else?
+2  A: 

Hello, here is an alternate approach: http://code-inside.de/blog-in/2008/04/17/aspnet-mvc-using-a-actionfilter-for-logging/

Sebastian Sedlak
A: 

Sebastian's answer is certainly not bad (up-voted), however, it is re-implementing functionality already available in IIS. IIS's logging will probably work better than something homegrown, too. (For example, it will log stuff which doesn't pass through the MVC stack.) For that reason, I would favor using something along the lines of your option 3.

Craig Stuntz
Surely that makes the site more dependant on external factors. What happens when/ if the MVC is ported to Mono?Just a thought.
Kieron
Well, I think that's right. Neither doing it yourself nor using built in functionality will solve every problem. Each has its advantages and disadvantages. That's why I voted for Sebastian's answer as well as writing my own. :)
Craig Stuntz
A: 

Why not just use [Google Analytics]?(http://www.google.com/analytics/) In my opinion a far simpler way to get these statistics. Plus you'll get a wealth of bonus information. Not to mention you have acces to it anywhere in the world.

SpoBo
A: 

I counter logging strictly server-side. You need some client-side code to execute to count cache reloads and such. That's why all modern web analytics (such as Google Analytics and more) have a client-side piece of... code too.

Output cache can also be a pain when doing logging if you don't insert an HttpModule before the Output cache module and so on.

Andrei Rinea