8

I would like to keep a log of each page view for all users of my web application. After enough time has passed, I will then use that log to generate reports. I would like to have the logging mechanism be a bit flexible since I wouldn't need to log every http request.

An example use case is: A company signs up for my web app and allows 5 employees to use the application. I would like to report that 3 employees used the application in the last week. Or show that 4 employees used it between June and August of the current year.

I'm using asp.net mvc with sql server, if that makes a difference.

Is it just as simple as this? Create a sql table with the following columns: UserId, ControllerName, ActionName, ActionParameters, CreatedOn. Then create an ActionFilterAttribute that adds a record to the db for each action invoked.

Are there any pitfalls that I should worry about (other than a potentially large table size)?

Jim Geurts
  • 20,189
  • 23
  • 95
  • 116
  • 2
    Maybe a more scalable solution is to use Google Analytics and track "virtual urls" that I could parse. Like /reports////. After that, use their data feed api and parse the response – Jim Geurts Oct 03 '09 at 20:02
  • 1
    Not secure and users can switch off JS or block GA. – queen3 Oct 03 '09 at 20:47

3 Answers3

6

Some of the pitfalls:

  1. You will not be able to do any server level page caching.
  2. Lots of db writes since you are in effect logging each request.
  3. If there are any actions that happen on pages you could be storing redunant information.

I would assume you would implement this in some type of custom AuthorizeAttribute so your not coding it on every controller but then it will be literally every request which would possibly be more than you actually need.

Have you considered using some type of IIS log parsing utility? It would have a lot of the info you need already.

Kelsey
  • 47,246
  • 16
  • 124
  • 162
2

Another option if you are willing to use JavaScript is that you could implement something along the lines of SiteCatalyst. You send a request using JavaScript from the client on page load to a controller action with the appropriate information in the url. eg:

www.example.com\AnalyticsController\Action\currentController\currentAction\userName

The advantage is that your logging is not executed in sequence with all your other operations and you can probably get it working by putting the AJAX call in the master page.

The downside is that it uses JavaScript which as pointed out could be disabled. It would also be really easy to spoof.

Dean Johnston
  • 446
  • 4
  • 7
0

Have a look at the IActionFilter interface - you can write your own class that implements this, and log whatever you need to.

belugabob
  • 4,302
  • 22
  • 22