• .NetCore学习笔记:五、MiniProfiler监控运行效率


    MiniProfiler是一个简单且高效的小型分析器,可用于.NET,Ruby,Go和Node.js.
    MiniProfiler不是将自己附加到每一个方法上,因为这样会有强侵入性而且也不是专注于分析性能问题。

    它是一个ADO.NET的分析器,可以分析对于ADO.NET(SQL Server、Oracle等)、LINQ-to-SQL、EF(Code First&EF Core)的原始调用(如生成的sql语句)。
    可编程式的分析器,通过在想要分析的步骤上加上step。

    MiniProfiler官网:http://miniprofiler.com/
    MiniProfiler开源地址:https://github.com/MiniProfiler/dotnet

    MiniProfiler在.NetCore中使用起来非常的方便,配置简单,下面我们就来看一下怎么配置:

    1、在web项目中安装 MiniProfiler.AspNetCore.Mvc 程序包。

    2、在Startup.cs的ConfigureServices中配置MiniProfiler,这里主要完成MiniProfiler的自定义配置(如路由,如果没有特殊要求默认即可。)和内部服务在容器中的注入。

    1 public void ConfigureServices(IServiceCollection services)
    2 {
    3     services.AddMiniProfiler();
    4     services.AddControllersWithViews();
    5 }

    3、在Startup.cs的Configure中配置MiniProfiler,这里主要启用MiniProfilerMiddleware对HTTP请求进行监控。

    1 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    2 {
    3     ......
    4     app.UseMiniProfiler();
    5     ......
    6 }

    4、在为数据库启用MiniProfiler性能监控,将原有的_connection = new MySqlConnection(connectionString);替换成_connection = new ProfiledDbConnection(new MySqlConnection(connectionString), MiniProfiler.Current);就可以了。

    1 public UnitOfWork(IConfiguration configuration)
    2 {
    3     var connectionString = configuration.GetConnectionString("SqlConnection");
    4     _connection = new ProfiledDbConnection(new MySqlConnection(connectionString), MiniProfiler.Current);
    5     _connection.Open();
    6 }

    5、自定义监控,可以对指定的一段代码进行性能监控

    1 public TestDto Get(string id)
    2 {
    3     using (MiniProfiler.Current.Step("一个测试")) {
    4         var test = _testDomain.Get(id);
    5         return test.MapTo<TestDto>();
    6     }
    7 }

    6、页面添加引用和标签

     

    最后我们来看一下成果:

     单机“share”可以查看详情

     根据监控的数据,我们就能很好的了解整个程序的性能情况。

    源码地址:https://github.com/letnet/NetCoreDemo

  • 相关阅读:
    换行符 CR
    c# 定义的属性名与保留关键字冲突
    Redis 以window 服务启动
    c# Guid.NewGuid().ToString(format
    select 下拉源动态赋值
    html 控制input标签只能输入数字
    HTTP 错误 500.19
    Android debugger 出现提示Connected to the target VM, address: 'localhost:xxxx', transport: 'socket'
    siege--Web性能压测工具
    python+selenium上传文件注意点
  • 原文地址:https://www.cnblogs.com/letnet/p/12724891.html
Copyright © 2020-2023  润新知