ASP.NET 2.0 Health Monitoring

0 comments
ASP.NET 2.0 Health Monitoring 健康監視系統提供了 Web 事件功能,用來監控 ASP.NET 應用程式的各種狀況。過去如果要追蹤系統錯誤、或是稽核某些特定事件,就必須自己撰寫程式碼來完成這些工作。但現在你甚至可以不寫任何程式碼,只要藉由 Web.config 設定,就可以啟用健康監視功能,為你的應用程式記錄例外錯誤、使用者驗證等等事件資訊。 健康監視系統提供許多 WebEventProvider 的選擇,你可以將事件資訊記錄在寫入 Windows 事件記錄檔、SQL Server 資料庫、ASP.NET trace messages、WMI 或是發送電子郵件傳送。

以下是啟用健康監視功能的設定:
<configuration>
<system.web>
<healthmonitoring enabled="true">
</healthmonitoring>
</system.web>
</configuration>


如果只做這樣的設定,便會繼承全域 Web.config 檔的預設組態設定,其檔案位於下列目錄:

%windir%\Microsoft.Net\Framework\v2.0.*\config\Web.config

其預設是使用使用 Windows 事件記錄檔提供者(EventLogWebEventProvider),所有事件記錄將會被記錄在 Windows 事件記錄檔。當這些預設組態並不能符合你的實際需求時,就要進一步為你的應用程式實作 <healthmonitoring> 區段設定屬於自己的設定或覆寫這些設定。

<healthmonitoring> 區段包含下列項目:


  • providers 定義處理事件的健康監視提供者。
  • eventMappings 對應 Web Event 類別,來訂閱你要監控的事件。 你也可以繼承基礎的 Web Event 類別產生自訂的事件。
  • rules 定義規提供者與事件的關聯規則,也就是指定你所訂閱的事件該由哪個 Provider 處理。

你可以在 System.Web.Management 命名空間找到相關的 Provider 及 Web Event 類別。如果要將事件資訊記錄在 SQL Server 資料庫,則必須要使用 aspnet_regsql.exe 工具來建立 SqlWebEventProvider 所需要的資料庫。請開啟 Visual Studio 2005 命令提示字元視窗輸入下列指令:
aspnet_regsql.exe -E -S <servername> -A w

執行完成後,你的 SQL Server 就會新增一個名為的 aspnetdb 的資料庫以及 aspnet_WebEvent_Events 資料表。

以下 Web.config 範例,使用 SqlWebEventProvider 記錄所有錯誤事件:
<configuration>
<connectionStrings>
<add name="AspNetSqlConnectionString"
connectionString="Server=.;database=aspnetdb;IntegratedSecurity=SSPI;"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<healthMonitoring enabled="true">
<providers>
<clear />
<add connectionStringName="AspNetSqlConnectionString"
maxEventDetailsLength="1073741823"
buffer="false"
name="SqlWebEventProvider"
type="System.Web.Management.SqlWebEventProvider"/>
</providers>
<eventMappings>
<clear />
<add name="All Errors" type="System.Web.Management.WebBaseErrorEvent"
startEventCode="0" endEventCode="2147483647" />
</eventMappings>
<rules>
<clear/>
<add name="All Errors Default" eventName="All Errors"
provider="SqlWebEventProvider"
profile="Default" minInstances="1" maxLimit="Infinite"
minInterval="00:00:00" />
</rules>
</healthMonitoring>
</system.web>
</configuration>

繼續閱讀...

設置 ASP.NET 網站管理工具

2 comments
ASP.NET 2.0 提供了網站管理工具可以讓任何擁有網站系統管理權限的使用者管理網站的組態設定。在 Visual Studio 中你可以點選 [網站] -> [ASP.NET 組態] 開啟網站管理工具。

如果你需要在你的 Web 伺服器啟用 ASP.NET 網站管理工具,請參照以下步驟:
  1. 開啟網際網路資訊服務 (IIS) 管理員。
  2. 在預設的網站新增名為 WebSiteAdmin 的虛擬目錄,並將其路徑指向 %WINDIR%\Microsoft.NET\Framework\v2.0.50727\ASP.NETWebAdminFiles。
  3. 開啟 WebSiteAdmin 的 [內容] 對話方塊。
  4. 按一下 [ASP.NET] 索引標籤,選擇 ASP.NET 版本為 2.0.50727。
  5. 按一下 [目錄安全設定] 索引標籤,在 [匿名存取及驗證控制] 區段中,按一下 [編輯] 按鈕。
    在出現的 [驗證方法] 對話盒中,清除 [匿名存取] 核取方塊,然後選取 [整合式 Windows 驗證] 核取方塊。
存取網站管理工具時,你必須提供 applicationPhysicalPath、applicationUrl 兩個傳遞參數,例如:
http://localhost/WebSiteAdmin/default.aspx?applicationPhysicalPath=C:\Inetpub\wwwroot\WebSite1\&applicationUrl=/WebSite1
上例會告訴管理工具你的應用程式實際路徑位於 C:\Inetpub\wwwroot\WebSite1\,且其網址為 /WebSite1。

在預設的情況下,網站管理工具只限本機存取,如果要開放遠端存取,那麼就必須在 %WINDIR%\Microsoft.NET\Framework\v2.0.50727\ASP.NETWebAdminFiles\App_Code\WebAdminPage.cs 中,註解下列程式碼:
if (!application.Context.Request.IsLocal) {             
SecurityException securityException = new SecurityException(
(string)HttpContext.GetGlobalResourceObject("GlobalResources",
"WebAdmin_ConfigurationIsLocalOnly"));
WebAdminPage.SetCurrentException(application.Context,securityException);
application.Server.Transfer("~/error.aspx");
}

繼續閱讀...