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:
- Using System. Web. MVC;
- Using System. Web. Optimization;
- Using System. Web. Routing;
- Using stackexchange. Profiling;
- Using stackexchange. Profiling. EntityFramework6;
- namespace Miniprofilerdemo
- {
- Public class mvcapplication : System. Web. HttpApplication
- {
- protected void Application_Start()
- {
- Arearegistration. Registerallareas();
- Filterconfig. Registerglobalfilters(globalfilters. Filters);
- Routeconfig. RegisterRoutes(routetable. Routes);
- Bundleconfig. Registerbundles(bundletable. Bundles);
- MiniProfilerEF6. Initialize();
- }
- protected void application_beginrequest()
- {
- Miniprofiler. Start();
- }
- protected void application_endrequest()
- {
- Miniprofiler. Stop();
- }
- }
- }
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.
- @StackExchange. Profiling. Miniprofiler. Renderincludes()
Such as:
3. Add code to Web. config
- <system.webServer>
- <add Name="Miniprofiler" path="mini-profiler-resources/*" verb= "*" type="System.Web.Routing.UrlRoutingModule"resourcetype="Unspecified " precondition="integratedmode " />
- </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:
- Using miniprofilerdemo. DAL;
- Using System. LINQ;
- Using System. Web. MVC;
- Using stackexchange. Profiling;
- Using System. Collections. Generic;
- Using miniprofilerdemo. Models;
- Namespace Miniprofilerdemo. Controllers
- {
- Public class productcontroller : Controller
- {
- Public actionresult Index()
- {
- Using (efdbcontext db = new efdbcontext())
- {
- var profiler = miniprofiler. Current;
- List<Product> m;
- Using (Profiler. Step("Get product List" )
- {
- M = db. Products. ToList();
- }
- return View(m);
- }
- }
- }
- }
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