设为首页 加入收藏

TOP

记录数据库执行情况来分析数据库查询性能问题(四)
2014-11-24 08:19:30 来源: 作者: 【 】 浏览:16
Tags:记录 数据库 执行 情况 分析 查询 性能 问题
ew Code
public class ContextFactory
{
private static IContextHandler _ContextHandler;
private static object _lockPad = new object();
private static bool _Init = false;

///
/// 注册上下文句柄
///

///
public static void RegisterContextHandler(IContextHandler handler)
{
if (_Init == false)
{
lock (_lockPad)
{
if (_Init == false)
{
_ContextHandler = handler;
_Init = true;
}
}
}

}

///
/// 获取当前上下文
///

///
public static IContext GetContext()
{
if (_ContextHandler != null)
{
return _ContextHandler.GetContext();
}
else
{
return null;
}
}

///
/// 设置当前上下文(慎重使用)
///

public static void SetContext(IContext context)
{
_ContextHandler.SetContext(context);
}
}
复制代码


在应用程序中注册web上下文句柄,其实这里还可以注册很多类似的上下文句柄,比如异步信息等,有机会以后再分享。




protected void Application_Start()
{

SimpleWebContextHandler handler = new SimpleWebContextHandler();
ContextFactory.RegisterContextHandler(handler);
....
}
复制代码

第七:查看结果,这里只是记录在文本文件中,我们可以将这些日志记录在日志数据库中,然后通过各种语句产生不同的数据,比如查询访问时间最长的10条语句,使用次数最多的10条语句,某次请求中消耗时间最长的语句等等。


2012-02-22 15:33:46,545 [6] ERROR ApplicationLog.CommonLogger [(null)] - 请求id:0e6b7634-0f8e-49ee-8c1f-6e6700a143a9
请求IP:127.0.0.1
查询时间:20
查询SQL:select * from Customers

2012-02-22 15:33:46,592 [6] ERROR ApplicationLog.CommonLogger [(null)] - 请求id:0e6b7634-0f8e-49ee-8c1f-6e6700a143a9
请求IP:127.0.0.1
查询时间:0
查询SQL:select * from Categories

2012-02-22 15:33:46,592 [6] ERROR ApplicationLog.CommonLogger [(null)] - 请求id:0e6b7634-0f8e-49ee-8c1f-6e6700a143a9
请求IP:127.0.0.1
查询时间:0
查询SQL:select * from Orders



这里的内容主要是针对数据库访问,后面会利用AOP来记录每个方法的执行情况,无论此方法里面的具体操作是什么,统一记录,为程序员分析页面程序提供更加准备的信息。


摘自 min.jiang
首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇MongoDB学习(三)MongoDB shell命.. 下一篇MongoDB学习(一)安装配置

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)
·使用华为开发者空间 (2025-12-27 04:19:24)
·Getting Started wit (2025-12-27 03:49:24)
·Ubuntu 上最好用的中 (2025-12-27 03:49:20)