Simple Logging

Simple Logging

Simple Logging is the equivalent of Database.Log in EF6. It provides a simple way to get logs from EF Core without the need to configure any kind of external logging framework.
EF Core replaces Database.Log with a LogTo method called on DbContextOptionsBuilder in either AddDbContext or OnConfiguring.
1
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
2
=> optionsBuilder.LogTo(Console.WriteLine);
Copied!
There are multiple overloads available which can be used in different use cases.
    Set the minimum log level
      Example: .LogTo(Console.WriteLine, LogLevel.Information)
    Filter for only specific events:
      Example: .LogTo(Console.WriteLine, new[] {CoreEventId.ContextInitialized, RelationalEventId.CommandExecuted})
    Filter for all events in specific categories:
      Example: .LogTo(Console.WriteLine, new[] {DbLoggerCategory.Database.Name}, LogLevel.Information)
    Use a custom filter over event and level:
      Example: .LogTo(Console.WriteLine, (id, level) => id == RelationalEventId.CommandExecuting)
Output format can be minimally configured (API is in flux) but the default output looks something like:
1
warn: 12/5/2019 09:57:47.574 CoreEventId.SensitiveDataLoggingEnabledWarning[10400] (Microsoft.EntityFrameworkCore.Infrastructure)
2
Sensitive data logging is enabled. Log entries and exception messages may include sensitive application data, this mode should only be enabled during development.
3
dbug: 12/5/2019 09:57:47.581 CoreEventId.ShadowPropertyCreated[10600] (Microsoft.EntityFrameworkCore.Model.Validation)
4
The property 'BlogId' on entity type 'Post' was created in shadow state because there are no eligible CLR members with a matching name.
5
info: 12/5/2019 09:57:47.618 CoreEventId.ContextInitialized[10403] (Microsoft.EntityFrameworkCore.Infrastructure)
6
Entity Framework Core 5.0.0-dev initialized 'BloggingContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: SensitiveDataLoggingEnabled
7
dbug: 12/5/2019 09:57:47.644 CoreEventId.ValueGenerated[10808] (Microsoft.EntityFrameworkCore.ChangeTracking)
8
'BloggingContext' generated temporary value '-2147482647' for the 'Id' property of new 'Blog' entity.
9
...
Copied!
Last modified 2mo ago
Copy link