ASP.NET Core在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的所有调用记录结果的集合。
案例源码:
到此这篇关于ASP.NET Core在WebApi项目中使用MiniProfiler分析Entity Framework Core的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
- Entity Framework Core使用控制台程序生成数据库表
- Entity Framework Core延迟加载(懒加载)用法
- Entity Framework Core实现Like查询详解
- Entity Framework Core中执行SQL语句和存储过程的方法介绍
- Entity Framework Core批处理SQL语句
- Entity Framework Core实现软删除与查询过滤器
- Entity Framework Core生成列并跟踪列记录
- Entity Framework Core工具使用命令行
- Entity Framework Core关联删除
- 详解如何在ASP.NET Core中应用Entity Framework
- Entity Framework Core对Web项目生成数据库表
相关文章
ASP.NET中基于soaphead的webservice安全机制
常会用到WebService来通讯,但WebService发布后为了能调用,一般都通过发布到IIS后调用 的。在IIS里可以通过匿名访问,但这样大家都可能访问,不安全,下面提供一种基于soaphead的安全机制。2016-05-05如何传值在2个页面之间 要求不刷新父页面,并且不能用Querystring传值
通过Cookie,因为它既可以在服务器端对其进行操作,也可在客户端对其进行操作但是缺点是不安全,而且有时客户端会由于安全问题禁用Cookie!2008-12-12解决.net framework 4.0环境下遇到版本不同编译不通过的方法详解
本篇文章是对.net framework 4.0环境下遇到版本不同编译不通过的解决方法进行了详细的分析介绍,需要的朋友参考下2013-05-05
最新评论