有什么办法可以使用Dapper跟踪SQL日志? [英] Is there any way to trace\log the sql using Dapper?

查看:716
本文介绍了有什么办法可以使用Dapper跟踪SQL日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以将生成的sql转储到Debug日志中?我在winforms解决方案中使用了它,因此迷你分析器的想法对我不起作用。

解决方案

同样的问题,并在进行了一些搜索后实现了一些代码,但没有现成的东西。我要分享的nuget MiniProfiler.Integrations 上有一个软件包。 p>

更新V2 :它支持与其他数据库服务器一起使用,对于MySQL,它需要具有 MiniProfiler.Integrations.MySql



以下是使用SQL的步骤服务器:



1。实例化连接

  var factory = new SqlServerDbConnectionFactory(_connectionString); 
使用(var connection = DbConnectionFactoryHelper.New(factory,CustomDbProfiler.Current))
{
//您的代码
}

2。完成所有工作后,如果需要,将所有命令写入文件中

  File.WriteAllText( SqlScripts.txt,CustomDbProfiler.Current.ProfilerContext.BuildCommands()); 


Is there a way to dump the generated sql to the Debug log or something? I'm using it in a winforms solution so the mini-profiler idea won't work for me.

解决方案

I got the same issue and implemented some code after doing some search but having no ready-to-use stuff. There is a package on nuget MiniProfiler.Integrations I would like to share.

Update V2: it supports to work with other database servers, for MySQL it requires to have MiniProfiler.Integrations.MySql

Below are steps to work with SQL Server:

1.Instantiate the connection

var factory = new SqlServerDbConnectionFactory(_connectionString);
using (var connection = DbConnectionFactoryHelper.New(factory, CustomDbProfiler.Current))
{
 // your code
}

2.After all works done, write all commands to a file if you want

File.WriteAllText("SqlScripts.txt", CustomDbProfiler.Current.ProfilerContext.BuildCommands());

这篇关于有什么办法可以使用Dapper跟踪SQL日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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