AddDataContextLogging

Registers an ILoggerProvider that stores the host's ILogger messages in the trax.log table, readable through IDataContext.Logs. It is a sink for application logging, not a trace of the data context: every category at or above minimumLogLevel that is not blacklisted is stored, whatever wrote it, and no SQL or transaction boundary is recorded. See Debugging with the log table for querying it.

Signature

public static TraxEffectBuilderWithData AddDataContextLogging(
    this TraxEffectBuilderWithData effectBuilder,
    LogLevel? minimumLogLevel = null,
    List<string>? blacklist = null
)

Parameters

ParameterTypeRequiredDefaultDescription
minimumLogLevelLogLevel?NoLogLevel.InformationMinimum log level to capture.
blacklistList<string>?No[] (empty)Logger categories not to store: an exact category name, or a pattern in which * matches any run of characters (e.g., ["Microsoft.EntityFrameworkCore.*"])

Returns

TraxEffectBuilderWithData, for continued fluent chaining.

Example

services.AddTrax(trax => trax
    .AddEffects(effects => effects
        .UsePostgres(connectionString)
        .AddDataContextLogging(
            minimumLogLevel: LogLevel.Warning,
            blacklist: ["Microsoft.EntityFrameworkCore.Database.Command"])
    )
);

Remarks

  • Requires a data provider (UsePostgres, UseSqlite or UseInMemory). This is enforced at compile time. AddDataContextLogging is only available on TraxEffectBuilderWithData, which is returned by the data provider methods. Called before one, it fails with CS0619 and the instruction as its text: Call UsePostgres(...), UseSqlite(...) or UseInMemory(...) before AddDataContextLogging(...).
  • Registers DataContextLoggingProvider as an ILoggerProvider.
  • EF Core's command log, Microsoft.EntityFrameworkCore.Database.Command, is always skipped, because writing a row would log another one. Listing it in blacklist changes nothing.
  • When the host stops, the provider writes the entries already queued, waiting up to five seconds, before it lets go.
  • Log levels can be changed at runtime via the Dashboard's Server Settings page.