当前位置:网站首页>基于.NetCore开发博客项目 StarBlog - (11) 实现访问统计
基于.NetCore开发博客项目 StarBlog - (11) 实现访问统计
2022-06-12 00:36:00 【dotNET跨平台】
系列文章
基于.NetCore开发博客项目 StarBlog - (11) 实现访问统计
...
前言
放完端午假期回来,我的博客还没上线,看着各位大佬的网站都办得风生水起,我隐约感觉到一丝紧迫感,得抓紧时间写代码了~!
前面的文章已经把博客网站的基本功能实现起来了,距离网站上线只有一步之遥,然而还有一些细枝末节之处,对网站的运营或者用户体验有很大影响,必须完善的,比如:
网站访问统计
图床
按照先后顺序,本文先解决访问统计的问题。
一提到访问统计,众所周知的就是Google Analytics或者百度统计,前者在国内无法使用,后者应该是唯一选择了,但考虑到两者的功能差距以及该公司的口碑,对我这个小破站来说,我宁愿自己实现一个~
实现
实现起来应该是不难的,为了实现统计、分析,我们需要先记录,记录的内容包括用户的IP、设备、访问路径、访问时间,然后再对这些数据进行分析,就可以得出每篇文章、图片、分类的阅读量/访问量。
为了记录这些数据,需要在用户发起每一个请求时都进行处理,在AspNetCore中有多种实现,比如Filter、比如Middleware
这里我选的是中间件的方式
PS:我看到网上有一篇文章介绍Django实现访问统计的,那个作者写了一个方法,在每个接口里调用一次,看得我头皮发麻
在中间件中注入Repo,然后每次请求都把IP、设备、请求路径等数据保存在数据库中。这样看起来应该是会有一些影响性能的,不过考虑到博客网站的并发应该很小,带来的性能损失应该可以忽略不计。(说白了就是懒)
后续有性能瓶颈时再来优化吧~
模型定义
在StarBlog.Data/Models中新建一个VisitRecord.cs
为了节省篇幅,注释啥的就不放了,具体代码可见github
public class VisitRecord {
public int Id { get; set; }
public string Ip { get; set; }
public string RequestPath { get; set; }
public string? RequestQueryString { get; set; }
public string RequestMethod { get; set; }
public string UserAgent { get; set; }
public DateTime Time { get; set; }
}写个中间件
在StarBlog.Web/Middlewares中新建一个VisitRecordMiddleware.cs
public class VisitRecordMiddleware {
private readonly RequestDelegate _next;
public VisitRecordMiddleware(RequestDelegate requestDelegate) {
_next = requestDelegate;
}
public Task Invoke(HttpContext context, IBaseRepository<VisitRecord> visitRecordRepo) {
var request = context.Request;
var response = context.Response;
visitRecordRepo.InsertAsync(new VisitRecord {
Ip = "",
RequestPath = request.Path,
RequestQueryString = request.QueryString.Value,
RequestMethod = request.Method,
UserAgent = request.Headers.UserAgent,
Time = DateTime.Now
});
return _next(context);
}
}这里有个地方要注
意的,我的visitRecordRepo是在Invoke方法中注入的,为什么不在构造方法中注入呢?
因为AspNetCore中间件的构造方法是Singleton的,所以只能注入Singleton方式的服务,而FreeSQL的Repository是Scoped的,如果在构造方法中注入就会报错
所以只能在Invoke方法中注入~
使用中间件
编辑StarBlog.Web/Programs.cs,增加中间件配置
app.UseMiddleware<VisitRecordMiddleware>();PS:本项目基于.Net6,Startup和Program合二为一了;如果是.NetCore3.1及以下版本,则是在Startup.cs中配置中间件。
效果
目前还没有根据访问记录做分析,所以只能截个数据库的图片

OK,接下来可以根据这些数据来做分析了(后面管理后台开发的部分再来写)
边栏推荐
- How to send Apple phone WPS files to QQ mailbox
- [node] common methods of path module
- [day 5 of JUC learning] reference atomic classes and attribute modifiers
- Creating and running JMeter performance test scenarios
- Optimization method of win7 FPS
- Explore table component virtualization
- Mathematical modeling experience ----- summary of three modeling
- Jmeter性能测试场景的创建和运行
- The road of global evolution of vivo global mall -- multilingual solution
- 2022 edition of global and Chinese high purity silicon carbide powder operation research and investment strategy analysis report
猜你喜欢

Environment construction 2

七个需要关注的测试自动化趋势

Cuiyunkai, CEO of Gewu titanium Intelligent Technology: data value jump, insight into the next generation of change forces

Web keyboard input method application development guide (2) -- keyboard events

How to strengthen the prevention and control of major safety risks for chemical and dangerous goods enterprises in flood season
![[case] building a universal data lake for Fuguo fund based on star ring technology data cloud platform TDC](/img/e0/0326d01fc78ed2f96a475e28e74d1c.jpg)
[case] building a universal data lake for Fuguo fund based on star ring technology data cloud platform TDC

repeat_ L2-009 red envelope grabbing_ sort

Matplotlip basic drawing learning of data analysis - 01

The latest report of Xinsi technology shows that 97% of applications have vulnerabilities

ironSource&nbsp; New functions are released, and developers can adjust advertising strategies in real time in the same session
随机推荐
Creating and running JMeter performance test scenarios
Characteristics of JS logical operators
IP addressing overview
Use select to switch coroutines
JS——防止自动恢复页面位置
Explore table component virtualization
统一认证中心 Oauth2 高可用坑
关于接口测试的那些“难言之隐”
Month selector disable data after the current month to include the current month
Concevoir une table MySQL pour la mise en file d'attente des messages pour stocker les données des messages
[industry application] an article on power digital transformation
ironSource&nbsp; New functions are released, and developers can adjust advertising strategies in real time in the same session
Seven trends in test automation that need attention
Investment analysis and prospect Trend Research Report of global and Chinese cyclopentanyl chloride industry 2022-2028
2022 edition of global and Chinese sodium hydrosulfide market in-depth investigation and prospect Trend Forecast Report
How to optimize plantuml flow diagram (sequence diagram)
leetcodeSQL:614. Secondary followers
深度学习100例 | 第41天:语音识别 - PyTorch实现
Optimization method of win7 FPS
WPS标题段前间距设置无效解决方案