zoukankan      html  css  js  c++  java
  • 在WebApi项目里使用MiniProfiler并且分析 Entity Framework Core

    在WebApi项目里使用MiniProfiler并且分析 Entity Framework Core

    一、安装配置MiniProfiler

    在现有的ASP.NET Core MVC WebApi 项目里,通过Nuget安装MiniProfiler

    Install-Package MiniProfiler.AspNetCore.Mvc MiniProfiler.EntityFrameworkCore

    当然也可以通过Nuget Package Manager可视化工具安装

    11.png

    接下来就是如何配置和使用 MiniProfiler 了,总共分三步:

    第一步,来到Startup.csConfigureServices方法里,添加services.AddMiniProfiler();

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<DataContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DataContext")));
    
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    
            // 首先添加一个配置选项,用于访问分析结果:
            services.AddMiniProfiler(options =>
            {
                // 设定弹出窗口的位置是左下角
                options.PopupRenderPosition = RenderPosition.BottomLeft;
                // 设定在弹出的明细窗口里会显式Time With Children这列
                options.PopupShowTimeWithChildren = true;
                // 设定访问分析结果URL的路由基地址
                options.RouteBasePath = "/profiler";
            })
            // 然后在之前的配置后边加上AddEntityFramework():
            .AddEntityFramework();
        }
    

    第二步,来到来到Startup.csConfigure方法里,添加app.UseMiniProfiler();

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            ...
    
            // 最重要的一点是就是配置中间件在管道中的位置,一定要把它放在UseMvc()方法之前。 
            app.UseMiniProfiler();
    
            app.UseMvc();
        }
    

    第三步、运行程序,一共有3个可查看分析结果相关的URL地址:

    1./profiler/results-index

    • 先看results-index页面:

    12.png

    它表示每次调用API的记录结果。可以看到本次调用API的总时间为1578.4毫秒。

    2./profiler/results

    • 从result-index页面点击链接进入这次API调用的详细结果页面,也就是result页面:

    13.png

    它表示每次调用API的过程分析结果,具体到每一条SQL语句的内容和执行时间。

    3./profiler/results-list

    • 再看result-list页面:

    14.png

    它其实就表示每个API的所有调用记录结果的集合。

    三、案例源码:

    MiniProfilerCoreWebApiDemo
  • 相关阅读:
    关于MySQL死锁
    随手一记,maven打包
    js生成带logo的二维码
    java生成带logo的二维码
    关于网页中文本域高度自动适应问题,参考微信回复
    从git上拉下来的严选weex项目demo
    补装老版本的Java SE
    新MBP使用git命令时启用xcode的终端log
    电脑出现“损坏的图像”窗口提示dll没有被指定在Windows上运行如何解决
    不同浏览器隐藏默认表单样式
  • 原文地址:https://www.cnblogs.com/Run2948/p/11247857.html
Copyright © 2011-2022 走看看