Nop-Profiler的改进方向,通过Miniprofiler设置仅对某些条件下的访问开放profiler trace<七>

Nop-Profiler的改进方向,通过Miniprofiler设置仅对某些条件下的访问开放profiler trace<七>

分类: Nop Commerce   9人阅读  评论(0)  收藏  举报

[csharp]  view plain copy
  1. EngineContext.Current.Resolve<IConfigurationProvider<StoreInformationSettings>>()  
  2.                 .SaveSettings(new StoreInformationSettings()  
  3.                 {  
  4.                     StoreName = "Your store name",  
  5.                     StoreUrl = "http://www.yourStore.com/",  
  6.                     StoreClosed = false,  
  7.                     StoreClosedAllowForAdmins = false,  
  8.                     DefaultStoreTheme = "DarkOrange",  
  9.                     AllowCustomerToSelectTheme = false,  
  10.                     DisplayMiniProfilerInPublicStore = false,  
  11.                 });  

关于Mvc-Mini-Profiler

Mini-Profile的本意是用于Asp.NETMVC和Asp.nET程序Profile的简单工具,它本身不Hook到每一个线程,并不注重解决重要的Performance问题,相反:

. 适用与ADO.NET,LINQ to SQL.EF甚至EF-code first的性能监测

. 通过代码来Profile指定代码段的性能

我们来看一下Nop是怎么使用到Profiler的:

 

在Front web的_root.cshtml中

  var displayMiniProfiler = EngineContext.Current.Resolve<Nop.Core.Domain.StoreInformationSettings>().DisplayMiniProfilerInPublicStore; }
理论上,后台应该在商店信息部分设置是否要显示性能分析数据(这个对于分析网站的性能是有帮助的,但如何仅针对管理员还需要进一步讨论,例如写数据到Log,或者Table,而不是显示在Page上):
 public class StoreInformationSettings : ISettings     {          public string StoreName { get; set; }           public string StoreUrl { get; set; }          public bool StoreClosed { get; set; }          public bool StoreClosedAllowForAdmins { get; set; }           public string DefaultStoreTheme { get; set; }           public bool AllowCustomerToSelectTheme { get; set; }            public bool DisplayMiniProfilerInPublicStore { get; set; }     }
在InstallationService当中,我们DisplayMiniProfilerInPublicStore = false,
 
 
 
<head>中注入Miniprofile的脚本以及输出内容的CSS
 @if (displayMiniProfiler)     {         @MvcMiniProfiler.MiniProfiler.RenderIncludes();     }

 

在Global.asax.cs

  AreaRegistration.RegisterAllAreas();             if (DataSettingsHelper.DatabaseIsInstalled() &&                 EngineContext.Current.Resolve<StoreInformationSettings>().DisplayMiniProfilerInPublicStore)             {                 GlobalFilters.Filters.Add(new ProfilingActionFilter());             }

 

在Global.asax中,Web程序EndRequest中,Profile功能的关闭:
  
  
[csharp]  view plain copy
  1. protected void Application_EndRequest(object sender, EventArgs e)  
  2.  {  
  3.      if (DataSettingsHelper.DatabaseIsInstalled() &&  
  4.          EngineContext.Current.Resolve<StoreInformationSettings>().DisplayMiniProfilerInPublicStore)  
  5.      {  
  6.          //stop as early as you can, even earlier with MvcMiniProfiler.MiniProfiler.Stop(discardResults: true);  
  7.          MiniProfiler.Stop();  
  8.      }  
  9.  }  

使用步骤:

1.  下载MVC Mini profiler,添加引用

2.  一般而言,选择在Layout root文件中Head增加

 @MvcMiniProfiler.MiniProfiler.RenderIncludes() </head>

3.  在Global.asax.cs Application_BeginRequest:

 

其实在这里可以增加逻辑判断,例如仅本地登陆启动Profile;特定用户Profile,Nop Commerce在这个方面可以改进一下:
  if (Request.IsLocal)                 MvcMiniProfiler.MiniProfiler.Start(); 

 。。。。 。。。。

 protected void Application_End()                     {                         MvcMiniProfiler.MiniProfiler.Stop();          }

在后台某个View-Controller中:

using MvcMiniProfiler; 
 
... 
 
var profiler = MiniProfiler.Current; // it's ok if this is null 
 
using (profiler.Step("Set page title")) 

    ViewBag.Title = "Home Page"; 

 
using (profiler.Step("Doing complexstuff")) 

    using (profiler.Step("Step A")) 
    { // something more interestinghere 
        Thread.Sleep(100); 
    } 
    using (profiler.Step("Step B")) 
    { // and here 
        Thread.Sleep(250); 
    } 
}

 

 

更多信息,请参考:

http://code.google.com/p/mvc-mini-profiler/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值