最近在编写.net core程序,因为数据库从Sql Server 切换到 MySql的原因,无法直接查看sql的具体语句,随着业务量的剧增,痛苦也与日俱增,为了彻底解决该问题,我在github、stackoverflow等站点不断搜索,试图找到一个好的办法。
搜索的结果大都是这样的:
SHOW VARIABLES LIKE "general_log%";
SET GLOBAL general_log = 'ON';
好嘞,试过以后发现果然灵验,BUT,我没有权限操作mysql的计算机,my god,此路不通~~~~
最近搜索时再次发现MiniProfiler库有更新~~
dotnet core支持不错,终于看到希望了~~~
那就开始集成吧,如果你是asp.net core工程,就参考文档吧,
如果是控制台程序,那就按照步骤开始吧:
step 1:安装nuget包 MiniProfiler.EntityFrameworkCore ,目前仍是alpha版本。
【step 2】: 开启EF core的监控初始化 ,如果你使用EF Core的话
var initializer = new DiagnosticInitializer(new[] { new RelationalDiagnosticListener() });
initializer.Start();
【step 2】:开启Dapper的链接监控初始化,如果你使用Dapper的话
private DbConnection GetConnection()
{
DbConnection conn = new MySqlConnection(MySqlDBContextOptionBuilder.GetDbConnectionString(DbInfo));
if (MiniProfiler.Current != null)
{
conn = new StackExchange.Profiling.Data.ProfiledDbConnection(conn, MiniProfiler.Current);
}
conn.Open();
return conn;
}
step 3:启动监控,在你的执行代码上增加如下代码
var profiler = MiniProfiler.StartNew(m);
using (profiler.Step("SqlProfile"))
{
// 你的代码
}
// 输出日志
if (profiler?.Root != null)
{
var p = profiler.Root;
Trace.WriteLine($"{p.Name}:{p.Id},{p.DurationMilliseconds} ms");
if (p.HasChildren)
{
p.Children.ForEach(x =>
{
Trace.WriteLine($"{p.Name}:{x.Name},st:{x.StartMilliseconds} ms,exec:{x.DurationMilliseconds} ms");
if (x.CustomTimings?.Count > 0)
{
foreach (var ct in x.CustomTimings)
{
Trace.WriteLine($"{p.Name}:Start {ct.Key} --- ");
ct.Value?.ForEach(y =>
{
Trace.WriteLine($"{p.Name}:{y.CommandString}");
Trace.WriteLine($"{p.Name}:Execute time :{y.DurationMilliseconds} ms,Start offset :{y.StartMilliseconds} ms,Errored :{y.Errored}");
});
Trace.WriteLine($"{p.Name}:End {ct.Key} --- ");
}
}
});
}
}
profiler?.StopAsync(true).ConfigureAwait(false);
var profiler = MiniProfiler.StartNew(m);
using (profiler.Step("SqlProfile"))
{
// 你的代码
}
// 输出日志
if (profiler?.Root != null)
{
var p = profiler.Root;
Trace.WriteLine($"{p.Name}:{p.Id},{p.DurationMilliseconds} ms");
if (p.HasChildren)
{
p.Children.ForEach(x =>
{
Trace.WriteLine($"{p.Name}:{x.Name},st:{x.StartMilliseconds} ms,exec:{x.DurationMilliseconds} ms");
if (x.CustomTimings?.Count > 0)
{
foreach (var ct in x.CustomTimings)
{
Trace.WriteLine($"{p.Name}:Start {ct.Key} --- ");
ct.Value?.ForEach(y =>
{
Trace.WriteLine($"{p.Name}:{y.CommandString}");
Trace.WriteLine($"{p.Name}:Execute time :{y.DurationMilliseconds} ms,Start offset :{y.StartMilliseconds} ms,Errored :{y.Errored}");
});
Trace.WriteLine($"{p.Name}:End {ct.Key} --- ");
}
}
});
}
}
profiler?.StopAsync(true).ConfigureAwait(false);
3. 本节源码:[github](https://github.com/webmote-org/)
本博客Android APP 下载 |
![]() |
支持我们就给我们点打赏 |
![]() |
支付宝打赏 支付宝扫一扫二维码 |
![]() |
微信打赏 微信扫一扫二维码 |
![]() |
如果想下次快速找到我,记得点下面的关注哦!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2019-06-15 jQuery对checkbox的各种操作
2018-06-15 在.NET开发中的单元测试工具之(1)——NUnit
2018-06-15 在.NET开发中的单元测试工具之(2)——xUnit.Net
2018-06-15 Git 头像修改 原
2018-06-15 Dapper.NET——轻量ORM
2017-06-15 Windows平台分布式架构实践 - 负载均衡
2017-06-15 WINDOWS 2008Server 配置nginx 反向代理服务器 安装成服务