PetaPoco是一款适用于.Net(window) 和Mono( linux )的微小、快速、单文件的微型ORM。

MVC MiniProfilerStack Overflow团队设计的一款对ASP.NET MVC的性能分析的小程序,适用于.Net(window) 和Mono( linux )。可以对一个页面本身,及该页面通过直接引用、Ajax、Iframe形式访问的其它页面进行监控,监控内容包括数据库内容,并可以显示数据库访问的SQL(支持EF、EF CodeFirst等 )。并且以很友好的方式展现在页面上。

该Profiler的一个特别有用的功能是它与数据库框架的集成。除了.NET原生的 DbConnection类,profiler还内置了对实体框架(Entity Framework)以及LINQ to SQL的支持。任何执行的Step都会包括当时查询的次数和所花费的时间。为了检测常见的错误,如N+1反模式,profiler将检测仅有参数值存在差 异的多个查询。

MiniProfiler是以Apache License V2.0协议发布的,你可以在NuGet找到。

1.安装MiniProfiler

Install-Package MiniProfiler
Install-Package MiniProfiler.MVC4

2.在Global.asax加入MiniProfiler相关的监控代码

using StackExchange.Profiling; 
protected void Application_BeginRequest()
{
if (Request.IsLocal)
{
MiniProfiler.Start();
}
}
protected void Application_EndRequest()
{
MiniProfiler.Stop();
}

3.修改_Layout.cshtml视图文件

在Views\Shared\_Layout.cshtml文件的body前面加上一段代码,让监控展示在页面上。

增加

@StackExchange.Profiling.MiniProfiler.RenderIncludes()

如图

4.在Web.config加入代码

<system.webServer>
<handlers>
<add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
</handlers>
</system.webServer>

4.查看运行结果

运行程序,查看页面如下图:

已经可以看到页面生成所需要的时间,此时还没监控到sql的运行情况

5.给内部函数增加监控

var profiler = MiniProfiler.Current;
using (profiler.Step("获取UserInfo"))
{
tmp = _userService.GetUserInfo(1);
tmp.username = "王五";
}

6.给petapoco增加监控,新增 DatabaseWithMVCMiniProfiler.cs

using System;
using System.Data;
using System.Data.Common;
using StackExchange.Profiling;
namespace SL.ORM.PetaPoco
{
public class DatabaseWithMiniProfiler : Database
{
public DatabaseWithMiniProfiler(IDbConnection connection) : base(connection) { }
public DatabaseWithMiniProfiler(string connectionStringName) : base(connectionStringName) { }
public DatabaseWithMiniProfiler(string connectionString, string providerName) : base(connectionString, providerName) { }
public DatabaseWithMiniProfiler(string connectionString, DbProviderFactory dbProviderFactory) : base(connectionString, dbProviderFactory) { }
        public override IDbConnection OnConnectionOpened(IDbConnection connection)//重点
{
// wrap the connection with a profiling connection that tracks timings
return new StackExchange.Profiling.Data.ProfiledDbConnection((DbConnection)connection, MiniProfiler.Current);
}
} }

7.生成和返回Database

public Database AccountDbContext
{
get
{
//return new Database(connectionName);
return new DatabaseWithMiniProfiler(connectionName);//使用MiniProfiler监控性能
}
set { this.AccountDbContext = value; }
}

8.再次运行可以看到有了sql的监控

其他 监控软件参考资料

There are a few ways to debug/profile NPoco. They are listed below, and are commonly done by inheriting from Database and overriding a specific method. Note: Make sure you instantiate your new class (MyDb as below) when creating a Database from then on.

NanoProfiler是一个EF Learning Labs出品的免费性能监控类库(即将开源)。它的思想和使用方式类似于MiniProfiler的。

国人设计,并开源.

http://www.cnblogs.com/teddyma/p/NanoProfiler_Introduction.html
public class MyDb : Database
{
public MyDb(string connectionStringName) : base(connectionStringName) { }
public override IDbConnection OnConnectionOpened(IDbConnection conn)
{
var dbprofiler=new DbProfiler(profilingSession.Current.Profiler);
return new ProfiledDbConnection(conn,dbprofiler);
}
}
miniprofiler

http://miniprofiler.com/

public class MyDb : Database
{
public MyDb(string connectionStringName) : base(connectionStringName) { }
public override IDbConnection OnConnectionOpened(IDbConnection conn)
{
return new ProfiledDbConnection((DbConnection)conn, MiniProfiler.Current);
}
}
Manual
public class MyDb : Database
{
public MyDb(string connectionStringName) : base(connectionStringName) { }
public override void OnExecutingCommand(IDbCommand cmd)
{
File.WriteAllText("log.txt", FormatCommand(cmd));
}
}
 
Glimpse

http://getglimpse.com/

Glimpse will usually hook itself up by installing the following packages.

Install-Package Glimpse.ADO
Install-Package Glimpse.Mvc4 (or your mvc version)

Show last SQL executed on the ASP.NET error page

Credit: Sam Saffron

public class MyDb : Database
{
public MyDb(string connectionStringName) : base(connectionStringName) { } public override void OnException(Exception e)
{
base.OnException(e);
e.Data["LastSQL"] = this.LastSQL;
}
}
void Application_Error(object sender, EventArgs e)
{
var lastError = Server.GetLastError(); string sql = null;
try
{
sql = lastError.Data["LastSQL"] as string;
}
catch
{
// skip it
}
if (sql == null) return; var ex = new HttpUnhandledException("An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.", lastError); Server.ClearError(); var html = ex.GetHtmlErrorMessage();
html = html.Insert(html.IndexOf("<b>Stack Trace:</b>"), @"
<b>Last Sql:</b><br><br>
<table width='100%' bgcolor='#ffffccc'>
<tbody>
<tr>
<td><code><pre>" + sql + @"</pre></code></td>
</tr>
</tbody>
</table><br>"); Response.Write(html);
Response.StatusCode = 500;
Response.End();
}

petapoco 使用 MiniProfiler Glimpse监控的更多相关文章

  1. MiniProfiler(MiniProfiler.EF6监控调试MVC5和EF6的性能)

    git:  https://github.com/MiniProfiler 以前开发Webform的时候可以开启trace来跟踪页面事件,这对于诊断程序的性能是有很大的帮助的,起到事半功倍的作用,今天 ...

  2. MiniProfiler.EF6监控调试MVC5和EF6的性能

    转自:蓝狐学MVC教程 以前开发Webform的时候可以开启trace来跟踪页面事件,这对于诊断程序的性能是有很大的帮助的,起到事半功倍的作用,今天我就来谈用mvc开 发项目的调试和性能监控.EF框架 ...

  3. MVC教程--MiniProfiler.EF监控调试MVC和EF的性能

    上一篇谈到mvc中ef输出执行sql日志:来谈用mvc开发项目的调试和性能监控.EF框架自动给我生成sql语句,当我们的程序遇到性能问题的时候我们可以用MiniProfiler.EF来监控调试MVC和 ...

  4. MiniProfiler性能监控分析工具在.NET项目中的使用

    MiniProfiler是一款针对.NET, Ruby, Go and Node.js的性能分析的轻量级程序.可以对一个页面本身,及该页面通过直接引用.Ajax.Iframe形式访问的其它页面进行监控 ...

  5. 转:asp.net mvc ef 性能监控调试工具 MiniProfiler

    MiniProfiler官网:http://miniprofiler.com/ MiniProfiler的一个特别有用的功能是它与数据库框架的集成.除了.NET原生的 DbConnection类,Mi ...

  6. 使用MiniProfiler跟踪MVC + EF + Bootstrap 2 权限管理系统的性能消耗

    安装MiniProfiler 在MVC + EF + Bootstrap 2 权限管理系统入门级(附源码)文章中下载了它的源码,调试模式下打开一个页面都要再2.5秒以上,所以使用MiniProfile ...

  7. 使用MiniProfiler调试ASP.NET MVC网站性能

    MiniProfiler 以前开发Webform的时候可以开启trace来跟踪页面事件,这对于诊断程序的性能是有很大的帮助的,起到事半功倍的作用,今天我就来谈用mvc开 发项目的调试和性能监控.EF框 ...

  8. 使用MiniProfiler调试Asp.net Mvc性能

    使用nuget添加MiniProfiler.EF组件 在Global文件中配置MiniProfiler protected void Application_Start() { AreaRegistr ...

  9. ASP.NET Core WebAPI中的分析工具MiniProfiler

    介绍 作为一个开发人员,你知道如何分析自己开发的Api性能么? 在Visual Studio和Azure中, 我们可以使用Application Insight来监控项目.除此之外我们还可以使用一个免 ...

随机推荐

  1. oracle win7下 卸载

    1 右击“计算机”-->管理-->服务和应用程序-->服务,停掉所有Oracle相关的服务(以Oracle打头的,比如OracleDBConsoleorcl). 2 开始--> ...

  2. php count函数

    最近被问到一个函数count 1.count("123456") 对应的输出是什么? 2.count(null) 对应的输出是什么? 以前没有认真的考虑,只是心里有一个印象那就是c ...

  3. solaris安装oracle遇INS 30131 错误

    安装11.2.0.4,报错: [FATAL] [INS-30131] Initial setup required for the execution of installer validations ...

  4. 【如何快速的开发一个完整的iOS直播app】(原理篇)

    原文转自:袁峥Seemygo    感谢分享.自我学习 目录 [如何快速的开发一个完整的iOS直播app](原理篇) [如何快速的开发一个完整的iOS直播app](播放篇) [如何快速的开发一个完整的 ...

  5. getElementByName()和getElementById的区别

    因为在属性中,id时唯一的,getElementById取出的是一个元素但是可以出现相同的name,取到的是一个Array ,getElementsByName取出的是数组 记录代码如下: <! ...

  6. 在linux中添加ftp用户,并设置相应的权限

    在linux中添加ftp用户,并设置相应的权限,操作步骤如下: 1.环境:ftp为vsftp.被限制用户名为test.被限制路径为/home/test 2.建用户:在root用户下: useradd ...

  7. Django + Apache + 树莓派 搭建内网微信公众号服务器

    其实早在微信开放公众号开发平台时就想弄一个自己的公众号服务器,奈何对web服务器搭建和开发一窍不通,只是注册了一下开发者帐号,并没有采取行动,万恶的拖延症. 前一年,开始接触python,打开了神奇世 ...

  8. 01-Swift 介绍

    简介 Swift 语言由苹果公司在 2014 年推出,用来撰写 OS X 和 iOS 应用程序 2014 年,在 Apple WWDC 发布 几家欢喜,几家愁 愁者:只学Object-C的人 欢喜者: ...

  9. [转]C#在创建完项目后如何重命名项目名称。

    今天写了个C#的小测试程序,一开始使用的默认命名WindowsFormsApplication2,写完后觉得名字不好看,于是想改个名字,但是试了一下,想完整的改名还挺复杂,不但要改解决方案名,项目名, ...

  10. 表单验证插件 - formValidator

    表单验证插件 - formValidator * 引入formValidator插件文件 * 引入formValidator插件的主文件 * 引入formValidator插件的正则有关文件 * 引入 ...