仅在 PetaPoco 出现错误时记录 SqlCommand
我知道我可以使用 PetaPoco 记录异常:
public override void OnException(Exception x)
{
_logger.LogError(x);
}
我也意识到我可以转储命令文本 + 参数:
public override void OnExecutingCommand(System.Data.IDbCommand cmd)
{
_logger.LogInfo(cmd.CommandText);
foreach (SqlParameter sqlParam in cmd.Parameters)
{
_logger.LogInfo(String.Format("Name: {0}; Value: {1}; SqlValue: {1}", sqlParam.ParameterName,
sqlParam.Value, sqlParam.SqlValue));
}
base.OnExecutingCommand(cmd);
}
当然,我真的不想在生产环境中记录每个命令 + 参数。
当抛出异常时仅记录命令+参数的最佳方法是什么?
谢谢!
I know I can log exceptions with PetaPoco thus:
public override void OnException(Exception x)
{
_logger.LogError(x);
}
I also realize I can dump the command text + params:
public override void OnExecutingCommand(System.Data.IDbCommand cmd)
{
_logger.LogInfo(cmd.CommandText);
foreach (SqlParameter sqlParam in cmd.Parameters)
{
_logger.LogInfo(String.Format("Name: {0}; Value: {1}; SqlValue: {1}", sqlParam.ParameterName,
sqlParam.Value, sqlParam.SqlValue));
}
base.OnExecutingCommand(cmd);
}
Naturally, I don't really want to log each command+params in a production environment.
What's the best approach to log the command + params only when an exception is thrown?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以试试这个。
LastSql
和LastArgs
也可用。You can try this.
LastSql
andLastArgs
are also available.