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

本文详细介绍了MvcMiniProfiler的使用方法及配置流程,包括在Asp.NET MVC项目中集成性能分析工具的具体步骤。
部署运行你感兴趣的模型镜像

EngineContext.Current.Resolve<IConfigurationProvider<StoreInformationSettings>>()
                .SaveSettings(new StoreInformationSettings()
                {
                    StoreName = "Your store name",
                    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,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功能的关闭: 
       protected void Application_EndRequest(object sender, EventArgs e)
        {
            if (DataSettingsHelper.DatabaseIsInstalled() &&
                EngineContext.Current.Resolve<StoreInformationSettings>().DisplayMiniProfilerInPublicStore)
            {
                //stop as early as you can, even earlier with MvcMiniProfiler.MiniProfiler.Stop(discardResults: true);
                MiniProfiler.Stop();
            }
        }

使用步骤:

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/

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

### 修复 Maven 项目中 SLF4J 报错 Failed to load class StaticLoggerBinder 当 Maven 项目中出现 `SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"` 错误时,表明项目缺少 SLF4J 的底层日志实现绑定,导致其无法正常工作,只能默认使用无操作(NOP)日志实现 [^1]。 SLF4J 本身是一个日志门面,不提供具体的日志实现,因此需要绑定一个实际的日志框架(如 Logback、Log4j 等)来正常工作。如果未正确配置绑定,将会默认使用 NOP 日志实现,导致日志无法输出 [^2]。 #### 使用 Log4j 作为日志实现 如果希望使用 Log4j 作为底层日志框架,可以在 `pom.xml` 中添加以下依赖: ```xml <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> </dependency> ``` 该依赖会引入 SLF4J 对 Log4j 的绑定支持,并提供 `StaticLoggerBinder` 实现,从而解决无法加载类的问题 [^4]。 #### 使用 NOP 作为占位实现 如果暂时不需要日志输出,或者希望避免日志相关的错误信息,可以使用 `slf4j-nop` 作为占位实现。它会将所有日志调用静默处理,避免运行时错误: ```xml <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>1.8.0-alpha2</version> </dependency> ``` 此依赖适用于测试或轻量级部署场景,但不适用于需要实际日志记录的生产环境 [^3]。 #### 检查依赖版本兼容性 在引入依赖时,需要注意版本兼容性问题。如果多个 SLF4J 绑定依赖同时存在,可能会导致冲突。建议使用 `mvn dependency:tree` 或 `gradle dependencies` 命令检查依赖树,确保只有一个 SLF4J 实现绑定存在 。 #### 手动移除冲突依赖 如果项目中存在多个 SLF4J 实现(例如同时引入了 `slf4j-log4j12` 和 `slf4j-simple`),会导致 `StaticLoggerBinder` 类冲突,从而出现错误。可以通过排除冲突依赖的方式解决: ```xml <dependency> <groupId>some.library</groupId> <artifactId>some-artifact</artifactId> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> </exclusion> </exclusions> </dependency> ``` 通过这种方式,可以确保 SLF4J 使用指定的实现类。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值