Repository navigation
Add EF Core-style logging for bulk insert operations #103
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e074358
Initial plan
Copilot d6a04ef
Add logging similar to EF Core: BulkInsertExecuted and ExecutedDbComm…
Copilot 2391791
Apply review fixes for logging and resource disposal
Copilot efb336b
Update docs and Copilot instructions for logging changes
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/Tests/Logging/CapturingLogger.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.Tests.Logging; | ||
|
|
||
| internal sealed record LogEntry(LogLevel Level, EventId EventId, string Message); | ||
|
|
||
| internal sealed class CapturingLogger : ILogger | ||
| { | ||
| private readonly List<LogEntry> _entries; | ||
|
|
||
| public CapturingLogger(List<LogEntry> entries) | ||
| { | ||
| _entries = entries; | ||
| } | ||
|
|
||
| public IDisposable? BeginScope<TState>(TState state) where TState : notnull => NullScope.Instance; | ||
|
|
||
| public bool IsEnabled(LogLevel logLevel) => true; | ||
|
|
||
| public void Log<TState>( | ||
| LogLevel logLevel, | ||
| EventId eventId, | ||
| TState state, | ||
| Exception? exception, | ||
| Func<TState, Exception?, string> formatter) | ||
| { | ||
| _entries.Add(new LogEntry(logLevel, eventId, formatter(state, exception))); | ||
| } | ||
|
|
||
| private sealed class NullScope : IDisposable | ||
| { | ||
| public static readonly NullScope Instance = new(); | ||
| public void Dispose() { } | ||
| } | ||
| } | ||
|
|
||
| internal sealed class CapturingLoggerProvider(List<LogEntry> entries) : ILoggerProvider | ||
| { | ||
| public ILogger CreateLogger(string categoryName) => new CapturingLogger(entries); | ||
| public void Dispose() { } | ||
| } |
132 changes: 132 additions & 0 deletions
132
tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/Tests/Logging/LoggingTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| using FluentAssertions; | ||
|
|
||
| using Microsoft.Data.Sqlite; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| using PhenX.EntityFrameworkCore.BulkInsert.Extensions; | ||
| using PhenX.EntityFrameworkCore.BulkInsert.Sqlite; | ||
| using PhenX.EntityFrameworkCore.BulkInsert.Tests.DbContext; | ||
|
|
||
| using Xunit; | ||
|
|
||
| namespace PhenX.EntityFrameworkCore.BulkInsert.Tests.Tests.Logging; | ||
|
|
||
| [Trait("Category", "Sqlite")] | ||
| public class LoggingTests | ||
| { | ||
| [Fact] | ||
| public async Task BulkInsert_LogsBulkInsertExecuted_AtInformationLevel() | ||
| { | ||
| // Arrange | ||
| var logEntries = new List<LogEntry>(); | ||
| using var loggerFactory = LoggerFactory.Create(builder => | ||
| builder | ||
| .AddProvider(new CapturingLoggerProvider(logEntries)) | ||
| .SetMinimumLevel(LogLevel.Debug)); | ||
|
|
||
| await using var connection = new SqliteConnection("DataSource=:memory:"); | ||
| await connection.OpenAsync(); | ||
|
|
||
| var context = new TestDbContextSqlite | ||
| { | ||
| ConfigureOptions = builder => builder | ||
| .UseSqlite(connection) | ||
| .UseLoggerFactory(loggerFactory) | ||
| .UseBulkInsertSqlite(), | ||
| }; | ||
| await context.Database.EnsureCreatedAsync(); | ||
|
PhenX marked this conversation as resolved.
Outdated
|
||
|
|
||
| var run = Guid.NewGuid(); | ||
| var entities = new List<TestEntity> | ||
| { | ||
| new TestEntity { TestRun = run, Name = "Entity1" }, | ||
| new TestEntity { TestRun = run, Name = "Entity2" }, | ||
| }; | ||
|
|
||
| // Act | ||
| await context.ExecuteBulkInsertAsync(entities); | ||
|
|
||
| // Assert | ||
| logEntries.Should().Contain(e => | ||
| e.Level == LogLevel.Information && | ||
| e.EventId.Id == 1004 && | ||
| e.Message.Contains("Executed BulkInsert")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task BulkInsert_LogsDbCommands_AtDebugLevel() | ||
| { | ||
| // Arrange | ||
| var logEntries = new List<LogEntry>(); | ||
| using var loggerFactory = LoggerFactory.Create(builder => | ||
| builder | ||
| .AddProvider(new CapturingLoggerProvider(logEntries)) | ||
| .SetMinimumLevel(LogLevel.Debug)); | ||
|
|
||
| await using var connection = new SqliteConnection("DataSource=:memory:"); | ||
| await connection.OpenAsync(); | ||
|
|
||
| var context = new TestDbContextSqlite | ||
| { | ||
| ConfigureOptions = builder => builder | ||
| .UseSqlite(connection) | ||
| .UseLoggerFactory(loggerFactory) | ||
| .UseBulkInsertSqlite(), | ||
| }; | ||
| await context.Database.EnsureCreatedAsync(); | ||
|
PhenX marked this conversation as resolved.
Outdated
|
||
|
|
||
| var run = Guid.NewGuid(); | ||
| var entities = new List<TestEntity> | ||
| { | ||
| new TestEntity { TestRun = run, Name = "Entity1" }, | ||
| }; | ||
|
|
||
| // Act - ReturnEntities always uses a temp table, triggering auxiliary SQL commands | ||
| await context.ExecuteBulkInsertReturnEntitiesAsync(entities); | ||
|
|
||
| // Assert | ||
| logEntries.Should().Contain(e => | ||
| e.Level == LogLevel.Debug && | ||
| e.EventId.Id == 1005 && | ||
| e.Message.Contains("Executed DbCommand")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task BulkInsert_LogMessageContainsTableName() | ||
| { | ||
| // Arrange | ||
| var logEntries = new List<LogEntry>(); | ||
| using var loggerFactory = LoggerFactory.Create(builder => | ||
| builder | ||
| .AddProvider(new CapturingLoggerProvider(logEntries)) | ||
| .SetMinimumLevel(LogLevel.Information)); | ||
|
|
||
| await using var connection = new SqliteConnection("DataSource=:memory:"); | ||
| await connection.OpenAsync(); | ||
|
|
||
| var context = new TestDbContextSqlite | ||
| { | ||
| ConfigureOptions = builder => builder | ||
| .UseSqlite(connection) | ||
| .UseLoggerFactory(loggerFactory) | ||
| .UseBulkInsertSqlite(), | ||
| }; | ||
| await context.Database.EnsureCreatedAsync(); | ||
|
PhenX marked this conversation as resolved.
Outdated
|
||
|
|
||
| var run = Guid.NewGuid(); | ||
| var entities = new List<TestEntity> | ||
| { | ||
| new TestEntity { TestRun = run, Name = "Entity1" }, | ||
| }; | ||
|
|
||
| // Act | ||
| await context.ExecuteBulkInsertAsync(entities); | ||
|
|
||
| // Assert | ||
| logEntries.Should().Contain(e => | ||
| e.Level == LogLevel.Information && | ||
| e.EventId.Id == 1004 && | ||
| e.Message.Contains("test_entity")); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.