MiniProfiler具体使用方法

本文以官网原文为基础,对MiniProfiler的使用方法进行分析和解释。

官网地址

使用方法

代码块分析

MiniProfiler可以为应用的每一个操作/行为创建分析步骤,如HTTP请求、启动过程。在分析器中使用“步骤”和“自定义的计时”实现性能监视,结构如下:每一层步骤都是上一次的子步骤

根监视点

    监视点/步骤1

        子步骤a

        子步骤b

            自定义监视

    监视点/步骤2

        自定义监视

在使用监视的时候,最好先从小的分析开始,根据需要逐步的缩小分析范围增加分析细节。具体使用时尽情调用MiniProfiler的拓展方法即可。 

       MiniProfiler.Current.Step(string name)
            最通用的简单方式
            参数:
                name:显示在分析结果中的名字

        .StepIf(string name, decimal minSaveMs, bool includeChildren = false)
            与Step类似,可以设置最小保存时间
            参数:
              name:显示在分析结果中的名字
              minSaveMs:最小保存时间。当步骤小于这个时间后不会记录在分析结果中
              includeChildren:是否在最小保存中计算子过程时间

        .CustomTiming(string category, string commandString, string executeType = null)
            自定义监视分析,设置分析的类别
            参数:
                category:监视分析的类别,会在分析结果中以列的形式展示
                commandString:监视的命令字符串,如sql语句、url
                executeType:命令的类型,如sql的Excute,url的post、get

        .CustomTimingIf(string category, string commandString, decimal minSaveMs, string executeType = null)
            与CustomTiming类似

         .Ignore()
            忽略某段过程

         .AddProfilerResults(MiniProfiler externalProfiler)
            添加分析结果。分析结果是一棵树,可以把树嵌套在另外一颗树中

        .AddCustomLink(string text, string url)
            在分析结果中添加自定义连接
            参数
                text:显示文本
                url:链接

 使用示例:

 /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        [HttpPost("Login")]
        public async Task<User> Login(string userName)
        {
            using (MiniProfiler.Current.Step("开始加载数据(根)"))
            {
                //这里是根的子步骤
                MiniProfiler.Current.Step("开始加载数据的子步骤");

                string url = @"http://localhost:8089/api/Equipment/1";
                //这里是子步骤的列
                using (MiniProfiler.Current.CustomTiming("newService", url, "get"))
                {
                    var client = new WebClient();
                    var reply = client.DownloadString(url);
                }

                UserSrevices userServices = new UserSrevices();
                List<User> users = await userServices.Query(p => p.UserName == userName && p.State == 1);
                return users == null || users.Count == 0 ? null : users.SingleOrDefault(); ;
            }

 SQL分析

MiniProfiler支持对Sql Server、MySQL等数据库进行性能分析,它们都是以ADO.NET的接口为基础的,如DBConnection。原理应该是一种【代理】,在代理中增加了connection调用前和调用后的时间消耗计算。对于Dapper、Linq2SQL轻量ORM(对ADO.NET的简单封装)可以直接使用MiniProfiler进行分析。

public DbConnection GetConnection()
{
    DbConnection connection = new System.Data.SqlClient.SqlConnection("...");
    return new StackExchange.Profiling.Data.ProfiledDbConnection(connection, MiniProfiler.Current);
}

SqlSugar分析

SqlSugar也是一种基于ADO.NET的ORM,但是它以sqlSugar的Client形式进行数据交换,因此无法直接使用MiniProfiler。但SqlSugar支持AOP日志,因此可以在AOPEvent中加入MiniProfiler。SqlSugar说明文档

思路就是在创建DB的时候在AOPEvent事件中定义性能分析。

  private DbContext()
        {
            if (string.IsNullOrEmpty(_connectionString))
                throw new ArgumentNullException("数据库连接字符串为空");
            _db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = _connectionString,
                DbType = _dbType,
                IsAutoCloseConnection = true,
                ConfigureExternalServices = new ConfigureExternalServices()
                {
                    //DataInfoCacheService = new HttpRuntimeCache()
                },
                MoreSettings = new ConnMoreSettings()
                {
                    //IsWithNoLockQuery = true,
                    IsAutoRemoveDataCache = true
                },
                AopEvents = new AopEvents()
                {
                    //执行后
                    OnLogExecuted = (sql, paras) =>
                    {
                        //MiniProfiler.Current.CustomTiming("OnLogExecuted", SqlHelper.ParameterFormat(sql, paras) + _db.Ado.SqlExecutionTime.ToString());

                    },
                    //执行前
                    OnLogExecuting = (sql, paras) =>
                    {
                        MiniProfiler.Current.CustomTiming("OnLogExecuting", SqlHelper.ParameterFormat(sql, paras));
                    },
                    OnError = (ex) => { 
                    /*
                     * 错误处理
                     */
                    },
                }
            });
        }

SqlSugar本身也是支持sql执行分析的,方法就是调用“_db.Ado.SqlExecutionTime”。效果如图所示

以上就是MiniProfiler的用法了。欢迎拓展

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值