Reputation: 9348
When I log a SqlException
with NLog, many of its important properties such as Stored procedure name are missing from the formatted exception.
What would be the most efficient way to log a SqlException
with NLog?
Upvotes: 1
Views: 140
Reputation: 36750
You could write the extra info into properties. The fluent interface is the most suitable in that case.
using NLog.Fluent;
...
var sqlCommand = new SqlCommand();
try
{
}
catch (SqlException ex)
{
Log.Error().Exception(ex)
.Message("Error in DB")
.Property("command-text", sqlCommand.CommandText)
.Property("command-parameters",
string.Join(",", sqlCommand.Parameters.Cast<SqlParameter>().Select(p => p.ParameterName + "=" + p.Value)))
.Write();
}
Usage in the config:
${event-properties:command-text}
and ${event-properties:command-parameters}
Upvotes: 1