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.
我想为我的Web应用程序的所有用户保留每个页面视图的日志。经过足够的时间后,我将使用该日志生成报告。我想让日志记录机制有点灵活,因为我不需要记录每个http请求。
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.
一个示例用例是:公司注册我的Web应用程序,允许5名员工使用该应用程序。我想报告说,上周有3名员工使用了该应用程序。或者表明当年6月至8月期间有4名员工使用过它。
I'm using asp.net mvc with sql server, if that makes a difference.
我正在使用带有sql server的asp.net mvc,如果这有所不同。
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.
它就这么简单吗?使用以下列创建一个sql表:UserId,ControllerName,ActionName,ActionParameters,CreatedOn。然后创建一个ActionFilterAttribute,为每个调用的操作向db添加记录。
Are there any pitfalls that I should worry about (other than a potentially large table size)?
是否有任何我应该担心的陷阱(除了可能很大的表格大小)?
3 个解决方案
#1
6
Some of the pitfalls:
一些陷阱:
- You will not be able to do any server level page caching.
- 您将无法执行任何服务器级页面缓存。
- Lots of db writes since you are in effect logging each request.
- 由于您实际记录了每个请求,因此大量的数据库写入。
- 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.
我假设您将在某种类型的自定义AuthorizeAttribute中实现此功能,因此您不需要在每个控制器上编写它,但它实际上是每个请求可能比您实际需要的更多。
Have you considered using some type of IIS log parsing utility? It would have a lot of the info you need already.
您是否考虑过使用某种类型的IIS日志解析实用程序?它会有很多你需要的信息。
#2
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:
如果您愿意使用JavaScript,另一个选择是您可以实现SiteCatalyst的某些功能。您在页面加载时使用JavaScript从客户端发送请求到控制器操作,并在URL中包含相应的信息。例如:
www.example.com\AnalyticsController\Action\currentController\currentAction\userName
www.example.com \ AnalyticsController \行动\ currentController \ currentAction \用户名
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.
优点是您的日志记录不会与所有其他操作按顺序执行,您可以通过将AJAX调用放在母版页中来使其工作。
The downside is that it uses JavaScript which as pointed out could be disabled. It would also be really easy to spoof.
缺点是它使用的JavaScript可能会被禁用。恶搞也很容易。
#3
0
Have a look at the IActionFilter interface - you can write your own class that implements this, and log whatever you need to.
看看IActionFilter接口 - 您可以编写自己的类来实现这一点,并记录您需要的任何内容。
#1
6
Some of the pitfalls:
一些陷阱:
- You will not be able to do any server level page caching.
- 您将无法执行任何服务器级页面缓存。
- Lots of db writes since you are in effect logging each request.
- 由于您实际记录了每个请求,因此大量的数据库写入。
- 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.
我假设您将在某种类型的自定义AuthorizeAttribute中实现此功能,因此您不需要在每个控制器上编写它,但它实际上是每个请求可能比您实际需要的更多。
Have you considered using some type of IIS log parsing utility? It would have a lot of the info you need already.
您是否考虑过使用某种类型的IIS日志解析实用程序?它会有很多你需要的信息。
#2
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:
如果您愿意使用JavaScript,另一个选择是您可以实现SiteCatalyst的某些功能。您在页面加载时使用JavaScript从客户端发送请求到控制器操作,并在URL中包含相应的信息。例如:
www.example.com\AnalyticsController\Action\currentController\currentAction\userName
www.example.com \ AnalyticsController \行动\ currentController \ currentAction \用户名
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.
优点是您的日志记录不会与所有其他操作按顺序执行,您可以通过将AJAX调用放在母版页中来使其工作。
The downside is that it uses JavaScript which as pointed out could be disabled. It would also be really easy to spoof.
缺点是它使用的JavaScript可能会被禁用。恶搞也很容易。
#3
0
Have a look at the IActionFilter interface - you can write your own class that implements this, and log whatever you need to.
看看IActionFilter接口 - 您可以编写自己的类来实现这一点,并记录您需要的任何内容。