• NHibernate系列文章四:NHibernate运行时监控


    摘要

    有三种方式可以实现NHibernate运行时监控,监控的信息包括:执行了的SQL语句、NHibernate执行过程、数据库性能分析。这对我们学习NHibernate有很大的帮助,在工作中也能快速定位问题,最好是能够掌握他们。

    1. LogSqlInConsole

    修改上一节的SessionFactory属性,添加行x.LogSqlInConsole = true;

     1         public static ISessionFactory SessionFactory
     2         {
     3             get
     4             {
     5                 if (_sessionFactory == null)
     6                 {
     7                     var cfg = new Configuration();
     8 
     9                     cfg.DataBaseIntegration(x =>
    10                     {
    11                         x.ConnectionString = "Data Source=localhost;Initial Catalog=NHibernateDemoDB;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
    12                         x.Driver<SqlClientDriver>();
    13                         x.Dialect<MsSql2008Dialect>();
    14                         x.LogSqlInConsole = true;
    15                     });
    16                     cfg.AddAssembly(Assembly.GetExecutingAssembly());
    17                     _sessionFactory = cfg.BuildSessionFactory();
    18                 }
    19                 return _sessionFactory;
    20             }
    21         }

    修改main函数,只保留调用GetAll方法的语句

     1         static void Main(string[] args)
     2         {
     3             IList<Customer> list = GetAll();
     4             Console.WriteLine("customer list count: {0}", list.Count);
     5             foreach(var item in list)
     6             {
     7                 Console.WriteLine("{0} {1}", item.FirstName, item.LastName);
     8             }
     9             Console.ReadLine();
    10         }

      运行程序得到结果:

    可以看到,NHibernate自动生成了SQL查询语句。

    2.  使用NHibernateProfile

    NHibernateProfile下载。目前版本是3.0。

    下载后解压缩,执行文件NHProf.exe。按照提示注册一个trial license,发送license的xml文件到注册邮箱,登录注册邮箱,将license的xml文件从收件箱拿下到本地,导入license的xml文件。

    导入后,看到这个窗口。

    从解压后的文件夹中找到dll文件HibernatingRhinos.Profiler.Appender.dll,将他copy到工程目录的$packages文件夹下,在工程中添加引用这个文件。

    修改main函数首行添加代码HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();

     1         static void Main(string[] args)
     2         {
     3             HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
     4 
     5             IList<Customer> list = GetAll();
     6             Console.WriteLine("customer list count: {0}", list.Count);
     7             foreach(var item in list)
     8             {
     9                 Console.WriteLine("{0} {1}", item.FirstName, item.LastName);
    10             }
    11             Console.ReadLine();
    12         }

    执行程序,得到NHibernateProfile监控窗口。

    NHibernate Profile不但可以监控到执行的sql语句,而且能够抓取到NHibernate执行过程以及显示Hibernate异常详细信息。

    3. 使用Microsoft SQL Server自带的工具SQL Server Profile

    在Microsoft SQL Server Management Studio里,选择Tools->SQL Server Profile,在弹出对话框中输入连接的用户名和密码后,点击"OK",来到如下窗口:

    选择"Events Selection",将Audt Login / Logout和ExistingConnection前面的勾去掉(这里只关心我们应用程序执行的SQL语句),点击"Run"。

    来到这个窗口:

    再次执行程序,得到监控结果:

    这里有更详细的数据库性能信息,比如每条语句用了多长时间、应用进程ID等。

  • 相关阅读:
    解决phpmailer可以在windows下面发送成功, 在linux下面失败的问题
    centos安装svn
    linux下面配置安装nodejs+npm
    排序与搜索
    链表
    栈和队列
    顺序表
    初识算法、数据结构
    Linux_02
    Linux_01
  • 原文地址:https://www.cnblogs.com/uncle_danny/p/5635015.html
Copyright © 2020-2023  润新知