使用Glimpse 監測ASP.NET MVC網站

來源:互聯網
上載者:User

使用MiniProfiler調試ASP.NET MVC網站效能,MiniProfiler可以很好的處理網站後端每個處理時間的事件,但是MiniProfiler是無法遠程做監測的動作,MiniProfiler只能夠監測本地端的動作,所以MiniProfier比較適合開發期間使用。

在開發ASP.NET WebFrom時,如果想要追蹤每個頁面的執行狀況與效能的話,其實是可以使用“Trace”功能,如此一來就可以在每個頁面的下方顯示頁面執行時的完整詳細資料,包括前端傳送的Http Request、所有的Session、Cookie等,對於開發時期來說,這些資訊可以協助我們在除錯時候可以掌握確切的資訊,然而系統上線之後,Trace功能勢必要關閉,而單靠ELMAH記錄錯誤訊息也無法完全掌握,因為有的時候執行正常並不表示功能正常,例如已耗用時間過慢、路徑錯誤等,

接下來就來介紹“Glimpse”,除了具有Trace的功能外,也可以結合Forms Authentication的登入認證,讓我們在系統上線之後,一樣可以即時讓開發人員追蹤頁面執行的各項資訊。

Glimpse是一款.NET下的效能測試工具,支援asp.net 、asp.net mvc, EF等等,優勢在於,不需要修改原項目任何代碼,且能輸出代碼執行各個環節的執行時間 ,安裝方式非常簡單,通過nuget直接安裝即可。

Glimpse

http://getglimpse.com/

http://nuget.org/List/Packages/Glimpse

What is Glimpse

At its core Glimpse allows you to debug your web site or web service right in the browser. Glimpse allows you to "Glimpse" into what's going on in your web server. In other words what Firebug is to debugging your client side code, Glimpse is to debugging your server within the client.
Fundamentally Glimpse is made up of 3 different parts, all of which are extensible and customizable for any platform:

  • Glimpse Server Modules – Code on the web server that collects meta data and sends it to the client.
  • Glimpse Protocol – A specification of that meta data.
  • Glimpse Client Side Viewer – Code on the web page or in the browser that takes the meta data from the server and displays it to the user.

在官網上的說明,目前Glimpse支援ASP.NET WebForm與ASP.NET MVC。

Glimpse有著類似Firefox的Firebug的外觀,可以在執行功能後隨時的展開或是收闔,但是Glimpse不是任何瀏覽器的外掛程式,Glimpse是一個依賴jQuery所建立的plugin,所以你的網站必須要引入使用jQuery,而瀏覽器則是不限定,根據官網所顯示的資訊,Glimpse可以支援多種的瀏覽器:Chrome 12, Firefox 4 and IE9。

使用NuGet安裝Glimpse

NuGet上面的Glimpse package目前只有支援ASP.NET MVC3

安裝完成後,也會一併安裝Glimpse for ASP.NET Beta(),安裝完成之後會在方案中建立一個檔案夾“App_Readme”,裡面有兩個Readme檔案,其中“glimpse.readme.txt” 的檔案內容裡有詳細說明,說明如何修改Web.Config以及功能介紹

通過NugGet安裝Glimpse,在Web.Config加了哪些東西

基本上安裝完成後就可以使用了,但在使用之前,先來看看Web.Config有多了什麼,在一開始的configSections中增加了「glimpse」的設定

然後在system.web的httpModules與httpHandlers都有增加,另外在system.webServer的modules與handlers中也有增加

而在Web.Config的最後面有多了一個glimpse的section,在這個Section中,可以針對不同的情境去改變設定,預設的設定是enabled=”true”,預設是把Glimpse的功能給開啟

在glimpse.readme.txt中就有說明,glimpse的Section如何做設定:

The following configuration values are allowed for Glimpse in your web.config:<glimpse enabled="true"     requestLimit="5"     loggingEnabled="false"    ipForwardingEnabled="false"    cacheEnabled="true">     <!--         set enabled to false to completely turn off Glimpse.         requestLimit specifies the max number of requests Glimpse will save.         enableLogging (false by default) if you experience problems with Glimpse         ipForwardingEnabled (false by default) will force Glimpse to validate IP addresses based on the value in the HTTP_X_FORWARDED_FOR header. Useful if your server is behind a proxy or load balancer.        cacheEnabled (true by default) Glimpse will tell browsers to cache static files by default    -->    <ipAddresses> <!-- List of IP addresses allowed to get Glimpse data. Optional. localhost (IPv4 & IPv6) by default -->        <add address="127.0.0.1" />        <add address="::1" />    </ipAddresses>    <contentTypes><!-- List of content types Glimpse will provide data for. Optional  text/html and application/json by default -->        <add contentType="text/html"/>    </contentTypes>    <pluginBlacklist><!-- List of plugins for Glimpse to ignore. Optional. By default all plugins will load -->        <add plugin="Glimpse.Core.Plugin.Request"/>        <add plugin="Glimpse.Core.Plugin.MetaData"/>    </pluginBlacklist>    <environments><!-- List of environments your application runs in. Used for the new environment switcher feature. Optional. By default environment information will no be shown -->        <add name="Dev" authority="localhost:33333" />        <add name="Prod" authority="getglimpse.com" />    </environments>    <urlBlacklist><!-- Glimpse will ignore all requests made to URI's that match any regular expression in this list. Optional. By default all URI's will be considered for Glimpse inspection. -->        <add url="{regex}"/>        <add url="{regex}"/>    </urlBlacklist></glimpse>

要開啟使用Glimpse相當簡單,只要輸入「http://你的網站網址/Glimpse.axd」就可以了,就會進入到下面的頁面

接著回到網站的頁面,可以看到頁面的右下角出現一個表徵圖,直接點擊表徵圖

其中會比較常用的有Ajax, Config, Enviroment, Execution, Request, Server, Trace, Views。

與ELMAH所遇到的問題是一樣的,那就是預設安裝後,都是可以匿名瀏覽,對於網站的安全性來說是個相當大的威脅,尤其是Glimpse的Config,會把Web.Config的所有資訊都完整呈現,所以這一篇文章就要來說明如何讓Glimpse在登入後才可以使用。

更改Web.Configglimpse配置

在Glimpse所提供的readme.txt中就已經有說明如何修改,讓啟用Glimpse是必須要登入後才可以,

<glimpse enabled="true" loggingEnabled="true" />

可以加上loggingEnabled=”true”,但是這樣還是不夠,必須要再進一步去阻止匿名使用者直接進入,所以修改如下:

<glimpse enabled="true" loggingEnabled="true" />

<location path="Glimpse.axd">

<system.web>

<authorization>

<deny users="*" />

</authorization>

</system.web>

</location>

如果說要再進一步防護的話,可以指定哪些使用者才能使用或是限定哪些角色許可權的使用者才能夠使用,於是我的修改如下:

<glimpse enabled="true" loggingEnabled="true" />

<location path="Glimpse.axd">

<system.web>

<authorization>

<allow roles="Admin"/>

<deny users="*" />

</authorization>

</system.web>

</location>

如此一來就阻止匿名使用者使用Glimpse功能,而且也只限定用有Admin角色許可權的使用者才能夠使用,不是Admin角色的使用者進入「http://你的網址/Glimpse.axd」時就會直接導回首頁。詳細的Glimpse Section的設定,我建議要詳讀官網的說明:

glimpse Document Configuration
http://getglimpse.com/Help/Configuration

另外要說明的是,如果你只希望在開發環境去啟用glimpse的功能,而在正式環境不希望去啟用glimpse時,並不需要上線前把glimpse給移除,只需要去更改glimpse Section的設定就可以,<glimpse enabled="false" loggingEnabled="true" />把原本enabled=”true” 改成 enabled=”false” 就可以。

其實glimpse可以結合ELMAH,讓ELMAH所記錄到的錯誤訊息於glimpse中顯示,在系統的登入認證後,只要啟用glimpse就可以去看ELMAH的紀錄資料,不必再另外進入ELMAH,

接下來介紹如何透過NuGet安裝Elmah plugin for Glimpse以及部分的修改設定。

請記得,你的網站必須示已經安裝了 ELMAH 以及 Glimpse,NuGet中搜尋 Glimpse就可以找到「Elmah plugin for Glimpse」

安裝完成之後,在你的網站上開啟glimpse後就可以在glimpse的功能視窗中看到「Elmah」的頁簽.

 

相關連結:

http://www.hanselman.com/blog/NuGetPackageOfTheWeek5DebuggingASPNETMVCApplicationsWithGlimpse.aspx

http://stackoverflow.com/questions/5746444/should-i-deploy-glimpse-to-the-production-site

使用 Glimpse 調試 ASP.NET MVC 應用

Elmah for Glimpse – Best of Both Worlds

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.