在WebApi项目里使用MiniProfiler并且分析 Entity Framework Core
2019-07-26 00:43
1396 查看
在WebApi项目里使用MiniProfiler并且分析 Entity Framework Core
一、安装配置MiniProfiler
在现有的ASP.NET Core MVC WebApi 项目里,通过Nuget安装
MiniProfiler:
Install-Package MiniProfiler.AspNetCore.Mvc MiniProfiler.EntityFrameworkCore
当然也可以通过
Nuget Package Manager可视化工具安装
接下来就是如何配置和使用 MiniProfiler 了,总共分三步:
第一步,来到Startup.cs
的ConfigureServices
方法里,添加services.AddMiniProfiler();
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDbContext<DataContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DataContext"))); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); // 首先添加一个配置选项,用于访问分析结果: services.AddMiniProfiler(options => { // 设定弹出窗口的位置是左下角 options.PopupRenderPosition = RenderPosition.BottomLeft; // 设定在弹出的明细窗口里会显式Time With Children这列 options.PopupShowTimeWithChildren = true; // 设定访问分析结果URL的路由基地址 options.RouteBasePath = "/profiler"; }) // 然后在之前的配置后边加上AddEntityFramework(): .AddEntityFramework(); }
第二步,来到来到Startup.cs
的Configure
方法里,添加app.UseMiniProfiler();
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { ... // 最重要的一点是就是配置中间件在管道中的位置,一定要把它放在UseMvc()方法之前。 app.UseMiniProfiler(); app.UseMvc(); }
第三步、运行程序,一共有3个可查看分析结果相关的URL地址:
1./profiler/results-index
- 先看results-index页面:
它表示每次调用API的记录结果。可以看到本次调用API的总时间为1578.4毫秒。
2./profiler/results
- 从result-index页面点击链接进入这次API调用的详细结果页面,也就是result页面:
它表示每次调用API的过程分析结果,具体到每一条SQL语句的内容和执行时间。
3./profiler/results-list
- 再看result-list页面:
它其实就表示每个API的所有调用记录结果的集合。
三、案例源码:
MiniProfilerCoreWebApiDemo
相关文章推荐
- Asp.Net Core 项目实战之权限管理系统(3) 通过EntityFramework Core使用PostgreSQL
- 使用 MiniProfiler 来分析 ASP.NET Core 应用
- 使用 MiniProfiler 来分析 ASP.NET Core 应用
- 尝试.Net Core—使用.Net Core + Entity FrameWork Core构建WebAPI(一)
- Asp.net Core中使用Entity Framework Core CodeFirst
- JS与OC交互(iPhone项目)中JavaScriptCore.framework的使用
- 使用Entity Framework Core需要注意的一个全表查询问题
- 使用ASP.NET Core MVC 和 Entity Framework Core 开发一个CRUD(增删改查)的应用程序
- 使用Entity Framework Core访问数据库(Oracle篇)
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
- Entity Framework Core 2.0 使用代码进行自动迁移
- ASP.NET CORE系列【三】使用Entity Framework Core进行增删改查
- .Net Core使用 MiniProfiler 进行性能分析
- [UWP小白日记-11]在UWP中使用Entity Framework Core(Entity Framework 7)操作SQLite数据库(一)
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
- System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> System.InvalidOperationException: 超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
- Entity Framework Core 2.0 中使用LIKE 操作符
- Entity Framework Core 使用HiLo生成主键
- ABP .Net Core Entity Framework迁移使用MySql数据库
- [Abp 源码分析]七、仓储与 Entity Framework Core