为什么要使用MiniProfiler? 如果程序需要性能调优,我们使用它可以帮我们分析代码的执行情况。 MiniProfiler是一款性能分析的小程序。 我们先以Console程序为例,使用MiniProfiler小工具。 1. 安装包:MiniProfiler 2. 创建一个profiler对象 //profiler和MiniProfiler.Current是相等的 var profiler = MiniProfiler.StartNew("My Pofiler Name 安装包:MiniProfiler.Mvc5 2. ; { MiniProfiler.StartNew(); } } 4.
http://mpvideo.qpic.cn/0b7854aaeaaaoeaonvhiejpfb36dalxqaaqa.f10002.mp4?dis_k=3cc4540bf66cc071d7d43e4
框架集成MiniProfiler+Swagger来做接口的简单性能分析,和每次请求中相关的Sql执行情况报告; 一、相关的依赖注入配置 // 服务注册 builder.Services.AddMiniProfilerSetup (); // 中间件 app.UseMiniProfilerMiddleware(); 相关参数设置 "Startup": { "MiniProfiler": { "Enabled": true using (MiniProfiler.Current.Step("获取成功后,开始处理最终数据")) { // 一些逻辑... } return SuccessPage (pageModelBlog); } 也可以直接输出对应的时间线内容 MiniProfiler.Current. res.Content = JsonHelper.GetJSON<MessageModel<string>>(json); context.Result = res; MiniProfiler.Current.CustomTiming
MiniProfiler(https://miniprofiler.com/)是一个轻量级且简单易用的分析工具库,它可以用来分析ASP.NET Core应用。 安装配置MiniProfiler 在现有的ASP.NET Core MVC项目里,通过Nuget安装: ? 接下来,想把MiniProfiler配置好,总共分三步? 运行应用,可以看到左下角就是MiniProfiler: ? 点击它之后会弹出窗口: ? 这里面有每个步骤具体的耗用时间。 分析局部代码 前面的例子里,我们使用MiniProfiler分析了页面整个流程的时间。而MiniProfiler也可以用来分析一段代码所耗用的时间。 这个例子里,我们使用了MiniProfiler.Current.CustomTiming()方法。
MVC MiniProfiler是Stack Overflow团队设计的一款对ASP.NET MVC的性能分析的小程序。 MiniProfiler是以Apache License V2.0协议发布的,你可以在NuGet找到。 包括以下核心组件: MiniProfiler MiniProfiler.EntityFramework 如何安装? MiniProfiler、MiniProfiler.EF、MiniProfiler.MVC3,同时会自动安装依赖组件:WebActivator, 同时也会自动在项目里面添加代码文件:MiniProfiler.cs (); 修改View的layout文件,在head区域增加如下代码:@StackExchange.Profiling.MiniProfiler.RenderIncludes() 如果安装步骤一切顺利的话
要在.NET Core项目中集成MiniProfiler和Swagger,可按照以下步骤操作: 1. 安装NuGet包: • 安装MiniProfiler.AspNetCore.Mvc包以集成MiniProfiler。 • 安装MiniProfiler.EntityFrameworkCore包以监控EF Core生成的SQL语句(可选)。 获取MiniProfiler HTML代码片段: • 在一个控制器中添加一个方法来获取MiniProfiler的HTML代码片段: [HttpGet] public IActionResult GetCounts using (MiniProfiler.Current.Step("准备数据")) { using (MiniProfiler.Current.CustomTiming
安装依赖包 在web项目打开nuget包管理器搜索 MiniProfiler.Mvc5和MiniProfiler.EF6安装。 2. 在Global.asax中添加配置代码 protected void Application_Start() { MiniProfiler.Configure(new MiniProfilerOptions { // Sets up the route to use for MiniProfiler resources: // Here, ~/profiler is if (Request.IsLocal) // Example of conditional profiling, you could just call MiniProfiler.StartNew() ; { MiniProfiler.StartNew(); } } protected void Application_EndRequest() { MiniProfiler.Current
MiniProfiler核心功能 专为ASP.NET Core Web Apps & Web APIs设计,支持: ✅ 全链路性能监控:数据库查询、API端点、业务逻辑等关键环节 ✅ 可视化分析:请求执行时间分布 直观呈现性能低下代码段 快速入门:ASP.NET Core API集成步骤 步骤1:创建WebApi项目 dotnet new webapi -n MyWebApi cd MyWebApi 步骤2:安装MiniProfiler 套件 dotnet add package MiniProfiler.AspNetCore dotnet add package MiniProfiler.AspNetCore.Mvc dotnet add returnawait connection.QueryAsync<SqlUser>("SELECT * FROM Users"); } } 步骤5:Program.cs配置MiniProfiler using StackExchange.Profiling; var builder = WebApplication.CreateBuilder(args); // 注册MiniProfiler
MiniProfiler~ 这个东西的介绍如下: MVC MiniProfiler是Stack Overflow团队设计的一款对ASP.NET MVC的性能分析的小程序。 第一步,从NuGet上下载所需要的包,下载内容如图: MiniProfiler核心(所有的MiniProfiler相关资源都需要先有他) ? 这里需要注意,新版本的MiniProfiler.EF是需要根据你的EF版本来下载的,分为MiniProfiler.EF6,MiniProfiler.EF5,MiniProfiler.EF(EF4以下)三个版本 MiniProfiler.MVC4(注:这里的MVC4是可以分析MVC4,5两个版本的,使用MVC3的同学请自行下载MiniProfiler.MVC3) ? 在MiniProfiler中,提供了两个委托,如下: MiniProfiler.Settings.Results_Authorize //配置监控的权限 MiniProfiler.Settings.Results_List_Authorize
2,运行命令 PM> Install-Package MiniProfiler ? 3,因为我这里使用的是Entity framework 4.1 code first 所以还需要下载 一个包PM> Install-Package MiniProfiler.EF ? 4,使用说明,查看连接: http://miniprofiler.com/ 5,我在MVC3+E.F4.1中的使用 View Code 1 public ActionResult (int pindex = 0) 2 { 3 try 4 { 5 var profiler = MiniProfiler.Current (Exception from HRESULT: 0x80131047) 10 //MiniProfiler attempts to resolve this issue by
MVC MiniProfiler是Stack Overflow团队设计的一款对ASP.NET MVC、WebForm 以及WCF 的性能分析的小程序。 MiniProfiler是以Apache License V2.0协议发布的,你可以在NuGet找到。 MVCMiniProfiler 简介 【观点】如何打造网站高性能的核心优势 Using MVC Mini Profiler as an HTTP Module Simple, fast and useful MiniProfiler
SQL监控
使用EF的同学应该很多人都知道MiniProfiler,我在前些年分享EF的时候有做过简单介绍。
那么我们在执行Dapper的时候是不是也可以对生成的sql做检测和性能监控。 Git地址
MiniProfiler监控套件还真不是一般的强。EF、MongoDB、MySql、Redis、SqlServer统统支持。 接下来我们实现对Dapper监控,导入Nuget包MiniProfiler.AspNetCore
public class ActionFilter : IAsyncActionFilter
{ await next();
}
WriteLog(profiler);
}
///
使用MiniProfiler调试ASP.NET MVC网站性能,MiniProfiler可以很好的处理网站后端每个处理时间的事件,但是MiniProfiler是无法远程做监测的动作,MiniProfiler
推荐MiniProfiler插件 工欲善其事,必先利其器。 我们使用EF和在很大程度提高了开发速度,不过随之带来的是很多性能低下的写法和生成不太高效的sql。 在这里强烈推荐一个插件MiniProfiler。实时监控页面请求对应执行的sql语句、执行时间。简单、方便、针对性强。 如图: ? 然后我们点击打开MiniProfiler工具(不要被吓到) ? ? 再看MiniProfiler的监控(瞬间101条sql变成了1条,这其中的性能可想而知。) ? AutoMapper工具 上面我们通过Include显示的执行表的连接查询显然是不错的,但还不够。
后端:C#、.NET 9、AspNetCoreRateLimit、SqlSugar、AutoMapper、SignalR、MiniProfiler、CAP、RabbitMQ、Redis、Consul等。
后端技术: .NET 8、SqlSugar、JWT、Automapper、AutoFac、Redis、IpRateLimiting、Swagger、MiniProfiler、Quartz.Net、Serilog
就清爽了很多,业务和逻辑依然很丰富: 还有没有其他扩展方案 当然,如果你感觉这种把扩展提出来还不能满足灵活的需求,可以再细致一下,针对每一个组件都做一个扩展,比如这样: xxx.Swagger、xxx.MiniProfiler
Microsoft.EntityFrameworkCore.Database.Command": "Information" // 记录生成的 SQL } } } 使用数据库性能分析工具 // 安装并配置 MiniProfiler 查询分析: 定期使用日志记录或性能分析工具(如 SQL Server Profiler, Azure Data Studio, MiniProfiler)审查生成的 SQL。
OSharp.AspNetCore.Routing;using OSharp.AutoMapper;using OSharp.Core;using OSharp.Log4Net;//using OSharp.MiniProfiler
中直接注入了,如下: protected void Application_Start() { //注入自己写的监听 DbInterception.Add(new MiniProfiler_EFModel.DatabaseLogger