miniprofiler的对数据库的监测使用。以nancy,petapoco为例
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语句等。
作者:过错
出处:http://www.cnblogs.com/wang2650/
关于作者:net开发做的久而已。十余年时光虚度!
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。如有问题,可以邮件:wang2650@163.com
联系我,非常感谢。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)