web应用程序的性能相信是大家普遍关心的一个问题,也相信大家有很多工具可用来分析应用程序的性能并能够找到其中的瓶颈,MiniProfiler 就是这个领域中的一款产品,它是一款简单的,功能强大的web应用分析工具,MiniProfiler 可用来帮助我们找到 慢查询, 慢响应 等问题。

MiniProfiler 可用在 Asp.NetASP.Net Core 中,这篇文章将会讨论如何使用 MiniProfiler,并通过它找到应用程序的性能问题。

安装 MiniProfiler

要想使用 MiniProfiler,需要通过 nuget 引用 MiniProfiler.AspNetCore.Mvc 包,可以通过 Visual Studio 2019 的 NuGet package manager 可视化界面安装 或者 通过 NuGet package manager 命令行工具输入以下命令:


dotnet add package MiniProfiler.AspNetCore.Mvc

安装好之后,接下来就要将 MiniProfiler 注入到 ServiceCollection 容器中,如下代码所示:


// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews(); services.AddMiniProfiler(options => options.RouteBasePath = "/profiler");
}

注入好之后,接下来就需要使用 UseMiniProfiler 扩展方法将其注入到 Request Pipeline 管道中,如下代码所示:


public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
app.UseMiniProfiler(); app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}

然后在 _Layout.cshtml 页面中增加如下两行命令。


@using StackExchange.Profiling
@addTagHelper *, MiniProfiler.AspNetCore.Mvc

最后需要在 WebPage 中指定 MiniProfiler 分析窗口应该显示的位置,那如何做呢? 在 body 标签内使用 mini-profiler 标记,如下代码所示:


<mini-profiler position="@RenderPosition.Right" max-traces="5" />

在 ASP.Net Core MVC 中使用 MiniProfiler

MiniProfiler 会提供 页面加载时间数据库查询性能指标,接下来把程序跑起来,你会看到如下的性能指标图。

有些朋友可能就要问了,大体时间我是知道了,那如果我只想获取某一指定代码块的执行时间呢? 当然也是可以的,下面的代码展示了如何去实现。


public class HomeController : Controller
{
ILogger<HomeController> logger; public HomeController(ILogger<HomeController> logger)
{
this.logger = logger;
} public IActionResult Index()
{
var miniProfiler = MiniProfiler.Current;
List<Author> authors = new List<Author>(); miniProfiler.RenderIncludes(this.HttpContext); using (miniProfiler.Step("Get Authors"))
{
authors.Add(new Author() { Id = 1, FirstName = "Joydip", LastName = "Kanjilal", Address = "Hyderabad, India" });
authors.Add(new Author() { Id = 2, FirstName = "Stephen", LastName = "Smith", Address = "NY, USA" });
authors.Add(new Author() { Id = 3, FirstName = "Anand", LastName = "Narayanan", Address = "Chennai, India" });
authors.Add(new Author() { Id = 4, FirstName = "Steve", LastName = "Jones", Address = "London, UK" });
}
return View(authors);
}
} public class Author
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
}

从上面的代码中可以看到,我用 using (miniProfiler.Step("Get Authors")) 做了语句块标记,理论上 mini-profile 窗口上应该有类似 Get Authors 指标栏,接下来把程序跑起来,一起来看看效果。

除了顺向操作,你也可以指定让某些代码块不要显示在 mini-profile 中,需要做的是调用 Ignore() 即可,如下代码所示:


using (MiniProfiler.Current.Ignore())
{
// Write code here that you don't
// want MiniProfiler to profile
}

使用 MiniProfile 分析 ADO.NET 查询

除了做一些常规的页面分析,还可以直接对 ADO.NET 查询性能进行分析,这就

如何在 ASP.Net Core 中使用 MiniProfiler的更多相关文章

  1. 如何在ASP.NET Core中实现CORS跨域

    注:下载本文的完整代码示例请访问 > How to enable CORS(Cross-origin resource sharing) in ASP.NET Core 如何在ASP.NET C ...

  2. 如何在ASP.NET Core中实现一个基础的身份认证

    注:本文提到的代码示例下载地址> How to achieve a basic authorization in ASP.NET Core 如何在ASP.NET Core中实现一个基础的身份认证 ...

  3. 如何在ASP.NET Core中应用Entity Framework

    注:本文提到的代码示例下载地址> How to using Entity Framework DB first in ASP.NET Core 如何在ASP.NET Core中应用Entity ...

  4. [转]如何在ASP.NET Core中实现一个基础的身份认证

    本文转自:http://www.cnblogs.com/onecodeonescript/p/6015512.html 注:本文提到的代码示例下载地址> How to achieve a bas ...

  5. 如何在ASP.NET Core中使用Azure Service Bus Queue

    原文:USING AZURE SERVICE BUS QUEUES WITH ASP.NET CORE SERVICES 作者:damienbod 译文:如何在ASP.NET Core中使用Azure ...

  6. 如何在ASP.NET Core中自定义Azure Storage File Provider

    文章标题:如何在ASP.NET Core中自定义Azure Storage File Provider 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun/p ...

  7. 如何在ASP.NET Core中使用JSON Patch

    原文: JSON Patch With ASP.NET Core 作者:.NET Core Tutorials 译文:如何在ASP.NET Core中使用JSON Patch 地址:https://w ...

  8. [翻译] 如何在 ASP.Net Core 中使用 Consul 来存储配置

    [翻译] 如何在 ASP.Net Core 中使用 Consul 来存储配置 原文: USING CONSUL FOR STORING THE CONFIGURATION IN ASP.NET COR ...

  9. [译]如何在ASP.NET Core中实现面向切面编程(AOP)

    原文地址:ASPECT ORIENTED PROGRAMMING USING PROXIES IN ASP.NET CORE 原文作者:ZANID HAYTAM 译文地址:如何在ASP.NET Cor ...

随机推荐

  1. Python内置模块(你还在pip install time?)&& apt-get install -f

    一.内置模块 之前不知道time是python自带的,还用pip安装.......还报错..... Python中有以下模块不用单独安装 1.random模块 2.sys模块 3.time模块 4.o ...

  2. 在.NET中体验GraphQL

    前言 以前需要提供Web服务接口的时候,除了标准的WEBAPI形式,还考虑了OData.GraphQL等形式,虽然实现思路上有很大的区别,但对使用方来说,都是将查询的主动权让渡给了前端,让调用方能够更 ...

  3. nginx+lua实现灰度发布/waf防火墙

    nginx+lua 实现灰度发布 waf防火墙 课程链接:[课程]Nginx 与 Lua 实现灰度发布与 WAF 防火墙(完)_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili 参考博客 Nginx ...

  4. K8S(15)监控实战-ELK收集K8S内应用日志

    K8S监控实战-ELK收集K8S内应用日志 目录 K8S监控实战-ELK收集K8S内应用日志 1 收集K8S日志方案 1.1 传统ELk模型缺点: 1.2 K8s容器日志收集模型 2 制作tomcat ...

  5. 设计模式(二十二)——状态模式(APP抽奖活动+借贷平台源码剖析)

    24.1 APP 抽奖活动问题 请编写程序完成 APP 抽奖活动 具体要求如下: 1) 假如每参加一次这个活动要扣除用户 50 积分,中奖概率是 10% 2) 奖品数量固定,抽完就不能抽奖 3) 活动 ...

  6. CodeForces 348D Turtles(LGV定理)题解

    题意:两只乌龟从1 1走到n m,只能走没有'#'的位置,问你两只乌龟走的时候不见面的路径走法有几种 思路:LGV定理模板.但是定理中只能从n个不同起点走向n个不同终点,那么需要转化.显然必有一只从1 ...

  7. Apple 订单系统 bug

    Apple 订单系统 bug 看不到最近的购买信息 https://secure1.www.apple.com.cn/shop/order/list refs xgqfrms 2012-2020 ww ...

  8. WebSocket All In One

    WebSocket All In One WebSocket heartbeat WebSocket 心跳检测 ping pong refs xgqfrms 2012-2020 www.cnblogs ...

  9. uname -a

    uname -a Linux shell command https://en.wikipedia.org/wiki/Uname#:~:text=uname $ uname # Darwin $ un ...

  10. vue & modal props & form data update bug

    vue & modal props & form data update bug OK <div> <BindModal :dialogBindVisible=&qu ...