如何记录/获取Dapper Extensions自动生成的SQL查询? [英] How to log/get a SQL query auto-generated by Dapper Extensions?

查看:736
本文介绍了如何记录/获取Dapper Extensions自动生成的SQL查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Dapper Extensions(DE)用作ORM。它在使用存储库模式实现的数据访问层中使用。 SQL Express是后端RDBMS。

I am using Dapper Extensions (DE) as ORM. It is consumed in Data Access Layer which is implemented using Repository pattern. SQL Express is back-end RDBMS.

DE自动为我生成大多数查询。我想记录这些自动生成的查询以进行调试。

DE automatically generates most of the queries for me. I want to log those auto-generated queries for debugging purpose.

我可以通过两种方式实现此目的:-


  1. 获取由DE生成的SQL查询(在执行之前或之后)并将其写入日志。 这是我的首选方式,因为我已经安装了日志记录模块(使用log4net)。我唯一需要的是DE生成的SQL。

  2. 将DE与某些日志记录工具集成。我阅读了答案。使用MiniProfiler工具看起来可能;但是如上所述,我已经有了日志模块。我不想仅使用其他工具记录SQL查询。

  1. Get the SQL query generated by DE (before or after it is executed) and write it to log. This is preferred way for me as I already have my logging module (using log4net) in place. The only thing I need is the SQL generated by DE.
  2. Integrate DE with some logging tool. I read this answer. It looks possible using MiniProfiler tool; but as I said above, I already have my logging module in place. I do not want to use other tool just for logging SQL queries.

如何记录/获取由Dapper自动生成的SQL查询扩展程序是否不使用任何其他日志记录工具?

How to log/get a SQL query auto-generated by Dapper Extensions without using any other logging tool?

其他类似的问题与Dapper有关。此问题与Dapper扩展有关。

The other similar question is about Dapper. This question is about Dapper Extensions.

推荐答案

查看评论来自@MarcGravell和关于

Looking at the comment from @MarcGravell and this question about doing the same with Dapper, MiniProfiler.Integrations is better way to implement logging for Dapper Extensions.

上面链接的问题与Dapper有关。但是Dapper Extensions在内部使用Dapper。因此,如果为Dapper实现了日志记录,那么Dapper Extensions的工作也是如此。

Above linked question is about Dapper. But Dapper Extensions uses Dapper internally. So, if logging is implemented for Dapper, same works for Dapper Extensions as well.

更多详细信息可以在 GitHub

示例代码如下:

var factory = new SqlServerDbConnectionFactory(connectionString);
CustomDbProfiler cp = new CustomDbProfiler();
using(var connection = DbConnectionFactoryHelper.New(factory, cp))
{
    //DB Code
}
string log = cp.ProfilerContext.GetCommands();

您可以使用内置 CustomDbProfiler CustomDbProfiler.Current (如果适合您)。 cp.ProfilerContext.GetCommands()将返回所有命令(成功和失败),无论您调用该方法多少次。我不确定,但是,它可能在内部维护串联的字符串(可能是 StringBuilder )。在这种情况下,这可能会降低性能。但是,就我而言,默认情况下禁用日志记录。我仅在需要调试时才启用日志记录。因此,这对我来说不是问题。

You can use in-build CustomDbProfiler using CustomDbProfiler.Current if that suits your need. cp.ProfilerContext.GetCommands() will return ALL the commands (success and failed) no matter how many times you call the method. I am not sure but, it might be maintaining concatenated string (StringBuilder may be) internally. If this is the case, this may slow down the performance. But, in my case, logging is disabled by default. I only enable logging when I need to debug something. So this is not a problem for me.

如果在很大范围内使用单个连接,这也可能会引起内存占用问题。为避免这种情况,请确保正确放置 CustomDbProfiler 实例。

This also may raise memory footprint issue if single connection is used over very large scope. To avoid this, make sure CustomDbProfiler instance is disposed properly.

如前所述,最初,我想避免这种方式(使用外部工具/库)。但是, MiniProfiler.Integrations 不会写日志本身。我可以简单地获取所有生成的查询,并将其提供给我的记录器模块以转储到文件中。这就是为什么,这对我来说现在更合适。

As mentioned in question, initially, I wanted to avoid this way (using external tool/library). But, MiniProfiler.Integrations is NOT writing the log itself. I can simply get all the queries generated and provide those to my logger module to dump into the file. That is why, this looks more suitable to me now.

MiniProfiler.dll内部实现了类似的逻辑(在<$ c中$ c> StackExchange.Profiling.Data.ProfiledDbConnection 和 StackExchange.Profiling.Data.ProfiledDbCommand 类),此处此处。因此,如果我决定(将来可能)绕过MiniProfiler,则可以自己使用此实现。

MiniProfiler.dll internally implements similar logic (in StackExchange.Profiling.Data.ProfiledDbConnection and StackExchange.Profiling.Data.ProfiledDbCommand classes) which is mentioned here and here. So, if I decide to (in future may be) bypass MiniProfiler, I can use this implementation myself.

这篇关于如何记录/获取Dapper Extensions自动生成的SQL查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆