How to make EF Core migrations less verbose

807 views Asked by At

I am using Entity Framework Core 1.1.0 with migrations. When I run them with

dotnet ef database update

in Package Manager Console the console is full of applied SQL. Instead of this, I would like to have printed only names of migrations that are currently being applied. How can I do this?

1

There are 1 answers

0
Vova Bilyachat On

I assume its configuration of your project.

To disable sql print try this

var builder = new DbContextOptionsBuilder<NAMEContext>();
builder.UseMySql(connectionString);
builder.UseLoggerFactory(new MigrationLoggerFactory()); <--- this seems to be what you are looking for
return new MigrationDataContext(builder.Options);

MigrationLoggerFactory

public class MigrationLoggerFactory : ILoggerFactory
    {
        public void Dispose() { }

        public ILogger CreateLogger(string categoryName)
        {
            if ("Microsoft.EntityFrameworkCore.Migrations".Equals(categoryName))
                return new MigrationLogger();

            return new NullLogger();
        }

        public void AddProvider(ILoggerProvider provider)
        {
        }
    }

NullLogger

public class NullLogger : ILogger
    {
        public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
        {
            throw new NotImplementedException();
        }

        public bool IsEnabled(LogLevel logLevel)
        {
            return false;
        }

        public IDisposable BeginScope<TState>(TState state)
        {
            return null;
        }
    }

Here is an article which could also be helpful