MiniProfiler 安装与设置

Installation

Installation is simple and is all done through the Package Manager Console integrated into Visual Studio. 

  1. Open the Package Manager Console (Tools -> Library Package Manager -> Package Manager Console.
  2. Type “Install-Package MiniProfiler” and press enter:
    MiniProfiler Package Manager
  3. Package Manager will now download and install MiniProfiler and add the required references to your project.
  4. As I mentioned previously, we’re going to be configuring MiniProfiler to profile our Entity Framework database context, in order to do so, we’ll also need the MiniProfiler.EF package, which is installed in exactly the same way, with
    “Install-Package MiniProfiler.EF”
  5. Now open your Global.asax.vb file, at the top of your Sub Application_Start, add:
    MiniProfilerEF.Initialize_EF42()
    This ensures that any connections created and used by Entity Framework are captured and logged into MiniProfiler like in my screenshot above.
  6. In the same file, add the following to the top of your Application_BeginRequest: 
    MiniProfiler.Start()
  7. And at the top of your Application_EndRequest: 
    MiniProfiler.Stop()

Almost done, I promise.  We now need to add a piece of code to the page you want to see the profiler on. Personally I have this on my Base.Master, the parent master page for all others, that way it’s available on every page.  The code you need to add, in your <head> tag is:

<%:MiniProfiler.RenderIncludes %> 这里如果不是MVC而是WebForm的话 请改成
<%= StackExchange.Profiling.MiniProfiler.RenderIncludes(useExistingjQuery:false) %>
webForm的话请放在页面的最下面(form下面)不然会报jquery引用错误,MVC暂时没试

You may need to add a reference to the namespace at the top of your page like so:

<%@ Import Namespace="StackExchange.Profiling" %>

And that’s it.  It’s worth noting that if MiniProfiler.Start() isn’t called, the .RenderIncludes function is smart enough not to clutter your browser with the JavaScript files that are required.  I personally wrap my .Start() in a Debugger.IsAttached statement, therefore I only show the profile in my local development environment.

Run your application and see what happens…

"Oh wait, Karl, you lied – it doesn’t work and I’m getting the following error when running MVC4"

Failed to load resource: the server responded with a status of 404 (Not Found) http://*/mini-profiler-resources/results

This article wouldn’t be interesting unless it solved at least one annoyance now, would it?  To fix the above error you need to add the following to your Web.Config

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

This sorts out the routing issue which appeared in MVC4.  Now try again, and you’ll be sorted!

Summary

MiniProfiler is a very powerful tool, I will go into the more advanced features of it in a later Article. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值