.net core WebAPI性能监控-MiniProfiler与Swagger集成


------------恢复内容开始------------

安装Nuget

Install-Package MiniProfiler.AspNetCore.Mvc Install-Package MiniProfiler.EntityFrameworkCore

MiniProfiler.EntityFrameworkCore 用来监控EF Core生成的SQL

配置

在startup.cs 中配置服务ConfigureServices

services.AddMiniProfiler(options => {                 options.RouteBasePath = "/profiler";             }).AddEntityFramework();

激活中间件,启用MiniProfiler服务,放在UseEndpoints方法之前。

app.UseMiniProfiler();

配置Swagger页面

  1. 先下载自定义Swagger页面 https://github.com/xuke353/swaggerui/blob/master/index.html。
    将该文件放到API层的根目录下,设置文件属性为【嵌入的资源】

 

  1. 在Startup.cs文件中,我们需要修改UseSwaggerUI中间件的配置
app.UseSwaggerUI(c => {     c.IndexStream = () => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("AdmBoots.Api.index.html");     c.RoutePrefix = string.Empty;     c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); });

注意:这里AdmBoots.Api是项目的命名空间名
当前这个时候还不能使用,我们还需要在 Swagger的index.html中进行配置,以便它能在 Swagger 中使用。
重点来了
我们首先需要获取用于显示MiniProfiler的html代码片段,随便写个控制器,使用MiniProfiler的API输出一下就可以了。

[HttpGet] public IActionResult GetCounts() {     var html = MiniProfiler.Current.RenderIncludes(_accessor.HttpContext);      return Ok(html.Value);  }



------------恢复内容开始------------

安装Nuget

Install-Package MiniProfiler.AspNetCore.Mvc Install-Package MiniProfiler.EntityFrameworkCore

MiniProfiler.EntityFrameworkCore 用来监控EF Core生成的SQL

配置

在startup.cs 中配置服务ConfigureServices

services.AddMiniProfiler(options => {                 options.RouteBasePath = "/profiler";             }).AddEntityFramework();

激活中间件,启用MiniProfiler服务,放在UseEndpoints方法之前。

app.UseMiniProfiler();

配置Swagger页面

  1. 先下载自定义Swagger页面 https://github.com/xuke353/swaggerui/blob/master/index.html。
    将该文件放到API层的根目录下,设置文件属性为【嵌入的资源】

 

  2.在Startup.cs文件中,我们需要修改UseSwaggerUI中间件的配置

app.UseSwaggerUI(c => {     c.IndexStream = () => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("AdmBoots.Api.index.html");     c.RoutePrefix = string.Empty;     c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); });

注意:这里AdmBoots.Api是项目的命名空间名
当前这个时候还不能使用,我们还需要在 Swagger的index.html中进行配置,以便它能在 Swagger 中使用。
重点来了
我们首先需要获取用于显示MiniProfiler的html代码片段,随便写个控制器,使用MiniProfiler的API输出一下就可以了。

[HttpGet] public IActionResult GetCounts() {     var html = MiniProfiler.Current.RenderIncludes(_accessor.HttpContext);      return Ok(html.Value);  }

注意事项

  1. 不要在网上随便找个MiniProfiler的HTML代码片段就拷贝到index.html中使用,这样是不会成功的,因为拷贝来的的版本号和我们所引用Nuget的版本号并不一致。
  2. MiniProfiler.Current.RenderIncludes(_accessor.HttpContext)中的_accessor.HttpContext是通过依赖注入IHttpContextAccessor接口获取的。IHttpContextAccessor需要在Startup.cs中进行注册。

依赖注入



 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM