WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
随笔 - 1079, 文章 - 1, 评论 - 75, 阅读 - 174万
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

.net core 下监控Sql的执行语句

Posted on   WebEnh  阅读(1231)  评论(0编辑  收藏  举报

原文:.net core 下监控Sql的执行语句

 

最近在编写.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/)

 
编辑推荐:
· 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 反向代理服务器 安装成服务
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

了解更多