使用性能优化工具 MVC Mini Profiler (MVC3+EF4.1)

2019-09-10 18:39:57 浏览数 (1)

安装:1,

2,运行命令 PM> Install-Package MiniProfiler

3,因为我这里使用的是Entity framework 4.1 code first 所以还需要下载 一个包PM> Install-Package MiniProfiler.EF

4,使用说明,查看连接: http://miniprofiler.com/

5,我在MVC3 E.F4.1中的使用

代码语言:javascript复制
View Code 
 1         public ActionResult Index(int pindex = 0)
 2         {
 3             try
 4             {
 5                 var profiler = MiniProfiler.Current; // it's ok if this is null
 6                 using (profiler.Step("Test Bless page"))
 7                 { // something more interesting here
 8                     ViewBag.BlessMessageList = GetBlessingStr(pindex);
 9                     Thread.Sleep(100);
10 
11                 }
12             }
13             catch (Exception ex)
14             {
15                 #region 操作失败发送邮件给管理员
16                 EmailServer send = new EmailServer();
17                 string body = "祭奠在线网站祈福墙出现错误,错误信息: "   ex.Message;
18                 //这个是附件列表
19                 List<string> list = new List<string>();
20                 string reciveMail = StringHelper.GetReceiveMail();
21                 string CcEmailList = StringHelper.GetCCMailList();
22                 send.Send(reciveMail, "", CcEmailList, "祭奠在线网站报错邮件", body, list);
23                 #endregion
24                 //并记录错误日志
25                 Log.WriteLog("网站祈福墙信息", "/Log/Blessing/", ex.Message);
26                 //strJson = "系统正在升级,请稍候再试!";
27             }
28             return View();
29

6,Global文件中

代码语言:javascript复制
View Code 
 1         protected void Application_Start()
 2         {
 3             AreaRegistration.RegisterAllAreas();
 4 
 5             RegisterGlobalFilters(GlobalFilters.Filters);
 6             RegisterRoutes(RouteTable.Routes);
 7             //启动性能调试工具---Created by isaac on 2012-04-28
 8             //Note, that EF 4.1 Update 1 (the version currently on NuGet) has a breaking change which throws the following error when specifying a connection string:
 9             //The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
10             //MiniProfiler attempts to resolve this issue by detecting the version of Entity Framework that it is running against. If this doesn't work (due to security exceptions), force the hack to be applied by replacing the Initialize() call with: 
11 
12             //MiniProfilerEF.Initialize_EF42(); // in Application_Start
13             MiniProfilerEF.Initialize(); //调用EF-CODE FIRST要下载包Install-Package MiniProfiler.EF
14             
15         }
16         protected void Application_BeginRequest()
17         {
18             //判断是否是本地操作
19             if (Request.IsLocal)
20             {
21                 MiniProfiler.Start();
22             }
23         }
24         protected void Application_EndRequest()
25         {
26             //停止性能工具
27             MiniProfiler.Stop();
28

7,结果

0 人点赞