.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);

### 引用链接
1. [口袋代码仓库](http://codeex.cn)
2. [在线计算器](http://jisuanqi.codeex.cn)
3. 本节源码:[github](https://github.com/webmote-org/)

猜你喜欢

转载自blog.csdn.net/webmote/article/details/79539561