原文如何在ASP.NET Core Web API中使用Mini Profiler

Anuraj发表于2019年11月25日星期一阅读时间:1分钟

ASPNETCoreMiniProfiler

这篇文章是关于如何在ASP.NET Core Web API中配置Mini Profiler。MiniProfiler是用于对应用程序进行性能分析的库和UI。MiniProfiler可帮助您评估应用程序的性能。使用Entity Framework扩展,您将能够衡量查询性能。”

首先,您需要安装MiniProfiler软件包和MiniProfiler Entity Framework软件包。(我假设您已经创建了一个ASP.NET Core Web API项目,如果没有先创建的话。)

dotnet add package MiniProfiler.AspNetCore.Mvc --version 4.1.0
dotnet add package MiniProfiler.EntityFrameworkCore --version 4.1.0

安装后,startup.cs按如下所示修改您的代码。

public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddEntityFrameworkSqlite().AddDbContext<DatabaseContext>();
services.AddMiniProfiler(options => options.RouteBasePath = "/profiler").AddEntityFramework();
services.AddControllers();
}

在上面的代码中,我们添加了用于Web API和实体框架的Profiler。配置完RouteBasePath属性后,我们就可以访问/profiler/results-index,处的当前请求/profiler/results以及/profiler/results-listJSON 处的所有请求列表中的所有请求的列表。

services.AddMemoryCache();代码是必需的-MiniProfiler中存在一个错误,如果我们尚未配置MemoryCache,它将失败。

接下来,我们需要添加MiniProfiler中间件,您可以这样做。

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseMiniProfiler();
/* Code removed for brevity. */
}

现在,您已经完成了ASP.NET Core Web API中Mini Profiler的配置。现在运行该应用程序,您将能够看到这样的结果。

如果要对自己的代码进行性能分析,则可以使用该MiniProfiler.Current对象,如下所示。

public IActionResult Put([FromRoute]int id, [FromBody]WeatherForecast weatherForecast)
{
using (MiniProfiler.Current.Step("PUT method"))
{
WeatherForecast weatherForecastById = null;
using (MiniProfiler.Current.Step("Getting Weather Forecase for the Id"))
{
weatherForecastById = GetWeatherForecast(id);
} if (weatherForecastById == null)
{
return NotFound();
} if (weatherForecastById.Id != id)
{
return BadRequest();
}
using (MiniProfiler.Current.Step("Updating the Data"))
{
_databaseContext.Entry(weatherForecast).State = EntityState.Modified;
_databaseContext.SaveChanges();
}
return NoContent();
}
}

这将帮助您识别代码中的问题并进行故障排除。这是有关此方法的配置文件信息的屏幕截图。

MiniProfiler可帮助您分析ASP.NET Core Web应用程序代码以及实体框架代码。它支持不同的数据库提供程序和扩展。MiniProfiler还带有许多扩展方法,这些方法无需编写自己的代码即可帮助分析代码。

快乐编程:)

你怎么看?我想在下面的评论部分中听到您的想法,建议和问题。


类似帖子

------------------------------------------------------原文为英文版-----------------------------------------------------------------

Posted by Anuraj on Monday, November 25, 2019 Reading time :1 minute

ASPNETCore MiniProfiler

This post is about how to configure Mini Profiler in ASP.NET Core Web API. MiniProfiler is a library and UI for profiling your application. MiniProfiler helps you to measure perfomance of your applications. With Entity Framework extension you will be able to measure query performance.”

First you need to install the MiniProfiler package and MiniProfiler Entity Framework package. (I assume you already created an ASP.NET Core Web API project, if not create it first.)

dotnet add package MiniProfiler.AspNetCore.Mvc --version 4.1.0
dotnet add package MiniProfiler.EntityFrameworkCore --version 4.1.0

Once installed, modify your startup.cs code like the following.

public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddEntityFrameworkSqlite().AddDbContext<DatabaseContext>();
services.AddMiniProfiler(options => options.RouteBasePath = "/profiler").AddEntityFramework();
services.AddControllers();
}

In the above code we are adding the Profiler for Web API and Entity Framework. Once you configure the RouteBasePath property, we are able access a list of all requests at /profiler/results-index, the current request at /profiler/results and at /profiler/results-list a list of all requests as JSON.

The services.AddMemoryCache(); code is required - there is a bug in MiniProfiler, if we have not configured MemoryCache, it will fail.

Next we need add the MiniProfiler middleware, you can do like this.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseMiniProfiler();
/* Code removed for brevity. */
}

Now you have completed the configuration of Mini Profiler in ASP.NET Core Web API. Now run the application, you will be able to see the results like this.

If you want to your own code profiling you can use the MiniProfiler.Current object, like this.

public IActionResult Put([FromRoute]int id, [FromBody]WeatherForecast weatherForecast)
{
using (MiniProfiler.Current.Step("PUT method"))
{
WeatherForecast weatherForecastById = null;
using (MiniProfiler.Current.Step("Getting Weather Forecase for the Id"))
{
weatherForecastById = GetWeatherForecast(id);
} if (weatherForecastById == null)
{
return NotFound();
} if (weatherForecastById.Id != id)
{
return BadRequest();
}
using (MiniProfiler.Current.Step("Updating the Data"))
{
_databaseContext.Entry(weatherForecast).State = EntityState.Modified;
_databaseContext.SaveChanges();
}
return NoContent();
}
}

This will help you to identify and troubleshoot problems on the code. Here is the screenshot of the profile information about this method.

MiniProfiler helps you to Profile ASP.NET Core Web Application code as well as the Entity Framework Code. It supports different Database providers and extensions. MiniProfiler also comes with lot of extension methods which helps to profile code without writing your own.

Happy Programming :)

What do you think? I would like to hear your thoughts, suggestions, and questions in the comments section below.


如何在ASP.NET Core Web API中使用Mini Profiler的更多相关文章

  1. 在ASP.NET Core Web API中为RESTful服务增加对HAL的支持

    HAL(Hypertext Application Language,超文本应用语言)是一种RESTful API的数据格式风格,为RESTful API的设计提供了接口规范,同时也降低了客户端与服务 ...

  2. 在 ASP.NET Core Web API中使用 Polly 构建弹性容错的微服务

    在 ASP.NET Core Web API中使用 Polly 构建弹性容错的微服务 https://procodeguide.com/programming/polly-in-aspnet-core ...

  3. [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了

    [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 本文首发自:博客园 文章地址: https://www.cnblogs.com/yilezhu/p/ ...

  4. C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志

    C#实现多级子目录Zip压缩解压实例 参考 https://blog.csdn.net/lki_suidongdong/article/details/20942977 重点: 实现多级子目录的压缩, ...

  5. ASP.NET Core Web API中使用Swagger

    本节导航 Swagger介绍 在ASP.NET CORE 中的使用swagger   在软件开发中,管理和测试API是一件重要而富有挑战性的工作.在我之前的文章<研发团队,请管好你的API文档& ...

  6. ASP.NET Core Web API中带有刷新令牌的JWT身份验证流程

    ASP.NET Core Web API中带有刷新令牌的JWT身份验证流程 翻译自:地址 在今年年初,我整理了有关将JWT身份验证与ASP.NET Core Web API和Angular一起使用的详 ...

  7. 如何在ASP.NET Core Web API测试中使用Postman

    使用Postman进行手动测试 如果您是开发人员,测试人员或管理人员,则在构建和使用应用程序时,有时了解各种API方法可能是一个挑战. 使用带有.NET Core的Postman为您的Web API生 ...

  8. 翻译一篇英文文章,主要是给自己看的——在ASP.NET Core Web Api中如何刷新token

    原文地址 :https://www.blinkingcaret.com/2018/05/30/refresh-tokens-in-asp-net-core-web-api/ 先申明,本人英语太菜,每次 ...

  9. ASP.NET Core Web API中实现全局异常捕获与处理

    处理全局异常 HANDLING ERRORS GLOBALLY 在上面的示例中,我们的 action 内部有一个 try-catch 代码块.这一点很重要,我们需要在我们的 action 方法体中处理 ...

随机推荐

  1. 高并发&高可用系统的常见应对策略

    解耦神器:MQ MQ是分布式架构中的解耦神器,应用非常普遍.有些分布式事务也是利用MQ来做的.由于其高吞吐量,在一些业务比较复杂的情况,可以先做基本的数据验证,然后将数据放入MQ,由消费者异步去处理后 ...

  2. Linux服务器安全加固

    关于对公司网站服务器安全加固的一些想法及思路: 一.修改密码和ssh登录端口,并且尽可能的用密钥对登录,禁止用密码登录(主要针对Linux)二.修改/etc/hosts.allow 设置仅仅允许某几台 ...

  3. 目标检测论文解读3——Fast R-CNN

    背景 deep ConvNet兴起,VGG16应用在图像分类任务上表现良好,本文用VGG16来解决检测任务.SPP NET存在CNN层不能fine tuning的缺点,且之前的方法训练都是分为多个阶段 ...

  4. gitlab 提示:remote: The project you were looking for could not be found.

    解决: git remote remove origin git remote add origin https://your_git_user_name@git.qutoutiao.net/your ...

  5. 【C#】身份证号正确性验证及正则表达式格式验证

    身份证前两位各省对应的编号是: 1.华北地区:北京市|11,天津市|12,河北省|13,山西省|14,内蒙古自治区|15: 2.东北地区: 辽宁省|21,吉林省|22,黑龙江省|23: 3.华东地区: ...

  6. python基础之八:知识小结及补充

    一.python2 与python3 的区别 1.在2中print后可带扣号,也可不带,3中必须带,否则报错! #print 'hello python2' print('hello python3' ...

  7. Windbg Watch(监视)窗口的使用

    在WinDbg中,可以使用“监视”窗口显示和更改全局和局部变量和寄存器信息. 您可以自定义此窗口以显示您正在跟踪的项..“监视”窗口可以显示所需的任何变量列表.这些变量可以包括来自任何函数的全局变量和 ...

  8. Django Flatpage设置

    参考链接:https://docs.djangoproject.com/en/1.11/ref/contrib/flatpages/ 知识点: 通过django.site数据库记录表,对应域名和请求站 ...

  9. C语言中宏的相关知识

    2019/04/27 16:02 1.宏的定义:宏定义就是预处理命令的一种,它允许用一个标识符来表示一个字符串.格式如下: #define name(宏名) stuff(字符串) 本质就是使用宏名去替 ...

  10. 解决github打不开

    今天重庆电信的“临时工”把github废了. 主要是github.githubassets.com和customer-stories-feed.github.com访问不到 通过修改host的方式上g ...