zoukankan      html  css  js  c++  java
  • [Asp.net mvc] 在Asp.net mvc 中使用MiniProfiler

    MiniProfilerStack Overflow团队设计的一款性能分析的小程序。可以对一个页面本身,及该页面通过直接引用、Ajax、Iframe形式访问的其它页面进行监控,监控内容包括数据库内容,并可以显示数据库访问的SQL(支持EF、EF CodeFirst等 ),并且以很友好的方式展现在页面上。官网链接:http://miniprofiler.com/
    今天使用到这个工具,记录下使用方法与填坑.(注:本文使用asp.net mvc示例)

    一.安装MiniProfiler

    在Visual Studio中可直接通过Nuget安装.
    命令行方式安装输入:Install-Package MiniProfiler
    图形界面安装直接搜索MiniProfiler即可

    二.配置

    [1].配置Global.asax.cs文件:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        if (Request.IsLocal)
        {
            MiniProfiler.Start();
        }
    }
    protected void Application_EndRequest(object sender, EventArgs e)
    {
        if (Request.IsLocal)
        {
            MiniProfiler.Stop();
        }
    }
    

    [2].Layout页面引用:

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>@ViewBag.Title</title>
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/bootstrap")
        @if (Request.IsLocal)
        {
            @StackExchange.Profiling.MiniProfiler.RenderIncludes()
        }
    </head>
    

    MiniProfiler.RenderIncludes()方法会在页面上生成一个脚本引用文件:

    <script async type="text/javascript" id="mini-profiler" 
        src="/mini-profiler-resources/includes.js?v=ySF6M98CBehTtL86BbiEmys9yxR1HKazhe2sznfdUWQ=" data-version="ySF6M98CBehTtL86BbiEmys9yxR1HKazhe2sznfdUWQ=" 
        data-path="/mini-profiler-resources/" data-current-id="dd06f423-464a-4c45-931c-96495a138813" data-ids="dd06f423-464a-4c45-931c-96495a138813" 
        data-position="left" data-trivial="false" data-children="false" data-max-traces="15" data-controls="false" data-authorized="true" 
        data-toggle-shortcut="Alt+P" data-start-hidden="false" data-trivial-milliseconds="2">
    </script>

    这个脚本会直接报404错误.所以要再配置下web.config,在system.webServer节点下增加:

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

    再刷新下页面就可以看到了,默认在页面左上角显示.附上成果图:


    三.监控EF的操作

    默认MiniProfiler是不会监控Sql的如果需要监控EntityFramework或其他方式生成的sql,执行的时间等还需要些其他的配置:
    首先根据EF的版本下载对应的MiniProfiler版本,这里我使用的EF6,如图:

    安装好设置下Global.asax.cs:

    MiniProfilerEF6.Initialize();
    

    注意:

    1.如果使用了数据库初始化工具要把这句代码放到初始化代码的前面,否则会报如下错误:

    在尝试添加“Loaded”事件处理程序前,实体框架已在使用一个 DbConfiguration 实例。在使用实体框架前,“Loaded”事件处理程序只能作为应用程序的一部分添加。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=260883。


    2.如果下载的MiniProfiler.EF(Obsolete)可能会报如下错误:

    无法将类型为“StackExchange.Profiling.Data.EFProfiledDbConnection”的对象强制转换为类型“System.Data.SqlClient.SqlConnection”。

    解决办法详细可参考重典的博客(点我跳转:)),也就是在初始化代码后添加这段代码:

     MiniProfilerEF.Initialize();
     System.Data.Entity.DbConfiguration.Loaded += (sender, e) =>  
            e.ReplaceService<System.Data.Entity.Core.Common.DbProviderServices>(
                 (services, o) => EFProfiledSqlClientDbProviderServices.Instance);
  • 相关阅读:
    Calling a parent window function from an iframe
    JSON with Java
    Posting array of JSON objects to MVC3 action method via jQuery ajax
    What's the difference between jquery.js and jquery.min.js?
    jquery loop on Json data using $.each
    jquery ui tabs详解(中文)
    DataTables warning requested unknown parameter
    Datatables 1.10.x在命名上与1.9.x
    jQuery 1.x and 2.x , which is better?
    DataTabless Add rows
  • 原文地址:https://www.cnblogs.com/summit7ca/p/5167257.html
Copyright © 2011-2022 走看看