miniprofiler的对数据库的监测使用。以nancy,petapoco为例
2016-08-04 10:55
621 查看
miniprofiler的使用
miniprofiler的详细介绍请看这里http://miniprofiler.com/。(可以对数据库和页面等监控如 ado ef mvc mongodb)本文以nancy和petapoco为示例
安装
Install-Package MiniProfiler你也可以使用其他的类库,如MiniProfiler.EF6 MiniProfiler.WCF MiniProfiler.MongoDb MiniProfiler.MongoDb之类的。我用的是petapoco,所以用Install-Package MiniProfiler就可以了
数据库部分
在petapoco.cs的类中修改如下public void OpenSharedConnection() { if (_sharedConnectionDepth == 0) { _sharedConnection = _factory.CreateConnection(); _sharedConnection.ConnectionString = _connectionString; //MiniProfiler 加入MiniProfiler _sharedConnection= new StackExchange.Profiling.Data.ProfiledDbConnection( (DbConnection) _sharedConnection , MiniProfiler.Current); if (_sharedConnection.State == ConnectionState.Broken) { _sharedConnection.Close(); } if (_sharedConnection.State == ConnectionState.Closed) { _sharedConnection.Open(); } _sharedConnection = OnConnectionOpened(_sharedConnection); if (KeepConnectionAlive) { _sharedConnectionDepth++; } // Make sure you call Dispose } _sharedConnectionDepth++; }
其实就是替换_sharedConnection= new StackExchange.Profiling.Data.ProfiledDbConnection( (DbConnection) _sharedConnection , MiniProfiler.Current);
官方的文档
public static DbConnection GetOpenConnection() { var cnn = CreateRealConnection(); // A SqlConnection, SqliteConnection ... or whatever // wrap the connection with a profiling connection that tracks timings return new StackExchange.Profiling.Data.ProfiledDbConnection(cnn, MiniProfiler.Current); }
Bootstrapper部分 RequestStartup的方法中如下:
protected override void RequestStartup(TinyIoCContainer requestContainer, IPipelines pipelines, NancyContext context) { //MiniProfiler MiniProfiler.Start(); //请求结束后,停止miniprofiler pipelines.AfterRequest.AddItemToEndOfPipeline( (ctx) => { MiniProfiler.Stop(); }); }
module部分
假设你的module叫MiniProfiler。主要字符串的编码。
Get["/MiniProfiler"] = _ => { var renderStr = StackExchange.Profiling.MiniProfiler.RenderIncludes().ToString(); return View["MiniProfiler", renderStr]; };
视图部分
主要字符串的编码。Html.Raw是htmldecode的作用@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic> @{ Layout = null; } <!doctype html> <html lang="zh-cn"> <head> </head> <body> @Html.Raw(Model) </body> </html>
查看 http://localhost:9777/miniprofiler
结果类似如下30.0 ms 6.6 ms 1.1 ms 17.5 ms 14114.3 ms 537.0 ms
点击进去可以查看加载时间。以及调用的sql语句等。
相关文章推荐
- 视频教程--ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库
- ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库
- ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库
- 使用 C# + SQLite + PetaPoco + django 快速打造桌面程序
- 使用PetaPoco结合注入实现业务级事务
- .Net Core 中使用PetaPoco ,T4生成模版
- 使用SqlDependency监测数据库
- PetaPoco微型ORM的使用错误记录
- petapoco 使用 MiniProfiler Glimpse监控
- 使用PetaPoco结合注入实现业务级事务
- PetaPoco Sql 语法及使用
- 关于PetaPoco的T4模板使用
- 你使用PetaPoco必须知道的事情
- Centos 6.4 Mono下使用Petapoco与Oracle数据库
- PetaPoco在.net项目中的简单使用 (存储过程篇)
- 轻量级ORM框架初探-Dapper与PetaPoco的基本使用
- Petapoco使用SQLite的异常问题
- mono for android中使用dapper或petapoco对sqlite进行数据操作
- 解决在MySQL使用PetaPoco T4生成数据的实体时得到当前MySQL数据库下所有表的错误方法
- 使用 C# + SQLite + PetaPoco + django 快速打造桌面程序