-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrations: SQL not logged on SqlServerCompact #7317
Comments
Hi @ErikEJ. I tried this with the following code and got the output below. It seems like commands from EnsureCreated are getting logged. Is there something else I need to do to repro this? public class Blog
{
public int Id { get; set; }
public string Title { get; set; }
public ICollection<Post> Posts { get; set; }
}
public class Post
{
public int Id { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
public string Title { get; set; }
}
public class BlogContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite(@"Data Source=.db");
}
}
public class Program
{
private static void Main()
{
using (var context = new BlogContext())
{
context.GetService<ILoggerFactory>().AddProvider(new DbLoggerProvider());
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
var blog = new Blog {Posts = new List<Post> { new Post() } };
context.Add(blog);
context.SaveChanges();
}
}
}
internal class DbLoggerProvider : ILoggerProvider
{
private static readonly string[] Whitelist =
{
typeof(IRelationalCommandBuilderFactory).FullName
};
public ILogger CreateLogger(string name)
=> Whitelist.Contains(name) ? (ILogger)new MyLogger() : NullLogger.Instance;
public void Dispose()
{
}
private class MyLogger : ILogger
{
public bool IsEnabled(LogLevel logLevel) => true;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception,
Func<TState, Exception, string> formatter)
=> Console.WriteLine(formatter(state, exception));
public IDisposable BeginScope<TState>(TState state) => null;
}
} Output:
|
Thanks @ajcvickers I guess my event filtering is too strict: https://github.com/ErikEJ/EntityFramework.SqlServerCompact/blob/master/src/Provider40/Extensions/Logging/DbSimpleLogger.cs#L25 |
Actually, it was not a filtering issue, it is due to this: https://github.com/aspnet/EntityFramework/blob/aff7bef4db688325af12cc45f825e19fc2b2bd00/src/Microsoft.EntityFrameworkCore.Relational/Storage/RelationalDataReader.cs#L77 (Dispose of the DbCommand causes the SQL CE provider to clear the CommandText) 😢 |
Marking for re-triage. Questions to consider:
|
@ajcvickers Thanks for reconsidering, found this older related issue: #3654 |
Actually, it might not be what I thought was the issue, that is: #3654 command?.Dispose(); - All statements, even INSERTs are logged correctly in the standard SaveChanges/Query pipeline, only the Migration SQL is not logged. When I run this test: https://github.com/ErikEJ/EntityFramework.SqlServerCompact/blob/master/test/EntityFramework.SqlServerCompact.FunctionalTests/BasicEndToEndScenarioForIdentity.cs I get this output, notice that the initial CREATE TABLE statement is missing
|
@ErikEJ Is there something you wan us to investigate here? |
@ajcvickers Yes, I am wondering why statement logging works only for query and update pipeline, but not for migrations - am I doing something wrong, or is it "by design" ? |
@ErikEJ When I run that test against SQL Server, this is what I see getting logged. If you modify the test on your machine to hit SQL Server do you see the migrations commands being logged? Or are you saying that you don't see the migrations commands only when trying to filter? If so, that's probably covered by #7217.
|
OK, so it is a SQL CE only issue, probably due to #3654 - and only when running Migration commands - never mind... |
@ajcvickers Re-opening - wonder why the logging of commands in the standard update pipeline works, as I would expect them to be affected by the same issue as the migrations commands (.CommandText being cleared by the SQL Compact ADO.NET provider on SqlCeCommand.Dispose() ) - and can you think of any workarounds (other that writing a wrapping ADO.NET provider) ? |
I ended up doing this hack for now: |
@bricelam to see if we can reorder so we don't dispose before logging |
@bricelam [closed-fixed] but not closed? |
I use that to represent "out for review" so it doesn't show up in my TODO query. |
That is fine. I will keep it in mind for next time. |
The logger implementation here (based on a sample from @rowanmiller ) works well for logging both INSERTS/UPDATES and SELECT, except for SQL statements generated by EnsureCreated (migrations)
https://github.com/ErikEJ/EntityFramework.SqlServerCompact/blob/master/src/Provider40/Extensions/Logging/DbLoggerProvider.cs
(I was unable to find an existing issue for this)
Further technical details
EF Core version: 1.2.0-preview1-22878
Database Provider: EntityFrameworkCore.SqlServerCompact40
Operating system: Windows 10
IDE: (e.g. Visual Studio 2015): VS 2015 Update 3
The text was updated successfully, but these errors were encountered: