性能工具 MiniProfilter

针对于.Net MVC+EF的开发模式,MiniProfilter提供了监控调试EF的功能,可以查看运行了那些sql,sql执行的时间等。

一、安装

vs打开NuGet的管理NuGet程序包,在线搜索MiniProfilter,如下图
这里写图片描述

安装完成后,在global文件中加上相应代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using StackExchange.Profiling;
using StackExchange.Profiling.EntityFramework6;

namespace MvcDemo
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            MiniProfilerEF6.Initialize();
        }

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

        protected void Application_EndRequest()
        {
            MiniProfiler.Stop();
        }

    }
}

start方法中添加Miniprofilter法eginRequest和EndRequest方
中添加监控起始代码。

二、配置

在web.config中的system.webServer节点中添加以下代码

<system.webServer>
    <handlers>
      <add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
    </handlers>
  </system.webServr>

然后在模板页添加渲染代码,如图

这里写图片描述
其中的参数是一个枚举类型用于定位监控显示
可以为空,默认定位左上角。运行网站效果如下图

三、监控具体方法

直接上代码

public ActionResult Index(string sortBy,string sortDesc,string searchString,int? page)
        {
            sortDesc = sortDesc ?? "Y";
            ViewBag.sortDesc = "Y".Equals(sortDesc) ? "N" : "Y";
            var miniProfilter = MiniProfiler.Current;
            using (miniProfilter.Step("获取用户列表"))
            {


                var users = from u in db.SysUsers select u;

                if (!string.IsNullOrEmpty(searchString))
                {
                    users = users.Where(u => u.UserName.Contains(searchString));
                }

                switch (sortDesc)
                {
                    case "Y":
                        users = users.OrderByDescending(u => u.UserName);
                        break;
                    case "N":
                        users = users.OrderBy(u => u.UserName);
                        break;
                }

                int pageSize = 2;
                int pageNumber = page ?? 1;

                return View(users.ToPagedList(pageNumber,pageSize));
            }
        }

Mark.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值