您的位置:首页 > 运维架构

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

2012-02-23 17:29 351 查看

EngineContext.Current.Resolve<IConfigurationProvider<StoreInformationSettings>>()
.SaveSettings(newStoreInformationSettings()
{
StoreName="Yourstorename",
StoreUrl="http://www.yourStore.com/",
StoreClosed=false,
StoreClosedAllowForAdmins=false,
DefaultStoreTheme="DarkOrange",
AllowCustomerToSelectTheme=false,
DisplayMiniProfilerInPublicStore=false,
});


关于Mvc-Mini-Profiler

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

.适用与ADO.NET,LINQtoSQL.EF甚至EF-codefirst的性能监测

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

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

在Frontweb的_root.cshtml中

vardisplayMiniProfiler=EngineContext.Current.Resolve<Nop.Core.Domain.StoreInformationSettings>().DisplayMiniProfilerInPublicStore;
}

理论上,后台应该在商店信息部分设置是否要显示性能分析数据(这个对于分析网站的性能是有帮助的,但如何仅针对管理员还需要进一步讨论,例如写数据到Log,或者Table,而不是显示在Page上):

publicclassStoreInformationSettings:ISettings
{
publicstringStoreName{get;set;}
publicstringStoreUrl{get;set;}
publicboolStoreClosed{get;set;}
publicboolStoreClosedAllowForAdmins{get;set;}
publicstringDefaultStoreTheme{get;set;}
publicboolAllowCustomerToSelectTheme{get;set;}

publicboolDisplayMiniProfilerInPublicStore{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(newProfilingActionFilter());
}


在Global.asax中,Web程序EndRequest中,Profile功能的关闭:

protectedvoidApplication_EndRequest(objectsender,EventArgse)
{
if(DataSettingsHelper.DatabaseIsInstalled()&&
EngineContext.Current.Resolve<StoreInformationSettings>().DisplayMiniProfilerInPublicStore)
{
//stopasearlyasyoucan,evenearlierwithMvcMiniProfiler.MiniProfiler.Stop(discardResults:true);
MiniProfiler.Stop();
}
}

[/code]
使用步骤:

1.下载MVCMiniprofiler,添加引用

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

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

3.在Global.asax.csApplication_BeginRequest:

其实在这里可以增加逻辑判断,例如仅本地登陆启动Profile;特定用户Profile,NopCommerce在这个方面可以改进一下:

if(Request.IsLocal)
MvcMiniProfiler.MiniProfiler.Start();

。。。。。。。。

protectedvoidApplication_End()
{
MvcMiniProfiler.MiniProfiler.Stop();
}

在后台某个View-Controller中:

usingMvcMiniProfiler;

...

varprofiler=MiniProfiler.Current;//it'sokifthisisnull

using(profiler.Step("Setpagetitle"))

{

ViewBag.Title="HomePage";

}

using(profiler.Step("Doingcomplexstuff"))

{

using(profiler.Step("StepA"))

{//somethingmoreinterestinghere

Thread.Sleep(100);

}

using(profiler.Step("StepB"))

{//andhere

Thread.Sleep(250);

}

}

更多信息,请参考:
http://code.google.com/p/mvc-mini-profiler/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐