Nop-Profiler的改進方向,通過Miniprofiler設置僅對某些條件下的訪問開放profiler trace

Nop-Profiler的改進方向,通過Miniprofiler設置僅對某些條件下的訪問開放profiler trace<七>

分類: Nop Commerce 9人閱讀 評論(0) 收藏 舉報

[csharp] view plaincopy
  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 plaincopy
  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/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章