1. 新建Web API项目

  2. 配置Swagger,本项目中使用的swagger包如下

  3. Nuget安装MiniProfiler,目前最新版本是4.2.1,支持的.Net框架是4.6.1,注意不要选错

  4. 在Global.asax.cs中添加如下代码

protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);
    MiniProfiler.Configure(new MiniProfilerOptions
    {
        RouteBasePath = "~/mini-profiler-resources",
        PopupRenderPosition = RenderPosition.Right,  // defaults to left
        PopupMaxTracesToShow = 2,                   // defaults to 15
        ColorScheme = ColorScheme.Auto,              // defaults to light
        IgnoredPaths = { "/lib/","/css/","images"}

     });
}
protected void Application_BeginRequest()
{
    MiniProfiler.StartNew();
}

protected void Application_EndRequest()
{
    MiniProfiler.Current?.Stop();
}
  1. web.config当中添加如下代码(添加在节点下)
<add name="MiniProfiler" path="mini-profiler-resources/*" verb="*"
           type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified"
           preCondition="integratedMode" />
  1. 这时候在swagger当中调用API之后,再访问~/mini-profiler-resources/results路径就会看到如下结果

如何才能使监控结果直接显示在Swagger页面呢?别急,继续如下步骤

  1. 下载默认的index.html页面添加到项目当中,注意:需要将index.html设置为嵌入的资源 官方下载
版权声明:本文为cndota2原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/cndota2/p/13906006.html