dotnet / dotnet/efcore

Improved logging support for efbundle

Open
#34,353 1 comment 1 reaction 0 assignees View on GitHub
area-migrations customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

I would like `efbundle` to use my logging configuration.

I use Serilog to log in a JSON format, including from my `DbContext`, e.g.

```
public class MyDatabaseContext(ILogger logger) : DbContext
{
readonly ILogger _log = logger.ForContext();

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(...);
optionsBuilder.UseLoggerFactory(Microsoft.Extensions.Logging.LoggerFactory.Create(logger =>
{
logger.AddSerilog();
}));
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
_log.Information("Some useful information...");
}

...
}
```

When running a packaged `efbundle` to apply migrations, messages from within the context will be logged as JSON, but messages from the bundle tool itself are written raw to `stdout`. This mixing of log formats makes it difficult when streamed to something like Datadog:

![image](https://github.com/user-attachments/assets/d836a1e0-729f-4963-9e22-9e14092eff87)

The `efbundle` tool is already picking up the `DbContext` and its logging configuration from the startup project. Could we also have it use the logging infrastructure? Possible solutions:

* Automatically pick up the logging configuration from the same host builder.
* An explicit logging factory or hook for the migrations.
* Extending the design time context factory (https://learn.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli#from-a-design-time-factory).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.