petapoco 使用 MiniProfiler Glimpse監控


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()

如圖

image4.在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.查看運行結果

運行程序,查看頁面如下圖:

~8]7L17HR6VS4WI}][5)J71

已經可以看到頁面生成所需要的時間,此時還沒監控到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的監控

Q48Z(_[%V}[4{$RYC7%M)Y3

O$B49RV(NIYVN7_]$}S_)97

其他 監控軟件參考資料

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) 

Glimpse Screenshot

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();
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM