Logging
Wire an
ILoggerFactoryto theDataContextBuilderto capture the executed SQL, parameter values and connection lifecycle messages.
Prerequisites: Connections · Dependency injection · Query reuse.
Overview
Logging is configured per context through the DataContextBuilder: UseLoggerFactory(ILoggerFactory) enables it, and LogSensitiveData(bool) decides whether parameter values may appear in the output. The Use… provider extensions and the options-based DI registration both return the same builder, so logging configuration is provider-independent.
Wiring an ILoggerFactory
Set a factory on the DataContextBuilder; the context creates its loggers in the constructor.
using Microsoft.Extensions.Logging;
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole().SetMinimumLevel(LogLevel.Debug);
});
var builder = new DataContextBuilder()
.UseSqlite("app.db")
.UseLoggerFactory(loggerFactory)
.LogSensitiveData(false);
using var ctx = builder.CreateDataContext();
The three loggers
Three loggers are created from the factory:
| Member | Category | Purpose |
|---|---|---|
Logger |
the context type (for example nextorm.sqlite.SqliteDataContext) |
connection and command messages. Exposed on IContextEnvironment. |
CommandLogger |
the QueryCommand type |
attached to the commands built by From / From(...). Exposed on IContextEnvironment. |
ResultSetEnumeratorLogger |
NextORM.Core.ResultSetEnumerator |
streaming lifecycle messages (Trace on MoveNext, Debug when opening the connection or disposing the reader). Internal. |
LogSensitiveData
LogSensitiveData(bool) controls whether parameter values and the connection string are written. It defaults to false:
- the SQL is logged at
DebugasExecuting query: {sql}; - with
LogSensitiveData(false), a parameterised query additionally logsUse LogSensitiveData to see param values; - with
LogSensitiveData(true), every parameter name and value is logged, and connection creation logsCreating connection with {connStr}instead of justCreating connection.
Turn it on only when the log sink is trusted: parameter values may contain personal data.
Command logging
Buffered terminals (ToList, First, ExecuteScalar, ...) log the command through Logger before executing it. Streaming terminals use ResultSetEnumeratorLogger, which also emits Move next at Trace. Both paths are at Debug, so the log level of the configured factory must allow Debug for anything to appear:
var builder = new DataContextBuilder()
.UseSqlServer(connectionString)
.UseLoggerFactory(loggerFactory)
.LogSensitiveData(true); // includes parameter values in the output
Provider differences
Logging behaves the same across every provider; only the connection type differs. See Connections: provider differences.
See also
Source: src/nextorm.core/DI/DataContextBuilder.cs:20 (UseLoggerFactory).