Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
}
}
@tonysneed

This comment has been minimized.

Copy link
Owner Author

@tonysneed tonysneed commented Dec 26, 2014

This code relies on the following NuGet packages:
EntityFramework.SqlServer
Microsoft.Framework.Logging.Console

@ErikEJ

This comment has been minimized.

Copy link

@ErikEJ ErikEJ commented May 17, 2015

@tonysneed Is this still valid?

@nbalakin

This comment has been minimized.

Copy link

@nbalakin nbalakin commented May 23, 2015

@ErikEJ

var service = ((IAccessor<IServiceProvider>)context).Service;
var loggerFactory = service.GetRequiredService<ILoggerFactory>();
loggerFactory.AddConsole(LogLevel.Verbose);
@tongbong

This comment has been minimized.

Copy link

@tongbong tongbong commented May 26, 2015

Thanks @nbalakin and @tonysneed, it works.

@gius

This comment has been minimized.

Copy link

@gius gius commented Aug 11, 2015

@nbalakin the logger factory instance is shared across multiple DbContexts and thus everytime I call this code, another logger is added. As a result, provided I call this code when creating a new DbContext, after 5 DbContexts have been created during the application lifetime, all SQL queries are logged five times.

Am I doing anything wrong? Where exactly should this be called?

@bragma

This comment has been minimized.

Copy link

@bragma bragma commented Nov 6, 2015

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

@michaelvolz

This comment has been minimized.

Copy link

@michaelvolz michaelvolz commented Dec 11, 2015

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

This comment has been minimized.

Copy link

@SergeySagan SergeySagan commented Nov 3, 2016

Where is the RTM Version?

@Fisher-Joe

This comment has been minimized.

Copy link

@Fisher-Joe Fisher-Joe commented Feb 12, 2018

oh,It doesn't work.

@MohammadMQ

This comment has been minimized.

Copy link

@MohammadMQ 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