Expanding on Nate's answer for EF6, the NLogCommandInterceptor
seen in Logging and Intercepting Database Operations only shows the CommandText.
If there was some particular parameter value that caused that commandText to fail, the parameters values aren't emitted to the log. In my case I wanted to log what values were causing Foreign Key violations.
This can be improved by altering NLogCommandInterceptor's LogIfError
method like thus
private void LogIfError<TResult>(DbCommand command, DbCommandInterceptionContext<TResult> interceptionContext)
{
if (interceptionContext.Exception != null)
{
var commandDumper = new DbCommandDumper(command);
Log.Warn(Command failed:\r\n{0}", commandDumper.GetLogDump());
// Exception will get logged further up the stack
}
}
where the DbCommandDumper class reconstructs the DbCommand into TSQL that can be replayed into a test database.