MVC Tutorial--miniprofiler.ef Monitoring debug MVC and EF Performance

Source: Internet
Author: User

The previous article talked about the EF output in MVC to execute SQL logs, to talk about debugging and performance monitoring with MVC development projects. The EF framework automatically generates SQL statements for me, and when our program encounters performance problems we can use MINIPROFILER.EF to monitor the performance of MVC and EF, see the generated SQL statements, what SQL is running, and how long it takes. Miniprofiler.ef, a lightweight, open-source MVC performance Debug, monitoring component Miniprofiler a custom-built version for EF. Here's a specific example of how to debug MVC and EF Performance with MINIPROFILER.EF monitoring in our project.

1, installation Miniprofiler

Enter Miniprofiler in the NuGet search box and the following results will appear:

Click Install to add Miniprofiler related DLLs to the project. Install MINIPROFILER.MVC4,MINIPROFILER.EF5 (of course, the EF version used for the project; It seems only possible to use it after ef4+)

2, add the configuration related code into the project

1, in Global.asax to join the Miniprofiler related monitoring code

The complete content after the modification is:

  1. Using System. Web. MVC;
  2. Using System. Web. Optimization;
  3. Using System. Web. Routing;
  4. Using stackexchange. Profiling;
  5. Using stackexchange. Profiling. EntityFramework6;
  6. namespace Miniprofilerdemo
  7. {
  8. Public class mvcapplication : System. Web. HttpApplication
  9. {
  10. protected void Application_Start()
  11. {
  12. Arearegistration. Registerallareas();
  13. Filterconfig. Registerglobalfilters(globalfilters. Filters);
  14. Routeconfig. RegisterRoutes(routetable. Routes);
  15. Bundleconfig. Registerbundles(bundletable. Bundles);
  16. MiniProfilerEF6. Initialize();
  17. }
  18. protected void application_beginrequest()
  19. {
  20. Miniprofiler. Start();
  21. }
  22. protected void application_endrequest()
  23. {
  24. Miniprofiler. Stop();
  25. }
  26. }
  27. }

Among them is the addition of miniprofileref.initialize () in Application_Start and the addition of Application_BeginRequest, Application_ BeginRequest two application event functions, the function of which is to initialize MiniProfilerEF6 and start, end miniprofiler monitoring.

2. Modify the _layout.cshtml view file

Add a piece of code to the body of the views\shared\_layout.cshtml file to let the monitor appear on the page.

    1. @StackExchange. Profiling. Miniprofiler. Renderincludes()

Such as:

3. Add code to Web. config

  1. <system.webServer>
  2. <add Name="Miniprofiler" path="mini-profiler-resources/*" verb= "*" type="System.Web.Routing.UrlRoutingModule"resourcetype="Unspecified " precondition="integratedmode " />
  3. </system.webServer>

In order to show the debug trace time of MVC and EF on the page you have to add the above code. Such as:

In the Handlers node under the System.webserver configuration node, a handler named Miniprofiler is added.

3, view the running results run the program, view the page such as:

You can see the left corner of a number of chunks, indicating the number of milliseconds spent on this page, click on the number above to pop up detailed time tracking information, and you can see the SQL statement (shown in red needs to be optimized):

4, micro-monitoring method internal time now we add monitoring to Productcontroller, which monitors the time it takes to get the product record from the database. We changed the Productcontroller to:
  1. Using miniprofilerdemo. DAL;
  2. Using System. LINQ;
  3. Using System. Web. MVC;
  4. Using stackexchange. Profiling;
  5. Using System. Collections. Generic;
  6. Using miniprofilerdemo. Models;
  7. Namespace Miniprofilerdemo. Controllers
  8. {
  9. Public class productcontroller : Controller
  10. {
  11. Public actionresult Index()
  12. {
  13. Using (efdbcontext db = new efdbcontext())
  14. {
  15. var profiler = miniprofiler. Current;
  16. List<Product> m;
  17. Using (Profiler. Step("Get product List" )
  18. {
  19. M = db. Products. ToList();
  20. }
  21. return View(m);
  22. }
  23. }
  24. }
  25. }

Rebuild the project and run the view page again, such as:

You can see more of the "Get product List" record that we just added manually. This allows for subtle monitoring of the internal time of the method, which helps us find the bottleneck of our program quickly and easily.

MVC Tutorial--miniprofiler.ef Monitoring debug MVC and EF Performance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.