Chat Support
Loupe - Log - Monitor - Resolve
Loupe / Developer's Guide / For .NET Core / 6 / 8 / Developer's Guide - Entity Framework Core
In This Topic
    Developer's Guide - Entity Framework Core
    In This Topic

    If you're using Entity Framework Core you'll want to add the Loupe Agent for Entity Framework Core which automatically records log entries and metrics for EF actions. 

    This agent is open source and hosted on GitHub.  If you'd like to see it do something more/better/different or just curious how it works you can get the entire source code and even give the team a pull request or just log an issue.
     What Gets Recorded

    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). 

           

    The log message includes the SQL statement, 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 tune the agent to increase or decrease the amount of detail recorded in configuration or at runtime.

    For more information, see Using Loupe with Entity Framework - Tracing Actions

    The performance metric includes the SQL statement, execution duration, success or failure, and number of rows affected.  Using this information, Loupe Desktop can identify the top queries by duration, frequency, or total execution time and categorize performance of the same query by different input parameters. 

    For more information, see Using Loupe with Entity Framework - Analyzing Performance.

    Loupe highly compresses each log message, making it safe and easy to record details like the full SQL statement of every database action without affecting your application's performance
     Adding the Agent to your Project

    You only need to add the Loupe Agent for Entity Framework Core to a single project in your solution.  We recommend that you add it to the same project that contains your startup configuration since it has to be injected there.  The Agent is available on NuGet - just add the package Loupe.Agent.EntityFrameworkCore.  This will automatically add the core Loupe Agent if you haven't already.

    To activate the agent you need to make one call to register it as a service during startup. Once registered with Entity Framework it will automatically record information for every EF context in the application domain.

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        //Add Loupe and connect it to EF Core.
        services.AddLoupe()
                .AddEntityFrameworkCoreDiagnostics(); //Add the EF Core Agent
    }
    

    See Also