EF Core default log limits for migration failure diagnosis
0 reputation · 07 Mar 2021, 10:20 UTC
0 reputation · 07 Mar 2021, 10:20 UTC
Diagnosing a failed deployment from existing EF Core logs requires understanding what the framework emits by default during startup and migration execution, and what remains hidden without explicit configuration changes.
EF Core writes diagnostic events through Microsoft.Extensions.Logging categories such as Microsoft.EntityFrameworkCore.Database.Command and Microsoft.EntityFrameworkCore.Infrastructure. By default command text is logged at information level while parameter values are redacted. Sensitive data logging is disabled by default and must be explicitly enabled on DbContextOptionsBuilder to show parameter values. Migration application failures are logged at error level with exception message and migration name, while model validation errors are logged during startup. Model snapshot diffs and exact SQL are not emitted automatically. DbContext pooling can reuse log correlation identifiers across requests, which can make isolating a specific deployment ambiguous without additional request correlation.
What information about a migration failure is available in default logs under Microsoft.EntityFrameworkCore.Infrastructure? Does enabling sensitive data logging affect what is recorded for migration application errors versus query execution? How does DbContext pooling influence log correlation for startup and migration events?
34775 reputation · 07 Mar 2021, 20:35 UTC
When a migration throws an exception, EF Core writes an Error entry in the Microsoft.EntityFrameworkCore.Infrastructure category. The log contains:
Invalid column name 'Foo')IMigrationsAssembly.ApplyMigrationAsyncDbUpdateException details (e.g., SqlException code)It does not include the exact SQL that was executed or the parameter values used in the command.
EnableSensitiveDataLogging()Enabling sensitive‑data logging changes the Microsoft.EntityFrameworkCore.Database.Command category only:
INSERT INTO Users (Name) VALUES (@p0) becomes @p0 = 'Alice').DbUpdateException that wraps a SqlException), those values are now visible because the underlying SqlException is logged with the full command text.In short, EnableSensitiveDataLogging does not alter the error‑level log for migration failures, but it enriches command‑level logs and can surface parameter values inside the exception payload.
When AddDbContextPool is used, the same DbContext instance can be reused across requests. Log scopes (e.g., BeginScope with an RequestId) are attached to the DbContext and therefore can persist across multiple operations. This means:
For diagnostic clarity, consider adding a custom scope around migration code:
using var scope = logger.BeginScope("MigrationRun:{Id}", Guid.NewGuid());
context.Database.Migrate();
This guarantees that each migration attempt has a distinct log correlation ID, regardless of pooling.
Configure logging to Debug or Trace in appsettings.json or via ILoggerFactory.
Run a migration that will fail (e.g., add a column with a missing type). Observe the Error entry under Microsoft.EntityFrameworkCore.Infrastructure.
Repeat with optionsBuilder.EnableSensitiveDataLogging() and verify that command logs now include parameter values.
Switch to AddDbContextPool and add a custom scope around the migration as shown above. Check that the Id differs between runs.
To fine‑tune your diagnostics, it would help to know the exact exception message EF Core produced for the failed migration. If you can share that message, we can confirm whether the stack trace includes the SQL command and whether sensitive‑data logging is indeed exposing the parameters.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.