Skip to content

Instantly share code, notes, and snippets.

@tonysneed
Last active May 15, 2018 14:01
Show Gist options
  • Save tonysneed/4cac4f4dae2b22e45ec4 to your computer and use it in GitHub Desktop.
Save tonysneed/4cac4f4dae2b22e45ec4 to your computer and use it in GitHub Desktop.
Configure EF7 to Log to the Console
public static class DbContextExtensions
{
public static void LogToConsole(this DbContext context)
{
IServiceProvider contextServices = ((IDbContextServices)context).ScopedServiceProvider;
var loggerFactory = contextServices.GetRequiredService<ILoggerFactory>();
loggerFactory.AddConsole(LogLevel.Verbose);
}
}
@bragma
Copy link

bragma commented Nov 6, 2015

I have the same question, where should this code go? Thanks.

@michaelvolz
Copy link

This worked for me with EF7 rc2-16485:

    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc2-16485",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-15888",
    public static class DbContextExtensions
    {
        public static void LogToConsole(this DbContext context)
        {
            var contextServices = ((IInfrastructure<IServiceProvider>) context).Instance;
            var loggerFactory = contextServices.GetRequiredService<ILoggerFactory>();
            loggerFactory.AddConsole(LogLevel.Verbose);
        }
    }

@SergeySagan
Copy link

Where is the RTM Version?

@FisherMS
Copy link

oh,It doesn't work.

@MohammadMQ
Copy link

MohammadMQ commented May 15, 2018

include these

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Debug;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment