Using Loupe with an Entity Framework 6 Application 

If you're already using Loupe and Entity Framework 6, adding one assembly and one line of code will instantly provide you a wealth of diagnostic and performance info in test and production. 

Adding the Agent

There are only two things you need to do to get capturing Data:

  1. Add the Gibraltar Loupe Agent for EF6 Nuget package 
  2. Add one line of code to your project as shown below. 
//Register the Interceptor
Gibraltar.Agent.EntityFramework.LoupeCommandInterceptor.Register();

We recommend selecting your top-most project in the Default Project combo (e.g. your web site or application) to associate the agent with since it’ll pull in the Loupe Agent as well. 

Once you’ve added the Agent all you need to do is register it with Entity Framework so it will start listening to requests.  This is just a one line command which can be done any time in your application (and if called multiple   times is safe).

After this call is made any EF call in your application domain (that’s using EF 6, of course) will be recorded.

What’s in the Log?

Every EF call records at least two things – a performance metric and a log statement at the start of execution.  If the call throws an exception you’ll also get a message at the end with the result information (logged as a warning so it’ll show up as a Loupe Application Event).

Here’s an example of an insert run by Entity Framework as part of saving changes:

You can see that by default we record the full query, parameter values, information on the transaction (so on a busy system you can see what statements were really part of the same transaction, even if there  are multiple operations on the same thread concurrently) as well as the server and call stack that got you there.   You can turn off the call stack if you like, but it’s particularly useful when attempting to figure out why your database is getting spammed with unexpected queries. 

The agent works to attribute each database operation to the part of your source code that triggered the operation.  In the case of the example we’re looking at (right from our unit tests) we can see that it correctly attributed it to the save changes call in the unit test.  Since I have the source code on my machine I can even see the exact line.

Charting Performance

Since we’re recording metrics as well as logging queries we can use the metric graphing capability of Loupe Desktop to show us what queries are executed most frequently or taking the most time. 

To generate this chart, in the Log Viewer of Loupe Desktop:

  1. Click On New View
  2. Click on New Chart to select a charting view
  3. Drag the Database-Query metric onto the new view which will raise the Create Metric Chart dialog.
  4. Select to chart the Average Duration.  If you have a lot of database operations you may want to limit it to the top 10 or 20 values. 

And there you are!  No fuss, production-ready information on all of the database calls you’re making! 

And You Can Help!

If you’d like to see more, or even help out then check out the Open Source project on GitHub.  This is part of our push to make the source code for all of our platform agents available to you so if you’d like it to do something different you can fork it and make the change.  Even better, send us a pull request with your improvement and see your code shipped to the whole world as part of Loupe!