原文如何在ASP.NET Core Web API中使用Mini Profiler
由Anuraj发表于2019年11月25日星期一阅读时间:1分钟
这篇文章是关于如何在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-list
JSON 处的所有请求列表中的所有请求的列表。
该
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
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.